game: online: recoded but collision not work
This commit is contained in:
@ -6,7 +6,6 @@ import { Client } from "../../Client.js";
|
||||
import { PongBall } from "./PongBall.js";
|
||||
import { sleep } from "../../../utils/sleep.js";
|
||||
import { Wall } from "./Wall.js"
|
||||
import { Point } from "./Point.js";
|
||||
import { Position } from "./Position.js";
|
||||
|
||||
export class PongGame extends AGame
|
||||
@ -16,9 +15,10 @@ export class PongGame extends AGame
|
||||
* @param {CallableFunction} goal_handler
|
||||
* @param {CallableFunction} finish_handler
|
||||
* @param {CallableFunction} disconnect_handler
|
||||
* @param {CallableFunction} startHandler
|
||||
* @param {Number} id
|
||||
*/
|
||||
constructor(client, id, disconnectHandler, goalHandler, finishHandler)
|
||||
constructor(client, id, disconnectHandler, goalHandler, startHandler, finishHandler)
|
||||
{
|
||||
super(client, id, undefined, disconnectHandler, "pong");
|
||||
|
||||
@ -28,6 +28,11 @@ export class PongGame extends AGame
|
||||
* @type {CallableFunction}
|
||||
*/
|
||||
this._goalHandler = goalHandler;
|
||||
|
||||
/**
|
||||
* @type {CallableFunction}
|
||||
*/
|
||||
this._startHandler = startHandler;
|
||||
|
||||
/**
|
||||
* @type {CallableFunction}
|
||||
@ -39,7 +44,6 @@ export class PongGame extends AGame
|
||||
*/
|
||||
this.time;
|
||||
|
||||
|
||||
/**
|
||||
* @type {Boolean}
|
||||
*/
|
||||
@ -53,7 +57,7 @@ export class PongGame extends AGame
|
||||
/**
|
||||
* @type {Ball}
|
||||
*/
|
||||
this.ball = new PongBall(this, undefined, new Position());
|
||||
this.ball = new PongBall(this, undefined, new Position(), 0, 0);
|
||||
|
||||
/**
|
||||
* @type {[Wall]}
|
||||
@ -122,18 +126,14 @@ export class PongGame extends AGame
|
||||
*/
|
||||
render(ctx)
|
||||
{
|
||||
if(ctx instanceof CanvasRenderingContext2D)
|
||||
ctx.clearRect(0, 0, this.config.MAP_SIZE_Y, this.config.MAP_SIZE_Y);
|
||||
ctx.clearRect(0, 0, this.config.MAP_SIZE_Y, this.config.MAP_SIZE_Y);
|
||||
|
||||
this.drawSides(ctx);
|
||||
this.ball.render(ctx);
|
||||
|
||||
if(ctx instanceof CanvasRenderingContext2D)
|
||||
{
|
||||
ctx.strokeStyle = "#000000";
|
||||
ctx.lineWidth = this.config.STROKE_THICKNESS;
|
||||
ctx.stroke();
|
||||
}
|
||||
ctx.strokeStyle = "#000000";
|
||||
ctx.lineWidth = this.config.STROKE_THICKNESS;
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -149,6 +149,8 @@ export class PongGame extends AGame
|
||||
*/
|
||||
async _receive(data)
|
||||
{
|
||||
console.log(data)
|
||||
|
||||
if (this._inited === false && data.detail === "init_game")
|
||||
{
|
||||
this._initGame(data);
|
||||
@ -162,7 +164,23 @@ export class PongGame extends AGame
|
||||
else if (data.detail === "goal")
|
||||
await this._receiveGoal(data);
|
||||
else if (data.detail === "finish")
|
||||
await this._finishHandler(data);
|
||||
await this._receiveFinish(data);
|
||||
else if (data.detail === "start")
|
||||
await this._receiveStart();
|
||||
}
|
||||
|
||||
async _receiveFinish(data)
|
||||
{
|
||||
const winner = this.players.find(player => player.id === data.winner_id)
|
||||
this.finished = true;
|
||||
await this._finishHandler(winner);
|
||||
}
|
||||
|
||||
|
||||
async _receiveStart()
|
||||
{
|
||||
this.started = true;
|
||||
await this._startHandler();
|
||||
}
|
||||
|
||||
async _receiveGoal(data)
|
||||
@ -176,6 +194,7 @@ export class PongGame extends AGame
|
||||
}
|
||||
|
||||
player.score.push(data.timestamp)
|
||||
console.log(player)
|
||||
|
||||
await this._goalHandler(player);
|
||||
}
|
||||
@ -192,26 +211,6 @@ export class PongGame extends AGame
|
||||
this.ball.import(data);
|
||||
}
|
||||
|
||||
async _updateGoal(data)
|
||||
{
|
||||
/**
|
||||
* @type { Player }
|
||||
*/
|
||||
let player_found;
|
||||
|
||||
this.players.forEach(player => {
|
||||
if (data.player_id === player.id)
|
||||
{
|
||||
player_found = player;
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
player_found.score.push(data.timestamp);
|
||||
|
||||
await this._goalHandler(player_found);
|
||||
}
|
||||
|
||||
_initGame(data)
|
||||
{
|
||||
data.walls.forEach((wall_data) => {
|
||||
|
Reference in New Issue
Block a user