game: core: use server game calulation form
This commit is contained in:
@ -1,7 +1,10 @@
|
||||
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";
|
||||
|
||||
class Game
|
||||
{
|
||||
@ -15,8 +18,6 @@ class Game
|
||||
*/
|
||||
this.client = client;
|
||||
this.id = id;
|
||||
this.keys_pressed = [];
|
||||
this.last_pos = null
|
||||
}
|
||||
|
||||
async init()
|
||||
@ -43,60 +44,33 @@ class Game
|
||||
if (ret !== 0)
|
||||
return ret;
|
||||
|
||||
this.players = [];
|
||||
response_data.players_id.forEach(player_id => {
|
||||
let player = new Player(player_id, 0.5, 0, this, 0, 0, 0, 0);
|
||||
this.players.push(player);
|
||||
});
|
||||
|
||||
this.ball = new Ball(this);
|
||||
this.time = new Time();
|
||||
this.last_pos = null
|
||||
this._inited = false;
|
||||
this._wall_drew = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
draw_wall(ctx, stop_x, stop_y)
|
||||
draw_sides(ctx)
|
||||
{
|
||||
ctx.lineTo(stop_x, stop_y);
|
||||
}
|
||||
|
||||
draw_sides(ctx, nb_sides)
|
||||
{
|
||||
let start_x,
|
||||
start_y,
|
||||
stop_x,
|
||||
stop_y;
|
||||
|
||||
let radius = Math.min(this.config.size_x, this.config.size_y) / 2 - 10;
|
||||
|
||||
for (let i = 0; i <= nb_sides; i++)
|
||||
if (this._wall_drew !== true)
|
||||
{
|
||||
let angle = (i * 2 * Math.PI / nb_sides) + (Math.PI * 3 / 4);
|
||||
|
||||
stop_x = this.config.center_x + radius * Math.cos(angle);
|
||||
stop_y = this.config.center_y + radius * Math.sin(angle);
|
||||
|
||||
if (i == 0)
|
||||
ctx.moveTo(stop_x, start_y);
|
||||
if (i % 2 == 0)
|
||||
this.draw_wall(ctx, stop_x, stop_y);
|
||||
else
|
||||
this.players[(i - 1) / 2].draw(ctx);
|
||||
start_x = stop_x;
|
||||
start_y = stop_y;
|
||||
this.walls.forEach(wall => {
|
||||
wall.draw(ctx);
|
||||
});
|
||||
}
|
||||
this.players.forEach(player => {
|
||||
player.draw(ctx);
|
||||
});
|
||||
this._wall_drew = true;
|
||||
}
|
||||
|
||||
draw(ctx)
|
||||
{
|
||||
ctx.beginPath()
|
||||
|
||||
ctx.clearRect(0, 0, this.config.size_x, this.config.size_y);
|
||||
this.draw_sides(ctx, (this.players_id.length) * 2);
|
||||
this.draw_sides(ctx);
|
||||
this.ball.draw(ctx);
|
||||
|
||||
ctx.strokeStyle = "#000000";
|
||||
ctx.lineWidth = 10;
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
_send(data)
|
||||
@ -134,24 +108,54 @@ class Game
|
||||
})
|
||||
}
|
||||
|
||||
_update_ball(data)
|
||||
{
|
||||
this.ball.position_x = data.position_x;
|
||||
this.ball.position_y = data.position_y;
|
||||
this.ball.velocity_x = data.velocity_x;
|
||||
this.ball.velocity_y = data.velocity_y;
|
||||
}
|
||||
|
||||
_update(data)
|
||||
{
|
||||
console.log("bozo2", data)
|
||||
if (data.detail === "update_paddles")
|
||||
this._update_paddles(data);
|
||||
else if (data.detail === "update_ball")
|
||||
this._update_ball(data);
|
||||
else if (data.detail === "init_game")
|
||||
this._init_game(data)
|
||||
}
|
||||
|
||||
join()
|
||||
_init_game(data)
|
||||
{
|
||||
const ball_data = data.ball;
|
||||
this.ball = new Ball(this, ball_data.position_x, ball_data.position_y, ball_data.velocity_x, ball_data.velocity_y);
|
||||
|
||||
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.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
|
||||
));
|
||||
});
|
||||
|
||||
this._inited = true;
|
||||
}
|
||||
|
||||
async wait_init()
|
||||
{
|
||||
while (this._inited !== true)
|
||||
await sleep(100);
|
||||
}
|
||||
|
||||
async join()
|
||||
{
|
||||
if (this.started !== true || this.finished === true)
|
||||
{
|
||||
@ -167,6 +171,8 @@ class Game
|
||||
const data = JSON.parse(event.data);
|
||||
this._update(data);
|
||||
};
|
||||
|
||||
return this.wait_init();
|
||||
}
|
||||
|
||||
leave()
|
||||
|
Reference in New Issue
Block a user