57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
from __future__ import annotations
|
|
from typing import TYPE_CHECKING
|
|
|
|
import colors
|
|
|
|
from event_handlers import GameOverHandler
|
|
from useable import Unuseable
|
|
|
|
if TYPE_CHECKING:
|
|
from engine import Engine
|
|
from entity import Entity
|
|
|
|
class Stats:
|
|
"""Handles stats for an Entity"""
|
|
entity: Entity
|
|
|
|
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
|
|
#TODO: dummied in a "usable" to let dead objects be treated like items
|
|
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 = Unuseable()
|
|
self.entity.name = f"Dead {self.entity.name}"
|