1
0

Render entity names on mouse hover

This commit is contained in:
Timothy Warren 2022-01-11 16:21:42 -05:00
parent b0f5e2afe0
commit be838dfcc6
4 changed files with 49 additions and 12 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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__":

View File

@ -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)