42_ft_transcendence/frontend/static/js/api/game/Player.js

45 lines
1.4 KiB
JavaScript

class Player
{
constructor(id, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon)
{
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)
{
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 }