42_ft_transcendence/frontend/static/js/api/game/Player.js
2024-03-31 10:59:39 +02:00

112 lines
2.8 KiB
JavaScript

import { Game } from "./Game.js";
import { Point } from "./Point.js";
import { Segment } from "./Segment.js";
import { renderCube } from "../../3D/cube.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;
}
/**
*
* @param {Number} new_position
*/
update_pos(new_position, time)
{
this.position = new_position;
}
/**
* @param {CanvasRenderingContext2D} ctx
*/
draw(ctx)
{
if (this.is_connected === false)
{
if(ctx instanceof CanvasRenderingContext2D)
{
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);
console.log(paddle)
paddle.draw(ctx);
}
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 }