59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
from engine import Engine
|
|
from entity import Entity
|
|
|
|
class Action:
|
|
def apply(self, engine: Engine, entity: Entity) -> None:
|
|
raise NotImplementedError()
|
|
|
|
class QuitAction(Action):
|
|
def apply(self, engine: Engine, entity: Entity) -> None:
|
|
raise SystemExit()
|
|
|
|
|
|
class DirectionAction(Action):
|
|
def __init__(self, xdir: int, ydir: int):
|
|
super().__init__()
|
|
self.xdir = xdir
|
|
self.ydir = ydir
|
|
|
|
def apply(self, engine: Engine, entity: Entity) -> None:
|
|
raise NotImplementedError()
|
|
|
|
|
|
class MovementAction(DirectionAction):
|
|
def apply(self, engine: Engine, entity: Entity) -> None:
|
|
dest_x = entity.x + self.xdir
|
|
dest_y = entity.y + self.ydir
|
|
|
|
#bounds and collision checks
|
|
if not engine.floor_map.in_bounds(dest_x, dest_y):
|
|
return
|
|
if not engine.floor_map.tiles["walkable"][dest_x, dest_y]:
|
|
return
|
|
if engine.floor_map.get_entity_at(dest_x, dest_y, unwalkable_only=True) is not None:
|
|
return
|
|
|
|
entity.set_pos(dest_x, dest_y)
|
|
|
|
class MeleeAction(DirectionAction):
|
|
def apply(self, engine: Engine, entity: Entity) -> None:
|
|
dest_x = entity.x + self.xdir
|
|
dest_y = entity.y + self.ydir
|
|
|
|
target = engine.floor_map.get_entity_at(dest_x, dest_y)
|
|
|
|
if not target:
|
|
return
|
|
|
|
print(f"You kicked the {target.name}, which did nothing")
|
|
|
|
class MoveAction(DirectionAction):
|
|
def apply(self, engine: Engine, entity: Entity) -> None:
|
|
dest_x = entity.x + self.xdir
|
|
dest_y = entity.y + self.ydir
|
|
|
|
if engine.floor_map.get_entity_at(dest_x, dest_y):
|
|
return MeleeAction(self.xdir, self.ydir).apply(engine, entity)
|
|
else:
|
|
return MovementAction(self.xdir, self.ydir).apply(engine, entity)
|