game: add: scoreboard
This commit is contained in:
parent
e5ee0b34e5
commit
f2ce928fbb
@ -10,14 +10,24 @@ class Game
|
||||
{
|
||||
/**
|
||||
* @param {Client} client
|
||||
* @param {CallableFunction} update_goal
|
||||
*/
|
||||
constructor(client, id)
|
||||
constructor(client, id, update_goal)
|
||||
{
|
||||
/**
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = client;
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.id = id;
|
||||
|
||||
/**
|
||||
* @type {CallableFunction}
|
||||
*/
|
||||
this.update_goal = update_goal;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -153,6 +163,8 @@ class Game
|
||||
this._receive_ball(data);
|
||||
else if (data.detail === "init_game")
|
||||
this._init_game(data);
|
||||
else if (data.detail === "goal")
|
||||
this.update_goal(data);
|
||||
}
|
||||
|
||||
_init_game(data)
|
||||
|
@ -2,14 +2,14 @@ import { client } from "../index.js";
|
||||
import { Game } from "../api/game/Game.js";
|
||||
import AbstractView from "./abstracts/AbstractView.js";
|
||||
import { MyPlayer } from "../api/game/MyPlayer.js";
|
||||
import { lang } from "../index.js"
|
||||
import { lang } from "../index.js";
|
||||
|
||||
export default class extends AbstractView
|
||||
{
|
||||
constructor(params)
|
||||
{
|
||||
super(params, "Game");
|
||||
this.game = new Game(client, params.id);
|
||||
this.game = new Game(client, params.id, this.update_goal);
|
||||
this.keys_pressed = [];
|
||||
this.my_player = undefined;
|
||||
}
|
||||
@ -48,6 +48,14 @@ export default class extends AbstractView
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} data
|
||||
*/
|
||||
update_goal(data)
|
||||
{
|
||||
document.getElementById(`goal-${data.player}`).innerText = data.nb_goal;
|
||||
}
|
||||
|
||||
render()
|
||||
{
|
||||
let loop_id = setInterval(() => {
|
||||
@ -119,11 +127,14 @@ export default class extends AbstractView
|
||||
let players_list = document.getElementById("players_list");
|
||||
|
||||
this.game.players.forEach(player => {
|
||||
console.log(player);
|
||||
|
||||
let tr = document.createElement("tr");
|
||||
let name = document.createElement("td");
|
||||
let goal = document.createElement("td");
|
||||
|
||||
name.id = `username-${player.id}`;
|
||||
goal.id = `goal-${player.id}`;
|
||||
|
||||
name.innerText = player.username;
|
||||
goal.innerText = player.nb_goal;
|
||||
|
||||
|
@ -128,7 +128,6 @@ class Game(AbstractRoom):
|
||||
self._updated_players.append(player)
|
||||
|
||||
def _player_leave(self, player: Player):
|
||||
print(player.socket)
|
||||
self._update_player(player)
|
||||
|
||||
def _spectator_join(self, user_id: int, socket: WebsocketConsumer):
|
||||
@ -149,7 +148,6 @@ class Game(AbstractRoom):
|
||||
self._send_game_data(member)
|
||||
return member
|
||||
|
||||
|
||||
def start(self):
|
||||
if (self.started == True):
|
||||
return
|
||||
|
@ -174,7 +174,7 @@ def wall_collision(ball_angle: float, wall_angle: float) -> float:
|
||||
|
||||
return new_angle
|
||||
|
||||
def paddle_collision(ball: Ball, impact: Point, player: Player, inc_x: float, inc_y: float):
|
||||
async def paddle_collision(ball: Ball, impact: Point, player: Player, inc_x: float, inc_y: float):
|
||||
|
||||
diff_x: float = player.rail.stop.x - player.rail.start.x
|
||||
diff_y: float = player.rail.stop.y - player.rail.start.y
|
||||
@ -199,15 +199,14 @@ def paddle_collision(ball: Ball, impact: Point, player: Player, inc_x: float, in
|
||||
|
||||
hit_point: Point = Point(impact.x - inc_x, impact.y - inc_y)
|
||||
|
||||
print(hit_point, paddle)
|
||||
|
||||
if (not paddle.is_on(hit_point)):
|
||||
print(not paddle.is_on(hit_point))
|
||||
player.nb_goal += 1
|
||||
await SyncToAsync(player.game.broadcast)("goal", {"player": player.user_id, "nb_goal": player.nb_goal})
|
||||
return None
|
||||
|
||||
return ball.angle + math.pi
|
||||
|
||||
def collision(game: Game, impact_data: dict) -> bool:
|
||||
async def collision(game: Game, impact_data: dict) -> bool:
|
||||
|
||||
segment: Segment = impact_data.get("segment")
|
||||
|
||||
@ -227,7 +226,7 @@ def collision(game: Game, impact_data: dict) -> bool:
|
||||
angle = wall_collision(game.ball.angle, surface_angle)
|
||||
|
||||
else:
|
||||
angle = paddle_collision(game.ball, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y"))
|
||||
angle = await paddle_collision(game.ball, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y"))
|
||||
|
||||
if (angle is None):
|
||||
return False
|
||||
@ -243,11 +242,10 @@ async def update_ball(game: Game, impact_data: dict) -> None:
|
||||
|
||||
await asyncio.sleep(time_before_impact)
|
||||
|
||||
hit: bool = collision(game, impact_data)
|
||||
hit: bool = await collision(game, impact_data)
|
||||
|
||||
if (hit == False):
|
||||
await asyncio.sleep(0.1)
|
||||
print("Goal")
|
||||
game.ball.reset()
|
||||
else:
|
||||
game.ball.position = impact_data.get("impact")
|
||||
|
Loading…
Reference in New Issue
Block a user