42_ft_transcendence/frontend/static/js/api/game/Player.js
starnakin 7cf13640a1 game: core: change player to player.isconnected
befort: if a player is disconnected he doesn't have an object
after: he have un object with a socket == None
2024-01-19 15:38:04 +01:00

52 lines
1.7 KiB
JavaScript

class Player
{
constructor(id, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon, is_connected)
{
this.is_connected = is_connected;
this.id = id;
this.positon = positon;
this.nb_goal = nb_goal;
this.game = game;
this.rail_start_x = rail_start_x;
this.rail_start_y = rail_start_y;
this.rail_stop_x = rail_stop_x;
this.rail_stop_y = rail_stop_y;
this.rail_size = Math.abs(this.rail_stop_x - this.rail_start_x) + Math.abs(this.rail_stop_y - this.rail_start_y)
this.paddle_size = this.rail_size * this.game.config.paddle_ratio;
this.diff_x = this.rail_stop_x - this.rail_start_x,
this.diff_y = this.rail_stop_y - this.rail_start_y;
}
update_pos(new_position, time)
{
this.positon = new_position;
}
draw(ctx)
{
if (this.is_connected === false)
{
ctx.moveTo(this.rail_start_x, this.rail_start_y);
ctx.lineTo(this.rail_stop_x, this.rail_stop_y);
return;
}
let paddle_pos_x = this.rail_start_x + this.diff_x * this.positon,
paddle_pos_y = this.rail_start_y + this.diff_y * this.positon;
let start_x = paddle_pos_x - (this.diff_x * (this.paddle_size / 2 / this.rail_size)),
start_y = paddle_pos_y - (this.diff_y * (this.paddle_size / 2 / this.rail_size)),
stop_x = paddle_pos_x + (this.diff_x * (this.paddle_size / 2 / this.rail_size)),
stop_y = paddle_pos_y + (this.diff_y * (this.paddle_size / 2 / this.rail_size));
ctx.moveTo(start_x, start_y);
ctx.lineTo(stop_x, stop_y);
}
}
export { Player }