game: core: recreate classes see other paddle

This commit is contained in:
2024-01-17 19:06:36 +01:00
parent 6983983e58
commit 1af55795d9
13 changed files with 295 additions and 191 deletions

View File

@ -56,6 +56,7 @@ class Game
this.walls.forEach(wall => {
wall.draw(ctx);
});
console.log(this.players)
this.players.forEach(player => {
player.draw(ctx);
});
@ -82,7 +83,7 @@ class Game
}, 500);
}
_send_paddle(position, time)
_send_paddle_position(position, time)
{
if (this.last_pos !== null && this.last_pos.time >= time)
return;
@ -92,9 +93,38 @@ class Game
this._send({"detail": "update_my_paddle_pos", ...this.last_pos});
}
_update_paddles(data)
_receive_player_join(player_data)
{
let index = this.players.indexOf((player) => player.id === player_data.user_id);
if (index !== -1)
this.players.slice(index, 1);
this.players.push(new Player(player_data.user_id,
this,
player_data.rail_start_x,
player_data.rail_start_y,
player_data.rail_stop_x,
player_data.rail_stop_y,
player_data.nb_goal,
player_data.position.position
));
}
_receive_player_leave(player_data)
{
const user_id = player_data.user_id;
const index = this.players.find(player => player.id === user_id);
if (index === -1)
return
this.players.slice(index, 1);
}
_receive_paddle_position(data)
{
console.log(data)
data.players.forEach((player_data) => {
let player = this.players.find((player) => player.id === player_data.id);
if (player === null)
@ -103,14 +133,16 @@ class Game
})
}
_update(data)
_receive(data)
{
if (data.detail === "update_paddles")
this._update_paddles(data);
if (data.detail === "paddle_position")
this._receive_paddle_position(data);
else if (data.detail === "update_ball")
this._update_ball(data);
else if (data.detail === "init_game")
this._init_game(data)
else if (data.detail === "player_join")
this._receive_player_join(data)
}
_init_game(data)
@ -130,15 +162,7 @@ class Game
this.players = []
const players_data = data.players;
players_data.forEach((player_data) => {
this.players.push(new Player(player_data.user_id,
this,
player_data.rail_start_x,
player_data.rail_start_y,
player_data.rail_stop_x,
player_data.rail_stop_y,
player_data.nb_goal,
player_data.position
));
this._receive_player_join(player_data)
});
this._inited = true;
@ -164,7 +188,7 @@ class Game
this._socket.onmessage = (event) => {
const data = JSON.parse(event.data);
this._update(data);
this._receive(data);
};
return this.wait_init();

View File

@ -25,7 +25,7 @@ class MyPlayer extends Player
this.positon = new_pos;
this.game._send_paddle(this.positon, this.game.time._current_frame);
this.game._send_paddle_position(this.positon, this.game.time._current_frame);
}
update_pos(new_position, time)