48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import colors
|
|
from components.base_component import BaseComponent
|
|
|
|
class Fighter(BaseComponent):
|
|
entity: Any
|
|
|
|
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:
|
|
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 = self.entity.floor_map.engine
|
|
|
|
if self.entity is engine.player and self.entity.ai:
|
|
from event_handler import GameOverEventHandler
|
|
self.entity.floor_map.engine.event_handler = GameOverEventHandler(self.entity.floor_map.engine)
|
|
engine.message_log.add_message("You died.", colors.player_die)
|
|
|
|
else:
|
|
engine.message_log.add_message(f"The {self.entity.name} died", colors.enemy_die)
|
|
|
|
self.entity.char = "%"
|
|
self.entity.color = (191, 0, 0)
|
|
self.entity.walkable = True
|
|
self.entity.ai = None
|
|
self.entity.name = f"Dead {self.entity.name}"
|