player pos is checked

This commit is contained in:
2024-01-16 17:47:31 +01:00
parent a1e732f167
commit 58c0df447c
10 changed files with 180 additions and 52 deletions

View File

@ -16,6 +16,7 @@ class Game
this.client = client;
this.id = id;
this.keys_pressed = [];
this.last_pos = null
}
async init()
@ -57,6 +58,7 @@ class Game
{
if (this._socket === undefined)
return;
const loop_id = setInterval(() => {
if (this._socket.readyState === WebSocket.OPEN)
{
@ -66,24 +68,25 @@ class Game
}, 500);
}
_send_paddle(my_player)
_send_paddle(position, time)
{
this._send({"detail": "update_my_paddle_pos", "position": my_player.pos});
if (this.last_pos !== null && this.last_pos.time >= time)
return;
this.last_pos = {"time": time, "position": position};
this._send({"detail": "update_my_paddle_pos", ...this.last_pos});
}
_update_paddles(data)
{
data.forEach(player_data => {
let player = this.players.find(player_data2 => player_data2.id === player_data)
console.log(data)
data.players.forEach((player_data) => {
let player = this.players.find((player) => player.id === player_data.id);
if (player === null)
{
console.error("error 1000: je ne comprends pas");
return;
}
player.pos = player_data.pos;
});
return
player.update_pos(player_data.position, player_data.time);
})
}
_update_ball(data)
@ -96,6 +99,7 @@ class Game
_update(data)
{
console.log("bozo2", data)
if (data.detail === "update_paddles")
this._update_paddles(data);
else if (data.detail === "update_ball")

View File

@ -15,15 +15,36 @@ class MyPlayer extends Player
let new_pos = this.pos;
if (keys_pressed.includes("s"))
new_pos -= this.game.config.paddle_speed_per_second_max * this.time.deltaTimeSecond();
new_pos -= this.game.config.paddle_speed_per_second_max * this.time.deltaTimeSecond() * 1;
if (keys_pressed.includes("w"))
new_pos += this.game.config.paddle_speed_per_second_max * this.time.deltaTimeSecond();
new_pos += this.game.config.paddle_speed_per_second_max * this.time.deltaTimeSecond() * 1;
new_pos = Math.max(0 + this.game.config.paddle_ratio / 2, new_pos);
new_pos = Math.min(1 - this.game.config.paddle_ratio / 2, new_pos);
if (this.pos === new_pos)
return;
console.log(this.client.me.id)
this.pos = new_pos;
this.game._send_paddle(this);
this.game._send_paddle(this.pos, this.time._current_frame);
}
update_pos(new_position, time)
{
let position_verified = new_position;
let time_diff = (Date.now() - time) / 1000;
let sign = this - new_position >= 0 ? 1 : -1;
let distance = Math.abs(this.pos - new_position);
let distance_max = time_diff * this.game.config.paddle_speed_per_second_max;
if (distance > distance_max)
position_verified = distance_max * sign;
this.pos = position_verified;
}
}

View File

@ -8,6 +8,11 @@ class Player
this.nb_goal = nb_goal;
this.game = game;
}
update_pos(new_position, time)
{
this.pos = new_position;
}
}
export { Player }

View File

@ -109,8 +109,9 @@ export default class extends AbstractView
if (this.my_player)
this.my_player.update_paddle(this.keys_pressed);
this.draw_game();
this.time.new_frame()
this.time.new_frame();
//clearInterval(loop_id);
// 1 sec fps
}, 1000 / 60);
}