1
0
python-roguelike/components/fighter.py

66 lines
1.7 KiB
Python
Raw Normal View History

2022-01-10 14:09:31 -05:00
from __future__ import annotations
from typing import TYPE_CHECKING
2022-01-11 16:08:57 -05:00
import color
2022-01-07 16:39:37 -05:00
from components.base_component import BaseComponent
2022-01-10 14:09:31 -05:00
from render_order import RenderOrder
if TYPE_CHECKING:
from entity import Actor
2022-01-07 16:39:37 -05:00
class Fighter(BaseComponent):
2022-01-12 13:45:52 -05:00
parent: Actor
2022-01-10 14:09:31 -05:00
2022-01-07 16:39:37 -05:00
def __init__(self, hp: int, defense: int, power: int):
self.max_hp = hp
self._hp = hp
self.defense = defense
self.power = power
@property
def hp(self) -> int:
return self._hp
@hp.setter
def hp(self, value: int) -> None:
self._hp = max(0, min(value, self.max_hp))
2022-01-12 13:45:52 -05:00
if self._hp == 0 and self.parent.ai:
2022-01-10 14:09:31 -05:00
self.die()
def die(self) -> None:
2022-01-12 13:45:52 -05:00
if self.engine.player is self.parent:
2022-01-10 14:09:31 -05:00
death_message = "You died!"
2022-01-11 16:08:57 -05:00
death_message_color = color.player_die
2022-01-10 14:09:31 -05:00
else:
2022-01-12 13:45:52 -05:00
death_message = f"{self.parent.name} is dead!"
2022-01-11 16:08:57 -05:00
death_message_color = color.enemy_die
2022-01-10 14:09:31 -05:00
2022-01-12 13:45:52 -05:00
self.parent.char = "%"
self.parent.color = (191, 0, 0)
self.parent.blocks_movement = False
self.parent.ai = None
self.parent.name = f"remains of {self.parent.name}"
self.parent.render_order = RenderOrder.CORPSE
2022-01-10 14:09:31 -05:00
2022-01-11 16:08:57 -05:00
self.engine.message_log.add_message(death_message, death_message_color)
def heal(self, amount: int) -> int:
if self.hp == self.max_hp:
return 0
new_hp_value = self.hp + amount
if new_hp_value > self.max_hp:
new_hp_value = self.max_hp
amount_recovered = new_hp_value - self.hp
self.hp = new_hp_value
return amount_recovered
def take_damage(self, amount: int) -> None:
self.hp -= amount