2022-01-12 16:12:07 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Optional, TYPE_CHECKING
|
|
|
|
|
|
|
|
import actions
|
|
|
|
import color
|
2022-01-14 16:45:07 -05:00
|
|
|
import components.ai
|
2022-01-12 16:53:09 -05:00
|
|
|
import components.inventory
|
2022-01-12 16:12:07 -05:00
|
|
|
from components.base_component import BaseComponent
|
|
|
|
from exceptions import Impossible
|
2022-01-18 14:04:05 -05:00
|
|
|
from input_handlers import (
|
|
|
|
ActionOrHandler,
|
|
|
|
AreaRangedAttackHandler,
|
2022-02-08 10:00:28 -05:00
|
|
|
SingleRangedAttackHandler,
|
2022-01-18 14:04:05 -05:00
|
|
|
)
|
2022-01-12 16:12:07 -05:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from entity import Actor, Item
|
|
|
|
|
|
|
|
|
|
|
|
class Consumable(BaseComponent):
|
|
|
|
parent: Item
|
|
|
|
|
2022-01-18 14:04:05 -05:00
|
|
|
def get_action(self, consumer: Actor) -> Optional[ActionOrHandler]:
|
2022-01-12 16:12:07 -05:00
|
|
|
"""Try to return the action for this item."""
|
|
|
|
return actions.ItemAction(consumer, self.parent)
|
|
|
|
|
|
|
|
def activate(self, action: actions.ItemAction) -> None:
|
|
|
|
"""Invoke this item's ability.
|
|
|
|
|
|
|
|
`action` is the context for this activation.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-01-12 16:53:09 -05:00
|
|
|
def consume(self) -> None:
|
|
|
|
"""Remove the consumed item from its containing inventory."""
|
|
|
|
entity = self.parent
|
|
|
|
inventory = entity.parent
|
|
|
|
if isinstance(inventory, components.inventory.Inventory):
|
|
|
|
inventory.items.remove(entity)
|
|
|
|
|
2022-01-12 16:12:07 -05:00
|
|
|
|
2022-01-14 16:45:07 -05:00
|
|
|
class ConfusionConsumable(Consumable):
|
|
|
|
def __init__(self, number_of_turns: int):
|
|
|
|
self.number_of_turns = number_of_turns
|
|
|
|
|
2022-01-18 14:04:05 -05:00
|
|
|
def get_action(self, consumer: Actor) -> SingleRangedAttackHandler:
|
2022-01-14 16:45:07 -05:00
|
|
|
self.engine.message_log.add_message(
|
2022-02-08 10:00:28 -05:00
|
|
|
"Select a target location.", color.needs_target
|
2022-01-14 16:45:07 -05:00
|
|
|
)
|
2022-01-18 14:04:05 -05:00
|
|
|
|
|
|
|
return SingleRangedAttackHandler(
|
2022-01-14 16:45:07 -05:00
|
|
|
self.engine,
|
|
|
|
callback=lambda xy: actions.ItemAction(consumer, self.parent, xy),
|
|
|
|
)
|
|
|
|
|
|
|
|
def activate(self, action: actions.ItemAction) -> None:
|
|
|
|
consumer = action.entity
|
|
|
|
target = action.target_actor
|
|
|
|
|
|
|
|
if not self.engine.game_map.visible[action.target_xy]:
|
|
|
|
raise Impossible("You cannot target an area that you cannot see.")
|
|
|
|
if not target:
|
|
|
|
raise Impossible("You must select an enemy to target.")
|
|
|
|
if target is consumer:
|
|
|
|
raise Impossible("You cannot confuse yourself!")
|
|
|
|
|
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"The eyes of the {target.name} look vacant, as it starts to stumble around!",
|
|
|
|
color.status_effect_applied,
|
|
|
|
)
|
|
|
|
target.ai = components.ai.ConfusedEnemy(
|
|
|
|
entity=target,
|
|
|
|
previous_ai=target.ai,
|
|
|
|
turns_remaining=self.number_of_turns,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.consume()
|
|
|
|
|
|
|
|
|
2022-01-12 16:12:07 -05:00
|
|
|
class HealingConsumable(Consumable):
|
|
|
|
def __init__(self, amount: int):
|
|
|
|
self.amount = amount
|
|
|
|
|
|
|
|
def activate(self, action: actions.ItemAction) -> None:
|
|
|
|
consumer = action.entity
|
|
|
|
amount_recovered = consumer.fighter.heal(self.amount)
|
|
|
|
|
|
|
|
if amount_recovered > 0:
|
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"You consume the {self.parent.name}, and recover {amount_recovered} HP!",
|
2022-02-08 10:00:28 -05:00
|
|
|
color.health_recovered,
|
2022-01-12 16:12:07 -05:00
|
|
|
)
|
2022-01-12 16:53:09 -05:00
|
|
|
self.consume()
|
2022-01-12 16:12:07 -05:00
|
|
|
else:
|
|
|
|
raise Impossible(f"Your health is already full.")
|
2022-01-13 14:56:38 -05:00
|
|
|
|
|
|
|
|
2022-01-18 13:26:26 -05:00
|
|
|
class FireballDamageConsumable(Consumable):
|
|
|
|
def __init__(self, damage: int, radius: int):
|
|
|
|
self.damage = damage
|
|
|
|
self.radius = radius
|
|
|
|
|
2022-01-18 14:04:05 -05:00
|
|
|
def get_action(self, consumer: Actor) -> AreaRangedAttackHandler:
|
2022-01-18 13:26:26 -05:00
|
|
|
self.engine.message_log.add_message(
|
2022-02-08 10:00:28 -05:00
|
|
|
"Select a target location.", color.needs_target
|
2022-01-18 13:26:26 -05:00
|
|
|
)
|
2022-01-18 14:04:05 -05:00
|
|
|
|
|
|
|
return AreaRangedAttackHandler(
|
2022-01-18 13:26:26 -05:00
|
|
|
self.engine,
|
|
|
|
self.radius,
|
|
|
|
lambda xy: actions.ItemAction(consumer, self.parent, xy),
|
|
|
|
)
|
|
|
|
|
|
|
|
def activate(self, action: actions.ItemAction) -> None:
|
|
|
|
target_xy = action.target_xy
|
|
|
|
|
|
|
|
if not self.engine.game_map.visible[target_xy]:
|
|
|
|
raise Impossible("You cannot target an area that you cannot see.")
|
|
|
|
|
|
|
|
targets_hit = False
|
|
|
|
for actor in self.engine.game_map.actors:
|
|
|
|
if actor.distance(*target_xy) <= self.radius:
|
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"The {actor.name} is engulfed in a fiery explosion, taking {self.damage} damage!"
|
|
|
|
)
|
|
|
|
actor.fighter.take_damage(self.damage)
|
|
|
|
targets_hit = True
|
|
|
|
|
|
|
|
if not targets_hit:
|
|
|
|
raise Impossible("There are no targets in the radius.")
|
|
|
|
|
|
|
|
self.consume()
|
|
|
|
|
|
|
|
|
2022-01-13 14:56:38 -05:00
|
|
|
class LightningDamageConsumable(Consumable):
|
|
|
|
def __init__(self, damage: int, maximum_range: int):
|
|
|
|
self.damage = damage
|
|
|
|
self.maximum_range = maximum_range
|
|
|
|
|
|
|
|
def activate(self, action: actions.ItemAction) -> None:
|
|
|
|
consumer = action.entity
|
|
|
|
target = None
|
|
|
|
closest_distance = self.maximum_range + 1.0
|
|
|
|
|
|
|
|
for actor in self.engine.game_map.actors:
|
|
|
|
if actor is not consumer and self.parent.gamemap.visible[actor.x, actor.y]:
|
|
|
|
distance = consumer.distance(actor.x, actor.y)
|
|
|
|
|
|
|
|
if distance < closest_distance:
|
|
|
|
target = actor
|
|
|
|
closest_distance = distance
|
|
|
|
|
|
|
|
if target:
|
|
|
|
self.engine.message_log.add_message(
|
|
|
|
f"A lightning bolt stikes the {target.name} with a loud thunder, for {self.damage} damage!"
|
|
|
|
)
|
|
|
|
target.fighter.take_damage(self.damage)
|
|
|
|
self.consume()
|
|
|
|
else:
|
|
|
|
raise Impossible("No enemy is close enough to strike.")
|