GrandTabernacleAutoVI/js/game.js

111 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-11-15 09:09:13 +01:00
Renderer = new Render("canvas", "./assets/map/map7_recadr.png");
2023-11-29 15:53:35 +01:00
let Net = new Network("ws://129.151.227.50:8080/", Renderer);
//let ClientKeyboard = new Keyboard()
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-11-29 17:25:40 +01:00
let player = null;
2023-11-15 09:09:13 +01:00
2023-11-29 18:11:58 +01:00
Inp = new Input("canvas", Net);
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
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;
playersToAdd.forEach((p) => {
console.log("New player: ",playersToAdd[0].id);
players.push(playersToAdd[0]);
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;
}
}
}
}
2023-11-18 20:17:47 +01:00
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();
//Inp.player=player; //pour connecter les input au joueur client
console.log("Connected as id ",playerId);
2023-11-29 18:01:11 +01:00
Inp.player=player;
2023-11-29 18:11:58 +01:00
Renderer.AddPlayer(player)
2023-11-29 18:13:33 +01:00
players.forEach((p) => {
Renderer.AddPlayer(p)
2023-12-05 15:01:15 +01:00
});
2023-11-29 17:25:40 +01:00
}
}
else
{
2023-12-05 15:01:15 +01:00
update();
addPlayers();
remPlayers();
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-05 15:20:51 +01:00
players[i].update(squares, circles, dt);//squares, circles
2023-11-29 17:25:40 +01:00
}
Renderer.ReloadAff();
2023-11-19 18:55:21 +01:00
}
2023-11-29 19:03:50 +01:00
let newTime = new Date();
dt=newTime - currentTime;
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-11-29 17:36:27 +01:00
setInterval(game)