GrandTabernacleAutoVI/js/game.js

185 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-12-06 09:33:35 +01:00
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;
}
2023-11-15 09:09:13 +01:00
Renderer = new Render("canvas", "./assets/map/map7_recadr.png");
2023-12-06 14:02:35 +01:00
LB = new LeaderBoard("canvas");
2023-12-07 16:29:34 +01:00
cars = [new Car(Renderer, 220, 75, 0, 1, 0),
new Car(Renderer, 500, 75, 0, 1, 0),
new Car(Renderer, 900,75, 0, 1, 0),
new Car(Renderer, 228,587, 0, 1, 0),
new Car(Renderer, 947,587, 0, 1, 0),
new Car(Renderer, 1247,600, 1, 4, Math.PI/2),
new Car(Renderer, 607,330, 1, 4, Math.PI/2)]
2023-12-06 09:33:35 +01:00
let Net = new Network("ws://129.151.227.50:8080?name="+CookiePseudo(), Renderer);
2023-11-15 09:09:13 +01:00
2023-11-18 20:17:47 +01:00
2023-11-29 17:25:40 +01:00
let playerId = null; //id of client player
2023-11-19 18:38:27 +01:00
let players = [];
2023-12-06 09:50:34 +01:00
let bullets = [];
2023-11-29 17:25:40 +01:00
let player = null;
2023-11-15 09:09:13 +01:00
2023-12-06 09:50:34 +01:00
Inp = new Input("canvas", Net,Renderer);
2023-11-29 16:21:20 +01:00
2023-11-29 17:25:40 +01:00
playerId=Net.playerId;
player=Net.clientPlayer;
players=Net.getPlayersToAdd();
2023-11-29 19:12:36 +01:00
let dt = 1;
2023-11-29 17:25:40 +01:00
2023-12-05 15:01:15 +01:00
function update()
{
2023-12-06 10:46:06 +01:00
let playerToUpdate = Net.getPlayersToUpdate();
2023-12-05 15:01:15 +01:00
for (let i = 0;i<playerToUpdate.length;i++)
{
2023-12-07 09:45:58 +01:00
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++)
2023-12-05 15:01:15 +01:00
{
2023-12-07 09:45:58 +01:00
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;
}
2023-12-05 15:01:15 +01:00
}
}
}
}
function addPlayers()
{
let playersToAdd = Net.getPlayersToAdd();
playersToAdd.forEach((p) => {
2023-12-06 09:50:34 +01:00
console.log("New player: ",p.id);
players.push(p);
2023-12-05 15:01:15 +01:00
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;
}
}
}
}
2023-11-18 20:17:47 +01:00
2023-12-06 09:50:34 +01:00
function addBullets()
{
let bulletsToAdd = Net.getBulletsToAdd();
bulletsToAdd.forEach((b) => {
2023-12-06 10:30:35 +01:00
bullets.push(b);
Renderer.addBullet(b);
2023-12-06 09:50:34 +01:00
});
2023-12-07 13:45:22 +01:00
2023-12-06 09:50:34 +01:00
}
2023-12-06 10:46:06 +01:00
function updateBullets(dt)
2023-12-06 09:50:34 +01:00
{
2023-12-06 10:46:06 +01:00
for(let i = bullets.length-1;i>=0;i--)
2023-12-06 09:50:34 +01:00
{
2023-12-06 10:46:06 +01:00
bullets[i].update(dt);
2023-12-07 13:42:18 +01:00
bullets[i].checkCollisions(player,squares,circles,Net);
2023-12-06 10:46:06 +01:00
if(bullets[i].deleted)
2023-12-06 09:50:34 +01:00
{
2023-12-06 10:46:06 +01:00
Renderer.remBullet(bullets[i]);
bullets.splice(i,1);
2023-12-06 09:50:34 +01:00
}
}
}
2023-12-07 14:06:11 +01:00
function updateKills()
{
let deaths = Net.getDeathToAdd();
deaths.forEach((object) => {
let dead = object.id;
let killer = object.killerId;
console.log(killer,"killed",dead);
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++;
}
});
}
2023-11-29 19:03:50 +01:00
let currentTime = new Date();
2023-11-15 09:09:13 +01:00
function game() {
2023-11-29 17:25:40 +01:00
if(playerId==null)
{
if (Net.playerId!=null)
{
playerId=Net.playerId;
player=Net.clientPlayer;
players=Net.getPlayersToAdd();
2023-12-06 19:49:59 +01:00
Renderer.AddPlayer(player)
2023-12-07 09:35:35 +01:00
Renderer.SetPlayerId(player.id)
2023-12-06 19:49:59 +01:00
2023-11-29 17:25:40 +01:00
console.log("Connected as id ",playerId);
2023-11-29 18:01:11 +01:00
Inp.player=player;
2023-12-06 10:46:06 +01:00
Inp.bullets=bullets;
2023-12-06 19:49:59 +01:00
2023-11-29 18:13:33 +01:00
players.forEach((p) => {
Renderer.AddPlayer(p)
2023-12-05 15:01:15 +01:00
});
2023-12-06 14:02:35 +01:00
LB.ReloadAff(players, player);
2023-11-29 17:25:40 +01:00
}
}
else
{
2023-12-05 15:01:15 +01:00
update();
addPlayers();
remPlayers();
2023-12-06 09:50:34 +01:00
addBullets();
2023-12-07 14:06:11 +01:00
updateKills();
2023-12-05 15:08:02 +01:00
player.update(squares, circles, dt);
2023-11-29 17:25:40 +01:00
for (var i = players.length - 1; i >= 0; i--) {
2023-12-06 09:50:34 +01:00
players[i].update(squares, circles, dt);
2023-11-29 17:25:40 +01:00
}
Renderer.ReloadAff();
2023-12-07 12:04:32 +01:00
cars.forEach((c) => {
c.Update();
})
2023-12-06 10:46:06 +01:00
updateBullets(dt);
2023-12-06 14:02:35 +01:00
LB.ReloadAff(players, player);
2023-11-19 18:55:21 +01:00
}
2023-11-29 19:03:50 +01:00
let newTime = new Date();
dt=newTime - currentTime;
2023-12-06 09:50:34 +01:00
currentTime=newTime;
2023-11-19 19:20:02 +01:00
}
2023-11-29 17:36:27 +01:00
Net.connect(); //connect to server, create a player, and retrieve all players info
2023-11-29 17:06:30 +01:00
2023-12-06 09:50:34 +01:00
setInterval(game);