2022-01-06 11:56:08 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-11 16:08:57 -05:00
|
|
|
from typing import overload, Optional, Tuple, TYPE_CHECKING
|
|
|
|
|
|
|
|
import color
|
2022-01-12 16:12:07 -05:00
|
|
|
import exceptions
|
2022-01-06 11:56:08 -05:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from engine import Engine
|
2022-01-12 16:12:07 -05:00
|
|
|
from entity import Actor, Entity, Item
|
2022-01-06 11:56:08 -05:00
|
|
|
|
2022-01-07 14:18:47 -05:00
|
|
|
|
2022-01-06 11:09:43 -05:00
|
|
|
class Action:
|
2022-01-10 13:47:10 -05:00
|
|
|
def __init__(self, entity: Actor) -> None:
|
2022-01-07 16:25:07 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
2022-01-12 16:24:09 -05:00
|
|
|
class PickupAction(Action):
|
|
|
|
"""Pickup an item and add it to the inventory, if there is room for it."""
|
|
|
|
|
|
|
|
def __init__(self, entity: Actor):
|
|
|
|
super().__init__(entity)
|
|
|
|
|
|
|
|
def perform(self) -> None:
|
|
|
|
actor_location_x = self.entity.x
|
|
|
|
actor_location_y = self.entity.y
|
|
|
|
inventory = self.entity.inventory
|
|
|
|
|
|
|
|
for item in self.engine.game_map.items:
|
|
|
|
if actor_location_x == item.x and actor_location_y == item.y:
|
|
|
|
if len(inventory.items) >= inventory.capacity:
|
|
|
|
raise exceptions.Impossible("Your inventory is full.")
|
|
|
|
|
|
|
|
self.engine.game_map.entities.remove(item)
|
|
|
|
item.parent = self.entity.inventory
|
|
|
|
inventory.items.append(item)
|
|
|
|
|
|
|
|
self.engine.message_log.add_message(f"You picked up the {item.name}!")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
raise exceptions.Impossible("There is nothing here to pick up.")
|
|
|
|
|
|
|
|
|
2022-01-12 16:12:07 -05:00
|
|
|
class ItemAction(Action):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
entity: Actor,
|
|
|
|
item: Item,
|
|
|
|
target_xy: Optional[Tuple[int, int]] = None
|
|
|
|
):
|
|
|
|
super().__init__(entity)
|
|
|
|
self.item = item
|
|
|
|
if not target_xy:
|
|
|
|
target_xy = entity.x, entity.y
|
|
|
|
self.target_xy = target_xy
|
|
|
|
|
|
|
|
@property
|
|
|
|
def target_actor(self) -> Optional[Actor]:
|
|
|
|
"""Return the actor at this action's destination."""
|
|
|
|
return self.engine.game_map.get_actor_at_location(*self.target_xy)
|
|
|
|
|
|
|
|
def perform(self) -> None:
|
|
|
|
"""Invoke the item's ability, this action will be given to provide context."""
|
|
|
|
self.item.consumable.activate(self)
|
|
|
|
|
|
|
|
|
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-12 16:44:57 -05:00
|
|
|
class DropItem(ItemAction):
|
|
|
|
def perform(self) -> None:
|
|
|
|
self.entity.inventory.drop(self.item)
|
|
|
|
|
|
|
|
|
2022-01-10 13:21:17 -05:00
|
|
|
class WaitAction(Action):
|
|
|
|
def perform(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-01-07 15:52:53 -05:00
|
|
|
class ActionWithDirection(Action):
|
2022-01-10 13:47:10 -05:00
|
|
|
def __init__(self, entity: Actor, dx: int, dy: int):
|
2022-01-07 16:25:07 -05:00
|
|
|
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-10 13:47:10 -05:00
|
|
|
@property
|
|
|
|
def target_actor(self) -> Optional[Actor]:
|
|
|
|
"""Return the actor at this action's destination."""
|
|
|
|
return self.engine.game_map.get_actor_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:
|
2022-01-10 13:47:10 -05:00
|
|
|
target = self.target_actor
|
2022-01-07 15:52:53 -05:00
|
|
|
if not target:
|
2022-01-12 16:12:07 -05:00
|
|
|
raise exceptions.Impossible("Nothing to attack.")
|
2022-01-07 15:52:53 -05:00
|
|
|
|
2022-01-10 13:47:10 -05:00
|
|
|
damage = self.entity.fighter.power - target.fighter.defense
|
|
|
|
|
|
|
|
attack_desc = f"{self.entity.name.capitalize()} attacks {target.name}"
|
2022-01-11 16:08:57 -05:00
|
|
|
if self.entity is self.engine.player:
|
|
|
|
attack_color = color.player_atk
|
|
|
|
else:
|
|
|
|
attack_color = color.enemy_atk
|
|
|
|
|
2022-01-10 13:47:10 -05:00
|
|
|
if damage > 0:
|
2022-01-11 16:08:57 -05:00
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"{attack_desc} for {damage} hit points.",
|
|
|
|
attack_color,
|
|
|
|
)
|
2022-01-10 13:47:10 -05:00
|
|
|
target.fighter.hp -= damage
|
|
|
|
else:
|
2022-01-11 16:08:57 -05:00
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"{attack_desc} but does no damage.",
|
|
|
|
attack_color,
|
|
|
|
)
|
2022-01-07 15:52:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
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-12 16:12:07 -05:00
|
|
|
# Destination is out of bounds
|
|
|
|
raise exceptions.Impossible("That way is blocked.")
|
2022-01-07 16:25:07 -05:00
|
|
|
if not self.engine.game_map.tiles["walkable"][dest_x, dest_y]:
|
2022-01-12 16:12:07 -05:00
|
|
|
# Destination is blocked by a tile.
|
|
|
|
raise exceptions.Impossible("That way is blocked.")
|
2022-01-07 16:25:07 -05:00
|
|
|
if self.engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
|
2022-01-12 16:12:07 -05:00
|
|
|
# Destination is blocked by an entity.
|
|
|
|
raise exceptions.Impossible("That way is blocked.")
|
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:
|
2022-01-10 13:47:10 -05:00
|
|
|
if self.target_actor:
|
2022-01-07 16:25:07 -05:00
|
|
|
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()
|