2022-01-06 11:56:08 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-07 16:48:49 -05:00
|
|
|
from typing import Optional, Tuple, TYPE_CHECKING, overload
|
2022-01-06 11:56:08 -05:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from engine import Engine
|
|
|
|
from entity import Entity
|
|
|
|
|
2022-01-07 14:18:47 -05:00
|
|
|
|
2022-01-06 11:09:43 -05:00
|
|
|
class Action:
|
2022-01-07 16:25:07 -05:00
|
|
|
def __init__(self, entity: Entity) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.entity = entity
|
|
|
|
|
|
|
|
@property
|
|
|
|
def engine(self) -> Engine:
|
|
|
|
"""Return the engine this action belongs to."""
|
|
|
|
return self.entity.gamemap.engine
|
|
|
|
|
2022-01-07 16:48:49 -05:00
|
|
|
@overload
|
2022-01-07 16:25:07 -05:00
|
|
|
def perform(self) -> None:
|
2022-01-06 11:56:08 -05:00
|
|
|
"""Perform this action with the objects needed to determine its scope.
|
|
|
|
|
2022-01-07 16:25:07 -05:00
|
|
|
`self.engine` is the scope this action is being performed in.
|
2022-01-06 11:56:08 -05:00
|
|
|
|
2022-01-07 16:25:07 -05:00
|
|
|
`self.entity` is the object performing the action.
|
2022-01-06 11:56:08 -05:00
|
|
|
|
|
|
|
This method must be overwritten by Action subclasses.
|
|
|
|
"""
|
2022-01-06 11:09:43 -05:00
|
|
|
|
|
|
|
|
|
|
|
class EscapeAction(Action):
|
2022-01-07 16:25:07 -05:00
|
|
|
def perform(self) -> None:
|
2022-01-06 11:56:08 -05:00
|
|
|
raise SystemExit()
|
2022-01-06 11:09:43 -05:00
|
|
|
|
|
|
|
|
2022-01-07 15:52:53 -05:00
|
|
|
class ActionWithDirection(Action):
|
2022-01-07 16:25:07 -05:00
|
|
|
def __init__(self, entity, dx: int, dy: int):
|
|
|
|
super().__init__(entity)
|
2022-01-06 11:09:43 -05:00
|
|
|
|
|
|
|
self.dx = dx
|
|
|
|
self.dy = dy
|
2022-01-06 11:56:08 -05:00
|
|
|
|
2022-01-07 16:25:07 -05:00
|
|
|
@property
|
|
|
|
def dest_xy(self) -> Tuple[int, int]:
|
|
|
|
"""Returns this action's destination."""
|
|
|
|
return self.entity.x + self.dx, self.entity.y + self.dy
|
|
|
|
|
|
|
|
@property
|
|
|
|
def blocking_entity(self) -> Optional[Entity]:
|
|
|
|
"""Return the blocking entity at this action's destination."""
|
|
|
|
return self.engine.game_map.get_blocking_entity_at_location(*self.dest_xy)
|
|
|
|
|
2022-01-07 15:52:53 -05:00
|
|
|
|
|
|
|
class MeleeAction(ActionWithDirection):
|
2022-01-07 16:25:07 -05:00
|
|
|
def perform(self) -> None:
|
|
|
|
target = self.blocking_entity
|
2022-01-07 15:52:53 -05:00
|
|
|
if not target:
|
|
|
|
return # No entity to attack.
|
|
|
|
|
|
|
|
print(f"You kick the {target.name}, much to its annoyance!")
|
|
|
|
|
|
|
|
|
|
|
|
class MovementAction(ActionWithDirection):
|
2022-01-07 16:25:07 -05:00
|
|
|
def perform(self) -> None:
|
|
|
|
dest_x, dest_y = self.dest_xy
|
2022-01-06 11:56:08 -05:00
|
|
|
|
2022-01-07 16:25:07 -05:00
|
|
|
if not self.engine.game_map.in_bounds(dest_x, dest_y):
|
2022-01-07 14:18:47 -05:00
|
|
|
return # Destination is out of bounds
|
2022-01-07 16:25:07 -05:00
|
|
|
if not self.engine.game_map.tiles["walkable"][dest_x, dest_y]:
|
2022-01-07 14:18:47 -05:00
|
|
|
return # Destination is blocked by a tile.
|
2022-01-07 16:25:07 -05:00
|
|
|
if self.engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
|
2022-01-07 15:52:53 -05:00
|
|
|
return # Destination is blocked by an entity.
|
2022-01-06 11:56:08 -05:00
|
|
|
|
2022-01-07 16:25:07 -05:00
|
|
|
self.entity.move(self.dx, self.dy)
|
2022-01-07 15:52:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
class BumpAction(ActionWithDirection):
|
2022-01-07 16:25:07 -05:00
|
|
|
def perform(self) -> None:
|
|
|
|
if self.blocking_entity:
|
|
|
|
return MeleeAction(self.entity, self.dx, self.dy).perform()
|
2022-01-07 15:52:53 -05:00
|
|
|
else:
|
2022-01-07 16:25:07 -05:00
|
|
|
return MovementAction(self.entity, self.dx, self.dy).perform()
|