1
0
python-roguelike/actions.py

45 lines
1.2 KiB
Python
Raw Normal View History

2022-01-06 11:56:08 -05:00
from __future__ import annotations
from typing import TYPE_CHECKING
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-06 11:56:08 -05:00
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 overwritten by Action subclasses.
"""
raise NotImplementedError()
2022-01-06 11:09:43 -05:00
class EscapeAction(Action):
2022-01-06 11:56:08 -05:00
def perform(self, engine: Engine, entity: Entity) -> None:
raise SystemExit()
2022-01-06 11:09:43 -05:00
class MovementAction(Action):
def __init__(self, dx: int, dy: int):
super().__init__()
self.dx = dx
self.dy = dy
2022-01-06 11:56:08 -05: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):
2022-01-07 14:18:47 -05:00
return # Destination is out of bounds
2022-01-06 11:56:08 -05:00
if not 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-06 11:56:08 -05:00
2022-01-07 14:18:47 -05:00
entity.move(self.dx, self.dy)