From be838dfcc6e9bfc37ac29f51ef8b17440d4bb8b2 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 11 Jan 2022 16:21:42 -0500 Subject: [PATCH] Render entity names on mouse hover --- engine.py | 9 ++++----- input_handlers.py | 20 +++++++++++++++----- main.py | 6 ++++-- render_functions.py | 26 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/engine.py b/engine.py index f950500..fc390fc 100644 --- a/engine.py +++ b/engine.py @@ -8,7 +8,7 @@ from tcod.map import compute_fov from input_handlers import MainGameEventHandler from message_log import MessageLog -from render_functions import render_bar +from render_functions import render_bar, render_names_at_mouse_location if TYPE_CHECKING: from entity import Actor @@ -21,6 +21,7 @@ class Engine: def __init__(self, player: Actor): self.event_handler: EventHandler = MainGameEventHandler(self) self.message_log = MessageLog() + self.mouse_location = (0, 0) self.player = player def handle_enemy_turns(self) -> None: @@ -39,7 +40,7 @@ class Engine: # If a tile is "visible" it should be added to "explored" self.game_map.explored |= self.game_map.visible - def render(self, console: Console, context: Context) -> None: + def render(self, console: Console) -> None: self.game_map.render(console) self.message_log.render(console=console, x=21, y=45, width=40, height=5) @@ -51,6 +52,4 @@ class Engine: total_width=20, ) - # Actually output to screen - context.present(console) - console.clear() + render_names_at_mouse_location(console, x=21, y=44, engine=self) diff --git a/input_handlers.py b/input_handlers.py index 37e8cf0..db5eef8 100644 --- a/input_handlers.py +++ b/input_handlers.py @@ -52,17 +52,27 @@ class EventHandler(tcod.event.EventDispatch[Action]): def __init__(self, engine: Engine): self.engine = engine - @overload - def handle_events(self) -> None: - pass + def handle_events(self, context: tcod.context.Context) -> None: + for event in tcod.event.wait(): + context.convert_event(event) + self.dispatch(event) + + def ev_mousemotion(self, event: tcod.event.MouseMotion) -> None: + if self.engine.game_map.in_bounds(event.tile.x, event.tile.y): + self.engine.mouse_location = event.tile.x, event.tile.y def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]: raise SystemExit() + def on_render(self, console: tcod.Console) -> None: + self.engine.render(console) + class MainGameEventHandler(EventHandler): - def handle_events(self) -> None: + def handle_events(self, context: tcod.context.Context) -> None: for event in tcod.event.wait(): + context.convert_event(event) + action = self.dispatch(event) if action is None: @@ -94,7 +104,7 @@ class MainGameEventHandler(EventHandler): class GameOverEventHandler(EventHandler): - def handle_events(self) -> None: + def handle_events(self, context: tcod.context.Context) -> None: for event in tcod.event.wait(): action = self.dispatch(event) diff --git a/main.py b/main.py index ffaded0..45e1108 100755 --- a/main.py +++ b/main.py @@ -58,9 +58,11 @@ def main() -> None: ) as context: root_console = tcod.Console(screen_width, screen_height, order="F") while True: - engine.render(root_console, context) + root_console.clear() + engine.event_handler.on_render(console=root_console) + context.present(root_console) - engine.event_handler.handle_events() + engine.event_handler.handle_events(context) if __name__ == "__main__": diff --git a/render_functions.py b/render_functions.py index 0651e5a..47e457d 100644 --- a/render_functions.py +++ b/render_functions.py @@ -6,6 +6,19 @@ import color if TYPE_CHECKING: from tcod import Console + from engine import Engine + from game_map import GameMap + + +def get_names_at_location(x: int, y: int, game_map: GameMap) -> str: + if not game_map.in_bounds(x, y) or not game_map.visible[x, y]: + return "" + + names = ", ".join( + entity.name for entity in game_map.entities if entity.x == x and entity.y == y + ) + + return names.capitalize() def render_bar( @@ -34,3 +47,16 @@ def render_bar( string=f"HP: {current_value}/{maximum_value}", fg=color.bar_text, ) + + +def render_names_at_mouse_location( + console: Console, + x: int, + y: int, + engine: Engine +) -> None: + mouse_x, mouse_y = engine.mouse_location + + names_at_mouse_location = get_names_at_location(mouse_x, mouse_y, engine.game_map) + + console.print(x, y, names_at_mouse_location) \ No newline at end of file