GrandTabernacleAutoVI/js/game.js
Atsuyo-INSA aa4348c407 names
2023-12-06 10:14:56 +01:00

154 lines
No EOL
3.2 KiB
JavaScript

function CookiePseudo() {
nom = "pseudo=";
var liste = document. cookie. split (';');
for (var i = 0; i < liste.length; i++) {
var c = liste[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nom) == 0) return c.substring(nom.length, c.length);
}
return null;
}
Renderer = new Render("canvas", "./assets/map/map7_recadr.png");
let Net = new Network("ws://129.151.227.50:8080?name="+CookiePseudo(), Renderer);
//let ClientKeyboard = new Keyboard()
let playerId = null; //id of client player
let players = [];
let bullets = [];
let selfBullets = [];
let player = null;
Inp = new Input("canvas", Net,Renderer);
playerId=Net.playerId;
player=Net.clientPlayer;
players=Net.getPlayersToAdd();
let dt = 1;
let playerToUpdate;
function update()
{
playerToUpdate = Net.getPlayersToUpdate();
for (let i = 0;i<playerToUpdate.length;i++)
{
for (let j = 0;j<players.length;j++)
{
if(players[j].id==playerToUpdate[i].id)
{
players[j].x=playerToUpdate[i].x;
players[j].y=playerToUpdate[i].y;
players[j].dir=playerToUpdate[i].dir;
players[j].visibleDir=playerToUpdate[i].visibleDir;
break;
}
}
}
}
function addPlayers()
{
let playersToAdd = Net.getPlayersToAdd();
if (playersToAdd.length==0)
return;
console.log(playersToAdd);
playersToAdd.forEach((p) => {
console.log("New player: ",p.id);
players.push(p);
Renderer.AddPlayer(p);
});
}
function remPlayers()
{
let playerToRemove = Net.getPlayersToRemove();
if(playerToRemove.length==0)
return;
for(let i=0;i<playerToRemove.length;i++)
{
let id = playerToRemove[i];
Renderer.RemPlayer(id);
console.log("deleting Player ",id);
for(let i = 0;i<players.length;i++)
{
if (players[i].id==id)
{
players.splice(i,1);
break;
}
}
}
}
function addBullets()
{
let bulletsToAdd = Net.getBulletsToAdd();
if (bulletsToAdd.length==0)
return;
bulletsToAdd.forEach((b) => {
console.log("new bullet");
bullets.push(p);
Renderer.addBullet(p);
});
}
function updateBullets(list,target,dt)
{
for(let i = list.length-1;i>=0;i--)
{
list[i].update(dt);
list[i].checkCollisions(target,squares,circles);
if(list[i].deleted)
{
Renderer.remBullet(list[i]);
list.splice(i,1);
}
}
}
let currentTime = new Date();
function game() {
if(playerId==null)
{
if (Net.playerId!=null)
{
playerId=Net.playerId;
player=Net.clientPlayer;
players=Net.getPlayersToAdd();
//Inp.player=player; //pour connecter les input au joueur client
console.log("Connected as id ",playerId);
Inp.player=player;
Inp.bullets=selfBullets;
Renderer.AddPlayer(player)
players.forEach((p) => {
Renderer.AddPlayer(p)
});
}
}
else
{
update();
addPlayers();
remPlayers();
addBullets();
player.update(squares, circles, dt);
for (var i = players.length - 1; i >= 0; i--) {
players[i].update(squares, circles, dt);
}
Renderer.ReloadAff();
updateBullets(selfBullets,null,dt);
updateBullets(bullets,player,dt);
}
let newTime = new Date();
dt=newTime - currentTime;
currentTime=newTime;
}
Net.connect(); //connect to server, create a player, and retrieve all players info
setInterval(game);