Fix up all the runtime errors caused by the previous refactoring

This commit is contained in:
Eryn Wells 2022-05-01 09:51:22 -07:00
parent f1b95a697e
commit a54828c7fb
4 changed files with 40 additions and 20 deletions

View file

@ -2,26 +2,27 @@
# Eryn Wells <eryn@erynwells.me>
import logging
from .engine import Engine
from .geometry import Vector
from .object import Entity
LOG = logging.getLogger('events')
class Action:
def perform(self, engine: Engine, entity: Entity) -> None:
def perform(self, engine: 'Engine', entity: 'Entity') -> None:
'''
Perform this action. This is an abstract method that all subclasses
should implement.
'''
raise NotImplementedError()
def __repr__(self):
return f'{self.__class__.__name__}()'
class ExitAction(Action):
def perform(self, engine: Engine, entity: Entity) -> None:
def perform(self, engine: 'Engine', entity: 'Entity') -> None:
raise SystemExit()
class RegenerateRoomsAction(Action):
def perform(self, engine: Engine, entity: Entity) -> None:
def perform(self, engine: 'Engine', entity: 'Entity') -> None:
...
class MovePlayerAction(Action):
@ -38,13 +39,13 @@ class MovePlayerAction(Action):
def __init__(self, direction: Direction):
self.direction = direction
def perform(self, engine: Engine, entity: Entity) -> None:
def perform(self, engine: 'Engine', entity: 'Entity') -> None:
new_player_position = entity.position + self.direction
position_is_in_bounds = self.map.tile_is_in_bounds(new_player_position)
position_is_walkable = self.map.tile_is_walkable(new_player_position)
overlaps_an_entity = any(new_player_position.x == obj.x and new_player_position.y == obj.y for obj in engine.entities)
position_is_in_bounds = engine.map.tile_is_in_bounds(new_player_position)
position_is_walkable = engine.map.tile_is_walkable(new_player_position)
overlaps_another_entity = any(new_player_position == ent.position for ent in engine.entities if ent is not entity)
LOG.debug(f'Attempting to move player to {new_player_position} (in_bounds:{position_is_in_bounds} walkable:{position_is_walkable} overlaps:{overlaps_an_entity})')
if position_is_in_bounds and position_is_walkable and not overlaps_an_entity:
self.player.move_to(new_player_position)
LOG.debug(f'Attempting to move player to {new_player_position} (in_bounds:{position_is_in_bounds} walkable:{position_is_walkable} overlaps:{overlaps_another_entity})')
if position_is_in_bounds and position_is_walkable and not overlaps_another_entity:
entity.position = new_player_position

View file

@ -30,10 +30,11 @@ class Engine:
first_room = self.map.rooms[0]
player_start_position = first_room.midpoint
self.player = Entity('@', tcod.white, x=player_start_position.x, y=player_start_position.y)
self.player = Entity('@', position=player_start_position, fg=tcod.white)
self.entities: AbstractSet[Entity] = {self.player}
for _ in range(self.rng.randint(1, 15)):
self.entities.add(Entity('@', color=tcod.yellow, x=self.rng.randint(0, map_size.width), y=self.rng.randint(0, map_size.height)))
position = Point(self.rng.randint(0, map_size.width), self.rng.randint(0, map_size.height))
self.entities.add(Entity('@', position=position, fg=tcod.yellow))
def handle_event(self, event: tcod.event.Event):
action = self.event_handler.dispatch(event)

View file

@ -11,15 +11,23 @@ class Point:
self.y = y
@overload
def __add__(self, other: Vector) -> Point:
def __add__(self, other: 'Vector') -> 'Point':
...
@overload
def __add__(self, other) -> Point:
def __add__(self, other) -> 'Point':
if not isinstance(other, Vector):
raise TypeError('Only Vector can be added to a Point')
return Point(self.x + other.dx, self.y + other.dy)
@overload
def __eq__(self, other: 'Point') -> bool:
...
def __eq__(self, other):
if not isinstance(other, Point):
raise TypeError('Points can only be compared to other Points')
return self.x == other.x and self.y == other.y
def __str__(self):
return f'(x:{self.x}, y:{self.y})'

View file

@ -8,10 +8,20 @@ from typing import Optional
class Entity:
'''A single-tile drawable entity with a symbol and position.'''
def __init__(self, symbol: str, *, position: Optional[Point] = None, color: Optional[tcod.Color] = None):
def __init__(self, symbol: str, *,
position: Optional[Point] = None,
fg: Optional[tcod.Color] = None,
bg: Optional[tcod.Color] = None):
self.position = position if position else Point()
self.color = color if color else tcod.white
self.foreground = fg if fg else tcod.white
self.background = bg
self.symbol = symbol
def print_to_console(self, console: tcod.Console) -> None:
console.print(x=self.__x, y=self.__y, string=self.__symbol, fg=self.__color)
console.print(x=self.position.x, y=self.position.y, string=self.symbol, fg=self.foreground, bg=self.background)
def __str__(self):
return f'{self.symbol}[{self.position}]'
def __repr__(self):
return f'{self.__class__.__name__}({self.symbol}, position={self.position}, fg={self.foreground}, bg={self.background})'