fix: game: 2d work

This commit is contained in:
starnakin 2024-04-10 15:40:54 +00:00
parent 8b9c4d7c1c
commit 0b7e72d8e2
10 changed files with 41 additions and 39 deletions

View File

@ -97,15 +97,9 @@ export class AGame extends AExchangeable
*/ */
send(data) send(data)
{ {
console.log(this._socket)
if (this._socket === undefined || this._socket.readyState !== WebSocket.OPEN) if (this._socket === undefined || this._socket.readyState !== WebSocket.OPEN)
{ return;
console.log("bozo pas confirme") this._socket.send(data);
return ;
}
console.log("j'essaye de send")
this._socket.send(data);
console.log("j'ai passer le send")
} }
async join() async join()

View File

@ -2,6 +2,7 @@ import { AExchangeable } from "../../AExchangable.js";
import { PongGame } from "./PongGame.js"; import { PongGame } from "./PongGame.js";
import { renderCube} from "../../../3D/cube.js" import { renderCube} from "../../../3D/cube.js"
import { Position } from "./Position.js"; import { Position } from "./Position.js";
import { Point } from "./Point.js";
export class PongBall extends AExchangeable export class PongBall extends AExchangeable
{ {
@ -13,7 +14,7 @@ export class PongBall extends AExchangeable
* @param {Number} speed * @param {Number} speed
* @param {Number} size * @param {Number} size
*/ */
constructor(game, size, position = new Position(), angle, speed) constructor(game, size, position = new Position(new Point(game.config.CENTER_X, game.config.CENTER_Y), 0), angle, speed)
{ {
super(); super();

View File

@ -79,9 +79,9 @@ export class PongGame extends AGame
let response_data = await response.json(); let response_data = await response.json();
console.log(response_data.players[0])
response_data.players.forEach((player_data) => { response_data.players.forEach((player_data) => {
this.players.push(new PongPlayer(this.client, this)); let player = new PongPlayer(this.client, this)
this.players.push(player);
}); });
this.import(response_data); this.import(response_data);

View File

@ -13,7 +13,7 @@ export class MyPlayer extends PongPlayer
* @param {[Number]} score * @param {[Number]} score
* @param {Position} position * @param {Position} position
*/ */
constructor(client, game, score, rail, position = new Position()) constructor(client, game, score, rail, position = new Position(0.5))
{ {
super(client, game, client.me.id, client.me.username, client.me.avatar, score, rail, position, true); super(client, game, client.me.id, client.me.username, client.me.avatar, score, rail, position, true);
/** /**
@ -62,22 +62,22 @@ export class MyPlayer extends PongPlayer
*/ */
updatePaddle(keys_pressed) updatePaddle(keys_pressed)
{ {
let new_pos = this.position; let new_location = this.position.location;
keys_pressed.forEach(key => { keys_pressed.forEach(key => {
if (this.downKeys.includes(key)) if (this.downKeys.includes(key))
new_pos += this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond(); new_location += this.game.config.PADDLE_SPEED_PER_SECOND_MAX * this.game.time.deltaTimeSecond();
if (this.upKeys.includes(key)) if (this.upKeys.includes(key))
new_pos -= this.game.config.paddle_speed_per_second_max * this.game.time.deltaTimeSecond(); new_location -= this.game.config.PADDLE_SPEED_PER_SECOND_MAX * this.game.time.deltaTimeSecond();
}); });
new_pos = Math.max(0 + this.game.config.paddle_ratio / 2, new_pos); new_location = Math.max(0 + this.game.config.PADDLE_RATIO / 2, new_location);
new_pos = Math.min(1 - this.game.config.paddle_ratio / 2, new_pos); new_location = Math.min(1 - this.game.config.PADDLE_RATIO / 2, new_location);
if (this.position === new_pos) if (this.position.location === new_location)
return; return;
this.position = new_pos; this.position.location = new_location;
this._sendPaddlePosition(); this._sendPaddlePosition();
} }

View File

@ -18,7 +18,7 @@ export class PongPlayer extends APlayer
* @param {String} avatar * @param {String} avatar
* @param {Client} client * @param {Client} client
*/ */
constructor(client, game, id, username, avatar, score = [], rail = new Segment(game), position = new Position(), isConnected) constructor(client, game, id, username, avatar, score = [], rail = new Segment(game), position = new Position(0.5), isConnected)
{ {
super(client, game, id, username, avatar, isConnected) super(client, game, id, username, avatar, isConnected)
@ -47,7 +47,7 @@ export class PongPlayer extends APlayer
* *
* @param {Number} new_position * @param {Number} new_position
*/ */
updatePos(new_position, time) updatePos(new_position)
{ {
this.position = new_position; this.position = new_position;
} }

View File

@ -7,7 +7,7 @@ export class Position extends AExchangeable
* @param {Point | Number} location * @param {Point | Number} location
* @param {Number} time * @param {Number} time
*/ */
constructor(location, time) constructor(location = new Point(), time)
{ {
super(); super();
/** /**

View File

@ -20,7 +20,6 @@ class Point:
return dist((point.x, point.y), (self.x, self.y)) return dist((point.x, point.y), (self.x, self.y))
def copy(self): def copy(self):
return Point(self.x, self.y) return Point(self.x, self.y)
def to_dict(self) -> dict: def to_dict(self) -> dict:

View File

@ -47,12 +47,16 @@ class PongPlayer(APlayer):
def update_position(self, data: dict): def update_position(self, data: dict):
new_position: Position = Position() new_position: Position = Position(None)
new_position.location = data.get("position") position_dict = data.get("position")
if (position_dict is None):
self.send_error("missing position")
return
if (new_position.position is None): new_position.location = position_dict.get("location")
self.send_error("missing new_position") if (new_position.location is None):
self.send_error("missing location")
return return
new_position.time = data.get("time") new_position.time = data.get("time")
@ -65,9 +69,9 @@ class PongPlayer(APlayer):
self.game_member.send_error("time error") self.game_member.send_error("time error")
return return
distance: float = abs(self.position.position - new_position.position) distance: float = abs(self.position.location - new_position.location)
sign: int = 1 if self.position.position >= new_position.position else -1 sign: int = 1 if self.position.location >= new_position.location else -1
time_difference: float = (new_position.time - self.position.time) / 1000 time_difference: float = (new_position.time - self.position.time) / 1000
@ -76,19 +80,19 @@ class PongPlayer(APlayer):
new_position_verified: Position = new_position.copy() new_position_verified: Position = new_position.copy()
if (distance > max_distance): if (distance > max_distance):
new_position_verified.position = self.position.position + max_distance * sign new_position_verified.location = self.position.location + max_distance * sign
if (not config.PADDLE_POSITION_MIN <= new_position_verified.position <= config.PADDLE_POSITION_MAX): if (not config.PADDLE_POSITION_MIN <= new_position_verified.location <= config.PADDLE_POSITION_MAX):
new_position_verified.position = max(new_position_verified.position, config.PADDLE_POSITION_MIN) new_position_verified.location = max(new_position_verified.location, config.PADDLE_POSITION_MIN)
new_position_verified.position = min(new_position_verified.position, config.PADDLE_POSITION_MAX) new_position_verified.location = min(new_position_verified.location, config.PADDLE_POSITION_MAX)
invalid_pos: bool = new_position.position != new_position_verified.position invalid_pos: bool = new_position.location != new_position_verified.location
if (new_position != self.position): if (new_position.location != self.position.location):
self.game.update_player(self) self.game.update_player(self)
self.position = new_position self.position.location = new_position.location
if (invalid_pos): if (invalid_pos):
self.send("update_player", self.to_dict()) self.send("update_player", self.to_dict())

View File

@ -8,7 +8,10 @@ class Position:
self.location: float = location self.location: float = location
def copy(self): def copy(self):
return Position(self.location, self.time) try:
return Position(self.location.copy(), self.time)
except:
return Position(self.location, self.time)
def to_dict(self): def to_dict(self):

View File

@ -253,7 +253,7 @@ async def update_ball(game: PongGame, impact_data: dict) -> None:
await asyncio.sleep(0.1) # delay to create frontend animation await asyncio.sleep(0.1) # delay to create frontend animation
game.ball.reset() game.ball.reset()
else: else:
game.ball.position = impact_data.get("impact") game.ball.position.location = impact_data.get("impact")
await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict()) await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict())
@ -288,8 +288,9 @@ async def render(game: PongGame):
routine_ball.cancel() routine_ball.cancel()
routine_players.cancel() routine_players.cancel()
return return
await asyncio.sleep(0.3) await asyncio.sleep(0.05)
def routine(game: PongGame): def routine(game: PongGame):
asyncio.run(render(game)) asyncio.run(render(game))