Stepwise/source/stats.py
Kayne Ruse f831019148 Items can be picked up and stored in the inventory
When more than one item can be picked up, and options window is shown.

Stubs for "using" an item are in place.
2025-03-29 16:30:44 +11:00

58 lines
1.5 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
import colors
from event_handlers import GameOverHandler
from useable import BaseUseable
if TYPE_CHECKING:
from engine import Engine
from entity import Entity
class Stats:
"""Handles stats for an Entity"""
entity: Entity
#TODO: better combat system
def __init__(self, hp: int, attack: int, defense: int):
self._maximum_hp = hp
self._current_hp = hp
self.attack = attack
self.defense = defense
@property
def maximum_hp(self) -> int:
return self._maximum_hp
@property
def current_hp(self) -> int:
return self._current_hp
@current_hp.setter
def current_hp(self, value: int) -> None:
"""Clamps to (0,maximum_hp), and calls `die_and_despawn()` if needed"""
self._current_hp = max(0, min(value, self._maximum_hp))
if self.current_hp <= 0:
self.die_and_despawn()
def die_and_despawn(self) -> None:
engine: Engine = self.entity.floor_map.engine
if self.entity is engine.player and self.entity.ai: #handle game-over states
engine.event_handler = GameOverHandler(engine)
engine.message_log.add_message("You died.", colors.red)
else:
engine.message_log.add_message(f"The {self.entity.name} died", colors.yellow)
#transform into a dead body
self.entity.char = "%"
self.entity.color = (191, 0, 0)
self.entity.walkable = True
self.entity.ai = None #TODO: Could decay over time
self.entity.useable = BaseUseable(self.entity) #TMP
self.entity.name = f"Dead {self.entity.name}"