2022-01-02 15:23:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from engine import Engine
|
|
|
|
from entity import Entity
|
|
|
|
|
|
|
|
|
|
|
|
class Action:
|
|
|
|
def perform(self, engine: Engine, entity: Entity) -> None:
|
|
|
|
"""Perform this action with the objects needed to determine its scope.
|
|
|
|
|
|
|
|
`engine` is the scope this action is being performed in.
|
|
|
|
|
|
|
|
`entity` is the object performing the action.
|
|
|
|
|
|
|
|
This method must be overridden by Action subclasses.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class EscapeAction(Action):
|
|
|
|
def perform(self, engine: Engine, entity: Entity) -> None:
|
|
|
|
raise SystemExit()
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
class ActionWithDirection(Action):
|
2022-01-02 15:23:25 +00:00
|
|
|
def __init__(self, dx: int, dy: int):
|
|
|
|
super().__init__()
|
|
|
|
self.dx = dx
|
|
|
|
self.dy = dy
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
def perform(self, engine: Engine, entity: Entity) -> None:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
class MeleeAction(ActionWithDirection):
|
|
|
|
def perform(self, engine: Engine, entity: Entity) -> None:
|
|
|
|
dest_x = entity.x + self.dx
|
|
|
|
dest_y = entity.y + self.dy
|
|
|
|
target = engine.game_map.get_blocking_entity_at_location(dest_x, dest_y)
|
|
|
|
if not target:
|
|
|
|
return # no entity to blockj
|
|
|
|
print(f"You kick {target.name}, much to its annoyance")
|
|
|
|
|
|
|
|
|
|
|
|
class BumpAction(ActionWithDirection):
|
|
|
|
def perform(self, engine: Engine, entity: Entity) -> None:
|
|
|
|
dest_x = entity.x + self.dx
|
|
|
|
dest_y = entity.y + self.dy
|
|
|
|
if engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
|
|
|
|
return MeleeAction(self.dx, self.dy).perform(engine, entity)
|
|
|
|
else:
|
|
|
|
return MovementAction(self.dx, self.dy).perform(engine, entity)
|
|
|
|
|
|
|
|
|
|
|
|
class MovementAction(ActionWithDirection):
|
2022-01-02 15:23:25 +00:00
|
|
|
def perform(self, engine: Engine, entity: Entity) -> None:
|
|
|
|
dest_x = entity.x + self.dx
|
|
|
|
dest_y = entity.y + self.dy
|
|
|
|
|
|
|
|
if not engine.game_map.in_bounds(dest_x, dest_y):
|
|
|
|
return # OOB
|
|
|
|
if not engine.game_map.tiles["walkable"][dest_x, dest_y]:
|
2022-01-02 17:19:43 +00:00
|
|
|
return # can't walk
|
|
|
|
if engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
|
|
|
|
return # Blocked
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
entity.move(self.dx, self.dy)
|