GrandTabernacleAutoVI/js/game.js

89 lines
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-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-11-29 17:25:40 +01:00
}
}
else
{
2023-11-29 18:36:28 +01:00
let 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)
{
2023-11-29 18:54:28 +01:00
//console.log("updated player",playerToUpdate[i].id);
2023-11-29 18:36:28 +01:00
players[j].x=playerToUpdate[i].x;
players[j].y=playerToUpdate[i].y;
players[j].dir=playerToUpdate[i].dir;
2023-11-29 18:54:28 +01:00
players[j].visibleDir=playerToUpdate[i].visibleDir;
//console.log();
2023-11-29 18:36:28 +01:00
break;
}
}
}
2023-11-29 19:22:32 +01:00
let playersToAdd = Net.getPlayersToAdd();
if (playersToAdd.length!=0)
console.log("added ",playersToAdd.length);
players.concat(playersToAdd);
let playerToRemove = Net.getPlayersToRemove();
for(let id in playerToRemove)
{
console.log("deleting ",id);
for(let i = 0;i<players.length;i++)
{
if (players[i].id==id)
{
players.splice(i,1);
break;
}
}
}
2023-11-29 19:03:50 +01:00
player.update([],[], dt);
2023-11-29 17:25:40 +01:00
for (var i = players.length - 1; i >= 0; i--) {
2023-11-29 19:12:36 +01:00
players[i].update([],[], dt);//squares, circles
2023-11-29 17:25:40 +01:00
}
//console.log(Inp.calculateAngle(500,500))
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)