111 lines
2.5 KiB
JavaScript
111 lines
2.5 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
|
|
* @param {String} username
|
|
*/
|
|
constructor(game, id, username, 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;
|
|
|
|
/**
|
|
* @type {String}
|
|
*/
|
|
this.username = username;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {Number} new_position
|
|
*/
|
|
update_pos(new_position, time)
|
|
{
|
|
this.position = new_position;
|
|
}
|
|
|
|
/**
|
|
* @param {CanvasRenderingContext2D} ctx
|
|
*/
|
|
draw(ctx)
|
|
{
|
|
if(ctx instanceof CanvasRenderingContext2D)
|
|
{
|
|
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 = new Point(this.rail.start.x + this.diff_x * this.position,
|
|
this.rail.start.y + this.diff_y * this.position);
|
|
|
|
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);
|
|
}
|
|
else if(ctx instanceof WebGLRenderingContext)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
alert('Unknown rendering context type (wtf)');
|
|
}
|
|
}
|
|
|
|
from_json(data)
|
|
{
|
|
this.is_connected = data.is_connected;
|
|
this.id = data.user_id;
|
|
this.position = data.position.position ;
|
|
this.nb_goal = data.nb_goal;
|
|
this.rail = this.rail.from_json(data.rail);
|
|
this.username = data.username;
|
|
|
|
return this;
|
|
}
|
|
}
|
|
|
|
export { Player }
|