Add death
This commit is contained in:
parent
018dbe35b8
commit
02215c884a
@ -1,7 +1,17 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from components.base_component import BaseComponent
|
||||
from render_order import RenderOrder
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from entity import Actor
|
||||
|
||||
|
||||
class Fighter(BaseComponent):
|
||||
entity: Actor
|
||||
|
||||
def __init__(self, hp: int, defense: int, power: int):
|
||||
self.max_hp = hp
|
||||
self._hp = hp
|
||||
@ -15,3 +25,20 @@ class Fighter(BaseComponent):
|
||||
@hp.setter
|
||||
def hp(self, value: int) -> None:
|
||||
self._hp = max(0, min(value, self.max_hp))
|
||||
if self._hp == 0 and self.entity.ai:
|
||||
self.die()
|
||||
|
||||
def die(self) -> None:
|
||||
if self.engine.player is self.entity:
|
||||
death_message = "You died!"
|
||||
else:
|
||||
death_message = f"{self.entity.name} is dead!"
|
||||
|
||||
self.entity.char = "%"
|
||||
self.entity.color = (191, 0, 0)
|
||||
self.entity.blocks_movement = False
|
||||
self.entity.ai = None
|
||||
self.entity.name = f"remains of {self.entity.name}"
|
||||
self.entity.render_order = RenderOrder.CORPSE
|
||||
|
||||
print(death_message)
|
||||
|
@ -3,6 +3,8 @@ from __future__ import annotations
|
||||
import copy
|
||||
from typing import Optional, Tuple, Type, TypeVar, TYPE_CHECKING
|
||||
|
||||
from render_order import RenderOrder
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from components.ai import BaseAI
|
||||
from components.fighter import Fighter
|
||||
@ -27,6 +29,7 @@ class Entity:
|
||||
color: Tuple[int, int, int] = (255, 255, 255),
|
||||
name: str = "<Unnamed>",
|
||||
blocks_movement: bool = False,
|
||||
render_order: RenderOrder = RenderOrder.CORPSE,
|
||||
):
|
||||
self.x = x
|
||||
self.y = y
|
||||
@ -34,6 +37,7 @@ class Entity:
|
||||
self.color = color
|
||||
self.name = name
|
||||
self.blocks_movement = blocks_movement
|
||||
self.render_order = render_order
|
||||
if gamemap:
|
||||
# If gamemap isn't provided now, it will be later.
|
||||
self.gamemap = gamemap
|
||||
|
13
game_map.py
13
game_map.py
@ -88,7 +88,16 @@ class GameMap:
|
||||
default=tile_types.SHROUD,
|
||||
)
|
||||
|
||||
for entity in self.entities:
|
||||
entities_sorted_for_rendering = sorted(
|
||||
self.entities, key=lambda x: x.render_order.value
|
||||
)
|
||||
|
||||
for entity in entities_sorted_for_rendering:
|
||||
# Only print entities that are in the FOV
|
||||
if self.visible[entity.x, entity.y]:
|
||||
console.print(x=entity.x, y=entity.y, string=entity.char, fg=entity.color)
|
||||
console.print(
|
||||
x=entity.x,
|
||||
y=entity.y,
|
||||
string=entity.char,
|
||||
fg=entity.color
|
||||
)
|
||||
|
7
render_order.py
Normal file
7
render_order.py
Normal file
@ -0,0 +1,7 @@
|
||||
from enum import auto, Enum
|
||||
|
||||
|
||||
class RenderOrder(Enum):
|
||||
CORPSE = auto()
|
||||
ITEM = auto()
|
||||
ACTOR = auto()
|
Loading…
Reference in New Issue
Block a user