game: add: class: point and segment, add: type docstring
This commit is contained in:
@ -1,52 +1,153 @@
|
||||
import { Game } from "./Game.js";
|
||||
import { Point } from "./Point.js";
|
||||
import { Segment } from "./Segment.js";
|
||||
|
||||
class Player
|
||||
{
|
||||
constructor(id, game, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y, nb_goal, positon, is_connected)
|
||||
/**
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
this.is_connected = is_connected;
|
||||
this.id = id;
|
||||
this.positon = positon;
|
||||
this.nb_goal = nb_goal;
|
||||
/**
|
||||
* @type {Game}
|
||||
*/
|
||||
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;
|
||||
/**
|
||||
* @type {Boolean}
|
||||
*/
|
||||
this.is_connected = is_connected;
|
||||
|
||||
this.rail_size = Math.abs(this.rail_stop_x - this.rail_start_x) + Math.abs(this.rail_stop_y - this.rail_start_y)
|
||||
|
||||
/**
|
||||
* @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 {Number}
|
||||
*/
|
||||
this.rail_size = Math.abs(this.rail.stop.x - this.rail.start.x) + Math.abs(this.rail.stop.y - this.rail.start.y);
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.paddle_size = this.rail_size * this.game.config.paddle_ratio;
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.diff_x = this.rail.stop.x - this.rail.start.x;
|
||||
|
||||
this.diff_x = this.rail_stop_x - this.rail_start_x,
|
||||
this.diff_y = this.rail_stop_y - this.rail_start_y;
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.diff_y = this.rail.stop.y - this.rail.start.y;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Number} new_position
|
||||
*/
|
||||
update_pos(new_position, time)
|
||||
{
|
||||
this.positon = new_position;
|
||||
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);
|
||||
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 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));
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.rail_size = Math.abs(this.rail.stop.x - this.rail.start.x) + Math.abs(this.rail.stop.y - this.rail.start.y);
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.paddle_size = this.rail_size * this.game.config.paddle_ratio;
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.diff_x = this.rail.stop.x - this.rail.start.x;
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.diff_y = this.rail.stop.y - this.rail.start.y;
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
export { Player }
|
Reference in New Issue
Block a user