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