Skip to content

Commit

Permalink
Death works!
Browse files Browse the repository at this point in the history
Good enough for starters...
  • Loading branch information
hanok2 authored Sep 10, 2022
1 parent 9f52923 commit 52402ab
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
9 changes: 7 additions & 2 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
from tcod.console import Console

from globals import globals
from input_handler import EventHandler
from tcod.map import compute_fov

from entity import Actor

from input_handler import MainGameEventHandler


class Engine:
game_map = None

def __init__(self, player):
self.event_handler: EventHandler = EventHandler(self)
self.event_handler: MainGameEventHandler = MainGameEventHandler(self)
self.player = player

def handle_enemy_turns(self) -> None:
Expand All @@ -37,6 +40,8 @@ def update_fov(self) -> None:
def render(self, console: Console, context: Context) -> None:
self.game_map.render(console)

console.print(x=1, y=47, string=f"HP: {self.player.fighter.hp}/{self.player.fighter.max_hp}")

context.present(console)

console.clear()
Expand Down
5 changes: 4 additions & 1 deletion entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
from typing import Optional, Tuple, Type, TypeVar, TYPE_CHECKING

from render_order import RenderOrder
from globals import globals

from ai import BaseAI
Expand All @@ -14,7 +15,6 @@
T = TypeVar("T", bound="Entity")



class Entity:
'''
A generic object to represent players, enemies, items, etc.
Expand All @@ -31,13 +31,15 @@ def __init__(
color: Tuple[int, int, int] = (255, 0, 0),
name: str = "<Unnamed>",
blocks_movement: bool = False,
render_order: RenderOrder = RenderOrder.CORPSE,
) -> None:
self.x = x
self.y = y
self.char = char
self.color = color
self.name = name
self.blocks_movement = blocks_movement
self.render_order = render_order
if gamemap:
# If gamemap isn't provided now then we will set it later.
self.gamemap = gamemap
Expand Down Expand Up @@ -89,6 +91,7 @@ def __init__(
color=color,
name=name,
blocks_movement=True,
render_order=RenderOrder.ACTOR,
)

self.ai: Optional[BaseAI] = ai_cls(self)
Expand Down
4 changes: 4 additions & 0 deletions fighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import TYPE_CHECKING

from base_component import BaseComponent
from input_handler import GameOverEventHandler
from render_order import RenderOrder

if TYPE_CHECKING:
from entity import Actor
Expand All @@ -28,6 +30,7 @@ def hp(self, value: int) -> None:
def die(self) -> None:
if self.engine.player is self.entity:
death_message = "You died!"
self.engine.event_handler = GameOverEventHandler(self.engine)
else:
death_message = f"{self.entity.name} is dead!"

Expand All @@ -36,6 +39,7 @@ def die(self) -> None:
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)

Expand Down
10 changes: 8 additions & 2 deletions game_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ def render(self, console: Console) -> None:
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
)

##

Expand Down
33 changes: 30 additions & 3 deletions input_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class EventHandler(tcod.event.EventDispatch[Action]):
def __init__(self, engine):
self.engine = engine

def handle_events(self) -> None:
raise NotImplementedError()

def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
raise SystemExit()


class MainGameEventHandler(EventHandler):
def handle_events(self) -> None:
for event in tcod.event.wait():
action = self.dispatch(event)
Expand All @@ -61,9 +69,6 @@ def handle_events(self) -> None:
self.engine.handle_enemy_turns()
self.engine.update_fov() # Update the FOV before the players next action.

def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
raise SystemExit

def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
action: Optional[Action] = None

Expand All @@ -84,4 +89,26 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
return action


class GameOverEventHandler(EventHandler):
def handle_events(self) -> None:
for event in tcod.event.wait():
action = self.dispatch(event)

if action is None:
continue

action.perform()

def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
action: Optional[Action] = None

key = event.sym

if key == tcod.event.K_ESCAPE:
action = EscapeAction(self.engine.player)

# No valid key was pressed
return action


##
10 changes: 10 additions & 0 deletions render_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from enum import auto, Enum


class RenderOrder(Enum):
CORPSE = auto()
ITEM = auto()
ACTOR = auto()


##

0 comments on commit 52402ab

Please sign in to comment.