162 lines
5.0 KiB
Python
162 lines
5.0 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
import random
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
from data.constants import MESSAGE_LOG_LIMIT, TILE_FLOOR
|
|
from entities.entity import Entity
|
|
from entities.enemy import Enemy
|
|
|
|
if TYPE_CHECKING:
|
|
from engine.map_gen import GameMap, Point
|
|
from entities.player import Player
|
|
|
|
EQUIP_CATEGORIES: Final = ("weapon", "armor", "boots")
|
|
CONSUMABLE_CATEGORIES: Final = ("potion", "scroll")
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class TimedEffect:
|
|
"""A temporary status applied by a potion; reverted when turns run out."""
|
|
|
|
kind: str # "strength" | "invisible"
|
|
power_bonus: int = 0
|
|
turns_left: int = 0
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Item(Entity):
|
|
blocks_movement: bool = False
|
|
key: str = "item"
|
|
name: str = "Item"
|
|
sprite_key: str = "item"
|
|
category: str = "potion"
|
|
power: int = 0
|
|
armor_bonus: int = 0
|
|
evasion_mod: int = 0
|
|
crit_mod: float = 0.0
|
|
ranged_range: int = 0
|
|
enables_running: bool = False
|
|
heal_amount: int = 0
|
|
effect: str = ""
|
|
effect_power: int = 0
|
|
effect_turns: int = 0
|
|
aoe_damage: int = 0
|
|
aoe_radius: int = 0
|
|
|
|
@property
|
|
def is_equippable(self) -> bool:
|
|
return self.category in EQUIP_CATEGORIES
|
|
|
|
@property
|
|
def is_consumable(self) -> bool:
|
|
return self.category in CONSUMABLE_CATEGORIES
|
|
|
|
def describe(self) -> str:
|
|
if self.category == "weapon":
|
|
parts = [f"power +{self.power}"]
|
|
if self.ranged_range:
|
|
parts.append(f"ranged {self.ranged_range}")
|
|
if self.crit_mod:
|
|
parts.append(f"crit {self.crit_mod:+.0%}")
|
|
return ", ".join(parts)
|
|
if self.category == "armor":
|
|
parts = [f"armor {self.armor_bonus}"]
|
|
if self.evasion_mod:
|
|
parts.append(f"evasion {self.evasion_mod:+d}")
|
|
return ", ".join(parts)
|
|
if self.category == "boots":
|
|
return "enables held-direction running"
|
|
if self.effect == "heal":
|
|
return f"restores {self.heal_amount} HP"
|
|
if self.effect == "strength":
|
|
return f"+{self.effect_power} power for {self.effect_turns} turns"
|
|
if self.effect == "invisible":
|
|
return f"invisible for {self.effect_turns} turns"
|
|
if self.effect == "fireball":
|
|
return f"{self.aoe_damage} damage to all enemies within {self.aoe_radius} tiles"
|
|
if self.effect == "teleport":
|
|
return "teleports you to a random floor tile"
|
|
return "reveals the entire floor layout"
|
|
|
|
|
|
def _log_message(log: list[str] | None, message: str) -> None:
|
|
if log is None:
|
|
return
|
|
log.append(message)
|
|
del log[:-MESSAGE_LOG_LIMIT]
|
|
|
|
|
|
def apply_fireball(
|
|
item: Item,
|
|
player: Player,
|
|
game_map: GameMap,
|
|
log: list[str] | None,
|
|
center: Point | None,
|
|
) -> list[Enemy]:
|
|
"""Sear every enemy within the blast radius of `center` (default: player).
|
|
|
|
Dead enemies are returned; the turn engine removes them and handles any
|
|
on-death behavior, matching how melee kills are resolved.
|
|
"""
|
|
cx, cy = center if center is not None else player.position
|
|
killed: list[Enemy] = []
|
|
for entity in game_map.entities:
|
|
if not isinstance(entity, Enemy) or not entity.alive:
|
|
continue
|
|
if max(abs(entity.x - cx), abs(entity.y - cy)) > item.aoe_radius:
|
|
continue
|
|
entity.hp = max(0, entity.hp - item.aoe_damage)
|
|
_log_message(log, f"The fireball sears the {entity.name} for {item.aoe_damage} damage!")
|
|
if not entity.alive:
|
|
killed.append(entity)
|
|
_log_message(log, f"You defeated the {entity.name}! (+{entity.xp_reward} XP)")
|
|
player.gain_xp(entity.xp_reward, log)
|
|
return killed
|
|
|
|
|
|
def apply_teleport(
|
|
player: Player,
|
|
game_map: GameMap,
|
|
rng: random.Random,
|
|
log: list[str] | None,
|
|
) -> bool:
|
|
occupied = {e.position for e in game_map.entities if e.blocks_movement}
|
|
free = [
|
|
(x, y)
|
|
for y, row in enumerate(game_map.tiles)
|
|
for x, tile in enumerate(row)
|
|
if tile == TILE_FLOOR and (x, y) not in occupied
|
|
]
|
|
if not free:
|
|
return False
|
|
player.x, player.y = rng.choice(free)
|
|
_log_message(log, "The world lurches sideways — you rematerialize elsewhere!")
|
|
return True
|
|
|
|
|
|
def apply_mapping(game_map: GameMap, log: list[str] | None) -> None:
|
|
for y in range(game_map.height):
|
|
for x in range(game_map.width):
|
|
game_map.explored[y][x] = 1
|
|
_log_message(log, "The layout of this floor unfurls in your mind!")
|
|
|
|
|
|
def apply_scroll(
|
|
item: Item,
|
|
player: Player,
|
|
game_map: GameMap,
|
|
rng: random.Random,
|
|
log: list[str] | None = None,
|
|
center: Point | None = None,
|
|
) -> list[Enemy]:
|
|
"""Apply a scroll's world effect. Returns enemies killed (caller removes)."""
|
|
if item.effect == "fireball":
|
|
return apply_fireball(item, player, game_map, log, center)
|
|
if item.effect == "teleport":
|
|
apply_teleport(player, game_map, rng, log)
|
|
elif item.effect == "mapping":
|
|
apply_mapping(game_map, log)
|
|
return []
|