Tweak frontend and game logic
This commit is contained in:
@ -69,14 +69,8 @@ class GameEngine:
|
||||
|
||||
return player
|
||||
|
||||
async def move_player(
|
||||
self, player: Player, direction: Direction
|
||||
) -> PlayerMoveResult:
|
||||
player.reset_timeout()
|
||||
|
||||
new_position = Position(player.position.x, player.position.y)
|
||||
logging.info(f"Player {player} move to {direction}")
|
||||
|
||||
def _move_position(self, position: Position, direction: Direction) -> Position:
|
||||
new_position = Position(position.x, position.y)
|
||||
if direction == Direction.LEFT:
|
||||
new_position.x -= 1
|
||||
elif direction == Direction.RIGHT:
|
||||
@ -87,39 +81,56 @@ class GameEngine:
|
||||
new_position.y += 1
|
||||
else:
|
||||
raise ValueError(f"Unhandled direction: {direction}")
|
||||
return new_position
|
||||
|
||||
async def move_player(
|
||||
self, player: Player, direction: Direction
|
||||
) -> PlayerMoveResult:
|
||||
player.reset_timeout()
|
||||
|
||||
# player will not be able to move once they reach the destination
|
||||
if player.reached_destination:
|
||||
return PlayerMoveResult.DESTINATION_REACHED
|
||||
|
||||
logging.info(f"Player {player} move to {direction}")
|
||||
new_position = self._move_position(player.position, direction)
|
||||
|
||||
player.move_attempt_count += 1
|
||||
|
||||
if not self.position_in_board_bounds(new_position):
|
||||
if not self._position_in_board_bounds(new_position):
|
||||
raise PositionOutOfBounds()
|
||||
|
||||
if self.colided_with_obstacle(new_position):
|
||||
if self._colided_with_obstacle(new_position):
|
||||
raise Collision()
|
||||
|
||||
player.position = new_position
|
||||
player.move_count += 1
|
||||
|
||||
if self._is_player_on_destination(player):
|
||||
player.reached_destination = True
|
||||
logging.info(f"Player {player} reached destination!")
|
||||
|
||||
if self.ws_server:
|
||||
await self.ws_server.send_game_state()
|
||||
|
||||
if self.is_player_on_destination(player):
|
||||
logging.info(f"Player {player} reached destination!")
|
||||
return PlayerMoveResult.DESTINATION_REACHED
|
||||
|
||||
self.__debug_print_board()
|
||||
|
||||
if player.reached_destination:
|
||||
return PlayerMoveResult.DESTINATION_REACHED
|
||||
|
||||
await asyncio.sleep(settings.game.MOVE_DELAY)
|
||||
|
||||
return PlayerMoveResult.OK
|
||||
|
||||
def is_player_on_destination(self, player: Player) -> bool:
|
||||
def _is_player_on_destination(self, player: Player) -> bool:
|
||||
return player.position == self.board.destination.position
|
||||
|
||||
def position_in_board_bounds(self, position: Position) -> bool:
|
||||
def _position_in_board_bounds(self, position: Position) -> bool:
|
||||
return (
|
||||
0 <= position.x < self.board.width and 0 <= position.y < self.board.height
|
||||
)
|
||||
|
||||
def colided_with_obstacle(self, position: Position) -> bool:
|
||||
def _colided_with_obstacle(self, position: Position) -> bool:
|
||||
return self.board.get_object_at_position(position) is not None
|
||||
|
||||
def get_board_layout(self) -> BoardLayout:
|
||||
|
||||
@ -22,6 +22,7 @@ class Player:
|
||||
)
|
||||
active: bool = True
|
||||
can_be_deactivated: bool = True
|
||||
reached_destination: bool = False
|
||||
|
||||
def reset_timeout(self) -> None:
|
||||
self.last_seen = datetime.datetime.now()
|
||||
|
||||
@ -15,9 +15,12 @@ class LayerDto(BaseModel):
|
||||
name: str
|
||||
objects: list[LayerObjectDto]
|
||||
|
||||
class GameStatePlayerDto(PlayerDto):
|
||||
reached_destination: bool
|
||||
|
||||
|
||||
class GameStateDto(BaseModel):
|
||||
board: BoardDto
|
||||
destination: DestinationDto
|
||||
players: list[PlayerDto]
|
||||
players: list[GameStatePlayerDto]
|
||||
layers: list[LayerDto]
|
||||
|
||||
Reference in New Issue
Block a user