GrandTabernacleAutoVI/public_html/js/game.js
2023-12-08 14:21:43 +01:00

200 lines
No EOL
4.3 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/map_principale.png", "./assets/map/map_secondaire.png");
LB = new LeaderBoard("canvas");
let sound = new Sound("./assets/sounds/");
sound.loadSounds();
cars = [new Car(Renderer, 0, 0, sound),
new Car(Renderer, 0, 7, sound),
new Car(Renderer, 1, 7, sound),
new Car(Renderer, 1, 13, sound),
new Car(Renderer, 1, 14, sound),
new Car(Renderer, 0, 15, sound)]
let Net = new Network("wss://ws.gta6.insat.fr:8080?name="+CookiePseudo(), sound);
let playerId = null; //id of client player
let players = [];
let bullets = [];
let player = null;
Inp = new Input("canvas", Net,Renderer, sound);
playerId=Net.playerId;
player=Net.clientPlayer;
players=Net.getPlayersToAdd();
let dt = 1;
function update()
{
if(player.y>470 && player.y<480 && player.x>237 && player.x<310) {
player.x=2510
player.y=2714
} else if (player.x>2588 && player.x<2687 && player.y>2129 && player.y<2180) {
player.x=270
player.y=250
}
let playerToUpdate = Net.getPlayersToUpdate();
for (let i = 0;i<playerToUpdate.length;i++)
{
if(playerToUpdate[i].id==player.id) {
player.x=playerToUpdate[i].x;
player.y=playerToUpdate[i].y;
player.dir=playerToUpdate[i].dir;
player.visibleDir=playerToUpdate[i].visibleDir;
} else {
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();
playersToAdd.forEach((p) => {
console.log("New player: ",p.id);
players.push(p);
Renderer.AddPlayer(p);
});
}
function remPlayers()
{
let playerToRemove = Net.getPlayersToRemove();
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();
bulletsToAdd.forEach((b) => {
bullets.push(b);
Renderer.addBullet(b);
});
}
function updateBullets(dt)
{
for(let i = bullets.length-1;i>=0;i--)
{
bullets[i].update(dt);
bullets[i].checkCollisions(player,squares,circles,Net);
if(bullets[i].deleted)
{
Renderer.remBullet(bullets[i]);
bullets.splice(i,1);
}
}
}
function updateKills()
{
let deaths = Net.getDeathToAdd();
deaths.forEach((object) => {
let dead = object.id;
let killer = object.killerId;
if(player.id==killer)
player.kill++;
for(let i=0;i<players.length;i++)
{
if(players[i].id==dead)
players[i].death++;
if(players[i].id==killer)
players[i].kill++;
}
});
}
let currentTime = new Date();
function game() {
if(playerId==null)
{
if (Net.playerId!=null)
{
playerId=Net.playerId;
player=Net.clientPlayer;
players=Net.getPlayersToAdd();
Renderer.AddPlayer(player)
Renderer.SetPlayerId(player.id)
console.log("Connected as id ",playerId);
Inp.player=player;
Inp.bullets=bullets;
players.forEach((p) => {
Renderer.AddPlayer(p)
});
players.push(new Player(-1,-50,-50,"VOITURE",0,null));
LB.ReloadAff(players, player);
}
}
else
{
update();
addPlayers();
remPlayers();
addBullets();
updateKills();
player.update(squares, circles, dt);
for (var i = players.length - 1; i >= 0; i--) {
players[i].update(squares, circles, dt);
}
Renderer.ReloadAff();
cars.forEach((c) => {
c.Update();
if(c.collide(player.x,player.y))
{
Net.died(player.id,-1);
player.x=-50;
player.deaths++;
player.health=10;
}
});
updateBullets(dt);
LB.ReloadAff(players, player);
}
let newTime = new Date();
dt=newTime - currentTime;
currentTime=newTime;
}
Net.connect(); //connect to server, create a player, and retrieve all players info
setInterval(game);