122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
import { Game } from "./Game.js";
|
|
import { Point } from "./Point.js";
|
|
import { Segment } from "./Segment.js";
|
|
|
|
class Player
|
|
{
|
|
/**
|
|
* @param {Number} id
|
|
* @param {Game} game
|
|
* @param {Segment} rail
|
|
* @param {Number} nb_goal
|
|
* @param {Number} position
|
|
* @param {Boolean} is_connected
|
|
*/
|
|
constructor(game, id, rail, nb_goal, position, is_connected)
|
|
{
|
|
/**
|
|
* @type {Game}
|
|
*/
|
|
this.game = game;
|
|
|
|
/**
|
|
* @type {Boolean}
|
|
*/
|
|
this.is_connected = is_connected;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.id = id;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.position = position;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.nb_goal = nb_goal;
|
|
|
|
/**
|
|
* @type {Segment}
|
|
*/
|
|
this.rail = rail === undefined ? new Segment() : rail;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {Number} new_position
|
|
*/
|
|
update_pos(new_position, time)
|
|
{
|
|
this.position = new_position;
|
|
}
|
|
|
|
/**
|
|
* @param {CanvasRenderingContext2D} ctx
|
|
*/
|
|
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 diff_x = this.rail.stop.x - this.rail.start.x,
|
|
diff_y = this.rail.stop.y - this.rail.start.y;
|
|
|
|
let rail_length = this.rail.len(),
|
|
paddle_length = rail_length * this.game.config.paddle_ratio;
|
|
|
|
let paddle_center = new Point(this.rail.start.x + diff_x * this.position,
|
|
this.rail.start.y + diff_y * this.position);
|
|
|
|
let paddle_start_x = paddle_center.x - (diff_x * (paddle_length / 2 / rail_length)),
|
|
paddle_start_y = paddle_center.y - (diff_y * (paddle_length / 2 / rail_length)),
|
|
paddle_stop_x = paddle_center.x + (diff_x * (paddle_length / 2 / rail_length)),
|
|
paddle_stop_y = paddle_center.y + (diff_y * (paddle_length / 2 / rail_length));
|
|
|
|
let paddle_start = new Point(paddle_start_x, paddle_start_y),
|
|
paddle_stop = new Point (paddle_stop_x, paddle_stop_y);
|
|
|
|
let paddle = new Segment(paddle_start, paddle_stop);
|
|
|
|
paddle.draw(ctx);
|
|
}
|
|
|
|
from_json(data)
|
|
{
|
|
/**
|
|
* @type {Boolean}
|
|
*/
|
|
|
|
this.is_connected = data.is_connected;
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.id = data.user_id;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.position = data.position.position ;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.nb_goal = data.nb_goal;
|
|
|
|
/**
|
|
* @type {Segment}
|
|
*/
|
|
this.rail = this.rail.from_json(data.rail);
|
|
|
|
return this
|
|
}
|
|
}
|
|
|
|
export { Player } |