2022-01-02 15:23:25 +00:00
|
|
|
from __future__ import annotations
|
2022-01-02 17:50:33 +00:00
|
|
|
from typing import Optional, Tuple, TYPE_CHECKING
|
2022-01-03 03:31:10 +00:00
|
|
|
import color
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from engine import Engine
|
2022-01-02 21:43:44 +00:00
|
|
|
from entity import Entity, Actor
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Action:
|
2022-01-02 21:43:44 +00:00
|
|
|
def __init__(self, entity: Actor) -> None:
|
2022-01-02 17:50:33 +00:00
|
|
|
super().__init__()
|
|
|
|
self.entity = entity
|
|
|
|
|
|
|
|
@property
|
|
|
|
def engine(self) -> Engine:
|
2022-01-03 03:31:10 +00:00
|
|
|
return self.entity.game_map.engine
|
2022-01-02 17:50:33 +00:00
|
|
|
|
|
|
|
def perform(self) -> None:
|
2022-01-02 15:23:25 +00:00
|
|
|
"""Perform this action with the objects needed to determine its scope.
|
|
|
|
|
2022-01-02 17:50:33 +00:00
|
|
|
`self.engine` is the scope this action is being performed in.
|
2022-01-02 15:23:25 +00:00
|
|
|
|
2022-01-02 17:50:33 +00:00
|
|
|
`self.entity` is the object performing the action.
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
This method must be overridden by Action subclasses.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class EscapeAction(Action):
|
2022-01-02 17:50:33 +00:00
|
|
|
def perform(self) -> None:
|
2022-01-02 15:23:25 +00:00
|
|
|
raise SystemExit()
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
|
|
|
|
class WaitAction(Action):
|
|
|
|
def perform(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
class ActionWithDirection(Action):
|
2022-01-02 21:43:44 +00:00
|
|
|
def __init__(self, entity: Actor, dx: int, dy: int):
|
2022-01-02 17:50:33 +00:00
|
|
|
super().__init__(entity)
|
2022-01-02 15:23:25 +00:00
|
|
|
self.dx = dx
|
|
|
|
self.dy = dy
|
|
|
|
|
2022-01-02 17:50:33 +00:00
|
|
|
@property
|
2022-01-02 21:43:44 +00:00
|
|
|
def dest_xy(self) -> Tuple[int, int]:
|
2022-01-02 17:50:33 +00:00
|
|
|
return self.entity.x + self.dx, self.entity.y + self.dy
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
@property
|
|
|
|
def target_actor(self) -> Optional[Actor]:
|
2022-01-03 03:31:10 +00:00
|
|
|
return self.engine.game_map.get_actor_at_location(*self.dest_xy)
|
2022-01-02 21:43:44 +00:00
|
|
|
|
2022-01-02 17:50:33 +00:00
|
|
|
@property
|
|
|
|
def blocking_entity(self) -> Optional[Entity]:
|
2022-01-03 03:31:10 +00:00
|
|
|
return self.engine.game_map.get_blocking_entity_at_location(*self.dest_xy)
|
2022-01-02 17:50:33 +00:00
|
|
|
|
|
|
|
def perform(self) -> None:
|
2022-01-02 17:19:43 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
class MeleeAction(ActionWithDirection):
|
2022-01-02 17:50:33 +00:00
|
|
|
def perform(self) -> None:
|
2022-01-02 21:43:44 +00:00
|
|
|
target = self.target_actor
|
2022-01-02 17:19:43 +00:00
|
|
|
if not target:
|
2022-01-02 21:43:44 +00:00
|
|
|
return # no entity to blockj
|
|
|
|
damage = self.entity.fighter.power - target.fighter.defense
|
|
|
|
|
|
|
|
attack_desc = f"{self.entity.name.capitalize()} attacks {target.name}"
|
2022-01-03 03:31:10 +00:00
|
|
|
if self.entity is self.engine.player:
|
|
|
|
attack_color = color.player_atk
|
|
|
|
else:
|
|
|
|
attack_color = color.enemy_atk
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
if damage > 0:
|
2022-01-03 03:31:10 +00:00
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"{attack_desc} for {damage} hit points."
|
|
|
|
)
|
2022-01-02 21:43:44 +00:00
|
|
|
target.fighter.hp -= damage
|
|
|
|
else:
|
2022-01-03 03:31:10 +00:00
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"{attack_desc} but does no damage."
|
|
|
|
)
|
2022-01-02 17:19:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MovementAction(ActionWithDirection):
|
2022-01-02 17:50:33 +00:00
|
|
|
def perform(self) -> None:
|
|
|
|
dest_x, dest_y = self.dest_xy
|
2022-01-02 15:23:25 +00:00
|
|
|
|
2022-01-03 03:31:10 +00:00
|
|
|
if not self.engine.game_map.in_bounds(dest_x, dest_y):
|
2022-01-02 15:23:25 +00:00
|
|
|
return # OOB
|
2022-01-03 03:31:10 +00:00
|
|
|
if not self.engine.game_map.tiles["walkable"][dest_x, dest_y]:
|
2022-01-02 17:19:43 +00:00
|
|
|
return # can't walk
|
2022-01-03 03:31:10 +00:00
|
|
|
if self.engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
|
2022-01-02 21:43:44 +00:00
|
|
|
return # Blocked
|
2022-01-02 15:23:25 +00:00
|
|
|
|
2022-01-02 17:50:33 +00:00
|
|
|
self.entity.move(self.dx, self.dy)
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
|
2022-01-02 17:50:33 +00:00
|
|
|
class BumpAction(ActionWithDirection):
|
|
|
|
def perform(self) -> None:
|
2022-01-02 21:43:44 +00:00
|
|
|
if self.target_actor:
|
2022-01-02 17:50:33 +00:00
|
|
|
return MeleeAction(self.entity, self.dx, self.dy).perform()
|
|
|
|
else:
|
|
|
|
return MovementAction(self.entity, self.dx, self.dy).perform()
|