42_ft_transcendence/games/objects/Game.py

53 lines
1.5 KiB
Python

from .Ball import Ball
from .Player import Player
from .Wall import Wall
import math
from .. import config
from ..models import GameModel
class Game:
def __init__(self, game_id: int) -> None:
self.ball: Ball = Ball()
game: GameModel = GameModel.objects.get(pk = game_id)
players_id: [int] = game.get_players_id()
radius: float = min(config.MAP_SIZE_X, config.MAP_SIZE_Y) / 2 - 10
nb_sides = len(players_id) * 2
self.polygon = []
for i in range(nb_sides):
angle: float = (i * 2 * math.pi / nb_sides) + (math.pi * 3 / 4)
x: float = config.MAP_CENTER_X + radius * math.cos(angle)
y: float = config.MAP_CENTER_Y + radius * math.sin(angle)
self.polygon.append((x, y))
self.players: list(Player) = []
self.walls: list(Wall) = []
for i in range(nb_sides):
if (i % 2 == 0):
self.players.append(Player(players_id[ i // 2], *self.polygon[i], *self.polygon[(i + 1) % nb_sides]))
else:
self.walls.append(Wall(*self.polygon[i], *self.polygon[(i + 1) % nb_sides]))
self.game_id: int = game_id
def to_dict(self):
data: dict = {"ball": self.ball.to_dict(),
"players": [player.to_dict() for player in self.players],
"walls": [wall.to_dict() for wall in self.walls],
}
print(data)
return data