(Underdepths) Pass 7
This commit is contained in:
40 files changed
+8023
-35
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from entities.entity import Entity
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class Enemy(Entity):
|
||||
key: str = "enemy"
|
||||
name: str = "Enemy"
|
||||
sprite_key: str = "enemy"
|
||||
hp: int = 1
|
||||
max_hp: int = 1
|
||||
power: int = 1
|
||||
armor: int = 0
|
||||
accuracy: int = 0
|
||||
evasion: int = 0
|
||||
crit_chance: float = 0.0
|
||||
speed: int = 1
|
||||
behavior: str = "wander"
|
||||
ranged_power: int = 0
|
||||
xp_reward: int = 0
|
||||
last_known_position: tuple[int, int] | None = None
|
||||
|
||||
@property
|
||||
def alive(self) -> bool:
|
||||
return self.hp > 0
|
||||
@@ -0,0 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class Entity:
|
||||
x: int = 0
|
||||
y: int = 0
|
||||
name: str = "Entity"
|
||||
sprite_key: str = "entity"
|
||||
blocks_movement: bool = True
|
||||
|
||||
@property
|
||||
def position(self) -> tuple[int, int]:
|
||||
return self.x, self.y
|
||||
|
||||
def move(self, dx: int, dy: int) -> None:
|
||||
self.x += dx
|
||||
self.y += dy
|
||||
@@ -0,0 +1,161 @@
|
||||
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 []
|
||||
@@ -0,0 +1,172 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from data.constants import (
|
||||
INVENTORY_MAX_SLOTS,
|
||||
LEVEL_HP_GAINS,
|
||||
LEVEL_POWER_GAINS,
|
||||
MAX_LEVEL,
|
||||
MESSAGE_LOG_LIMIT,
|
||||
PLAYER_ACCURACY,
|
||||
PLAYER_ARMOR,
|
||||
PLAYER_CRIT_CHANCE,
|
||||
PLAYER_EVASION,
|
||||
PLAYER_MAX_HP,
|
||||
PLAYER_POWER,
|
||||
PLAYER_SPEED,
|
||||
XP_THRESHOLDS,
|
||||
)
|
||||
from entities.entity import Entity
|
||||
from entities.item import Item, TimedEffect
|
||||
|
||||
|
||||
def _log_message(log: list[str] | None, message: str) -> None:
|
||||
if log is None:
|
||||
return
|
||||
log.append(message)
|
||||
del log[:-MESSAGE_LOG_LIMIT]
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class Player(Entity):
|
||||
name: str = "Player"
|
||||
sprite_key: str = "player"
|
||||
hp: int = PLAYER_MAX_HP
|
||||
max_hp: int = PLAYER_MAX_HP
|
||||
power: int = PLAYER_POWER
|
||||
armor: int = PLAYER_ARMOR
|
||||
accuracy: int = PLAYER_ACCURACY
|
||||
evasion: int = PLAYER_EVASION
|
||||
crit_chance: float = PLAYER_CRIT_CHANCE
|
||||
speed: int = PLAYER_SPEED
|
||||
level: int = 1
|
||||
xp: int = 0
|
||||
inventory: list[Item] = field(default_factory=list)
|
||||
equipped_weapon: Item | None = None
|
||||
equipped_armor: Item | None = None
|
||||
equipped_boots: Item | None = None
|
||||
effects: list[TimedEffect] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def alive(self) -> bool:
|
||||
return self.hp > 0
|
||||
|
||||
@property
|
||||
def total_power(self) -> int:
|
||||
weapon = self.equipped_weapon
|
||||
return self.power + (weapon.power if weapon else 0) + sum(
|
||||
effect.power_bonus for effect in self.effects
|
||||
)
|
||||
|
||||
@property
|
||||
def total_armor(self) -> int:
|
||||
armor = self.equipped_armor
|
||||
return self.armor + (armor.armor_bonus if armor else 0)
|
||||
|
||||
@property
|
||||
def total_evasion(self) -> int:
|
||||
armor = self.equipped_armor
|
||||
return self.evasion + (armor.evasion_mod if armor else 0)
|
||||
|
||||
@property
|
||||
def total_crit_chance(self) -> float:
|
||||
weapon = self.equipped_weapon
|
||||
return max(0.0, self.crit_chance + (weapon.crit_mod if weapon else 0.0))
|
||||
|
||||
@property
|
||||
def can_run(self) -> bool:
|
||||
return self.equipped_boots is not None and self.equipped_boots.enables_running
|
||||
|
||||
@property
|
||||
def has_bow(self) -> bool:
|
||||
weapon = self.equipped_weapon
|
||||
return weapon is not None and weapon.ranged_range > 0
|
||||
|
||||
@property
|
||||
def bow_range(self) -> int:
|
||||
weapon = self.equipped_weapon
|
||||
return weapon.ranged_range if weapon else 0
|
||||
|
||||
@property
|
||||
def is_invisible(self) -> bool:
|
||||
return any(effect.kind == "invisible" for effect in self.effects)
|
||||
|
||||
def heal(self, amount: int) -> int:
|
||||
previous = self.hp
|
||||
self.hp = min(self.max_hp, self.hp + max(0, amount))
|
||||
return self.hp - previous
|
||||
|
||||
def gain_xp(self, amount: int, log: list[str] | None = None) -> None:
|
||||
self.xp += amount
|
||||
while self.level < MAX_LEVEL and self.xp >= XP_THRESHOLDS[self.level - 1]:
|
||||
self.level += 1
|
||||
hp_gain = LEVEL_HP_GAINS[self.level - 1]
|
||||
power_gain = LEVEL_POWER_GAINS[self.level - 1]
|
||||
self.max_hp += hp_gain
|
||||
self.power += power_gain
|
||||
self.hp = self.max_hp
|
||||
_log_message(
|
||||
log, f"You reach level {self.level}! (HP +{hp_gain}, Power +{power_gain})"
|
||||
)
|
||||
|
||||
def add_item(self, item: Item) -> bool:
|
||||
if len(self.inventory) >= INVENTORY_MAX_SLOTS:
|
||||
return False
|
||||
self.inventory.append(item)
|
||||
return True
|
||||
|
||||
def _take_from_inventory(self, item: Item) -> bool:
|
||||
for index, candidate in enumerate(self.inventory):
|
||||
if candidate is item:
|
||||
del self.inventory[index]
|
||||
return True
|
||||
return False
|
||||
|
||||
def equip(self, item: Item) -> Item | None:
|
||||
"""Equip an inventory item into its slot; returns the replaced item."""
|
||||
if not item.is_equippable or not self._take_from_inventory(item):
|
||||
return None
|
||||
slot = {
|
||||
"weapon": "equipped_weapon",
|
||||
"armor": "equipped_armor",
|
||||
"boots": "equipped_boots",
|
||||
}[item.category]
|
||||
previous = getattr(self, slot)
|
||||
if previous is not None:
|
||||
self.inventory.append(previous)
|
||||
setattr(self, slot, item)
|
||||
return previous
|
||||
|
||||
def unequip(self, slot: str) -> Item | None:
|
||||
attribute = {
|
||||
"weapon": "equipped_weapon",
|
||||
"armor": "equipped_armor",
|
||||
"boots": "equipped_boots",
|
||||
}.get(slot)
|
||||
if attribute is None:
|
||||
return None
|
||||
item = getattr(self, attribute)
|
||||
if item is None or not self.add_item(item):
|
||||
return None
|
||||
setattr(self, attribute, None)
|
||||
return item
|
||||
|
||||
def consume(self, item: Item) -> bool:
|
||||
return self._take_from_inventory(item)
|
||||
|
||||
def tick_effects(self, log: list[str] | None = None) -> None:
|
||||
expired: list[TimedEffect] = []
|
||||
remaining: list[TimedEffect] = []
|
||||
for effect in self.effects:
|
||||
effect.turns_left -= 1
|
||||
if effect.turns_left <= 0:
|
||||
expired.append(effect)
|
||||
else:
|
||||
remaining.append(effect)
|
||||
self.effects = remaining
|
||||
for effect in expired:
|
||||
if effect.kind == "strength":
|
||||
_log_message(log, "Your surge of strength fades.")
|
||||
elif effect.kind == "invisible":
|
||||
_log_message(log, "You fade back into view.")
|
||||
Reference in new issue
Block a user