game: add: class: point and segment, add: type docstring
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import { sleep } from "../../utils/sleep.js";
|
||||
import { Ball } from "./Ball.js";
|
||||
import { GameConfig } from "./GameConfig.js"
|
||||
import { MyPlayer } from "./MyPlayer.js";
|
||||
import { Player } from "./Player.js";
|
||||
import { Time } from "./Time.js";
|
||||
import { Wall } from "./Wall.js";
|
||||
@ -20,6 +19,10 @@ class Game
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {Number}
|
||||
*/
|
||||
async init()
|
||||
{
|
||||
let response = await this.client._get(`/api/games/${this.id}`);
|
||||
@ -35,7 +38,7 @@ class Game
|
||||
this.finished = response_data.finished;
|
||||
this.winner_id = this.finished ? response_data.winner_id : undefined;
|
||||
|
||||
if (this.finished === true || this.started === false)
|
||||
if (this.finished === true)
|
||||
return 0;
|
||||
|
||||
this.config = new GameConfig(this.client);
|
||||
@ -51,6 +54,10 @@ class Game
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {CanvasRenderingContext2D} ctx
|
||||
*/
|
||||
draw_sides(ctx)
|
||||
{
|
||||
this.walls.forEach(wall => {
|
||||
@ -61,6 +68,10 @@ class Game
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {CanvasRenderingContext2D} ctx
|
||||
*/
|
||||
draw(ctx)
|
||||
{
|
||||
ctx.clearRect(0, 0, this.config.size_x, this.config.size_y);
|
||||
@ -91,6 +102,7 @@ class Game
|
||||
|
||||
_receive_player_join(player_data)
|
||||
{
|
||||
console.log(player_data)
|
||||
let index = this.players.indexOf((player) => player.id === player_data.user_id);
|
||||
|
||||
this.players[index].is_connected = true;
|
||||
@ -126,7 +138,6 @@ class Game
|
||||
|
||||
_receive(data)
|
||||
{
|
||||
console.log(data)
|
||||
if (data.detail === "update_paddle")
|
||||
this._receive_update_paddle(data);
|
||||
else if (data.detail === "update_ball")
|
||||
@ -147,26 +158,13 @@ class Game
|
||||
this.walls = [];
|
||||
const walls_data = data.walls;
|
||||
walls_data.forEach((wall_data) => {
|
||||
this.walls.push(new Wall(wall_data.rail_start_x,
|
||||
wall_data.rail_start_y,
|
||||
wall_data.rail_stop_x,
|
||||
wall_data.rail_stop_y));
|
||||
this.walls.push(new Wall().from_json(wall_data));
|
||||
});
|
||||
|
||||
this.players = []
|
||||
const players_data = data.players;
|
||||
players_data.forEach((player_data) => {
|
||||
this.players.push(new Player(player_data.user_id,
|
||||
this,
|
||||
player_data.rail_start_x,
|
||||
player_data.rail_start_y,
|
||||
player_data.rail_stop_x,
|
||||
player_data.rail_stop_y,
|
||||
player_data.nb_goal,
|
||||
player_data.position.position,
|
||||
player_data.is_connected,
|
||||
));
|
||||
|
||||
this.players.push(new Player(this).from_json(player_data));
|
||||
});
|
||||
|
||||
this._inited = true;
|
||||
@ -180,7 +178,7 @@ class Game
|
||||
|
||||
async join()
|
||||
{
|
||||
if (this.started !== true || this.finished === true)
|
||||
if (this.finished === true)
|
||||
{
|
||||
console.error("The Game is not currently ongoing.");
|
||||
return;
|
||||
|
Reference in New Issue
Block a user