diff --git a/game/components.py b/game/components.py index a75b35b..5eae5c0 100644 --- a/game/components.py +++ b/game/components.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Final, Self +from typing import Final, Self, Callable import attrs import math @@ -65,6 +65,8 @@ class Graphic: ch: int = ord("!") fg: tuple[int, int, int] = (255, 255, 255) +Action: Final = ("Action", Callable[[tcod.ecs.Entity], None]) +"""Possible action.""" Gold: Final = ("Gold", int) """Amount of gold.""" diff --git a/game/screens/game_screens.py b/game/screens/game_screens.py index 5144aea..2633fff 100644 --- a/game/screens/game_screens.py +++ b/game/screens/game_screens.py @@ -10,10 +10,10 @@ from tcod.event import KeySym from tcod.map import Map import g -from game.components import Gold, Graphic, Position +from game.components import Action, Gold, Graphic, Position from game.constants import DIRECTION_KEYS, ACTION_KEYS from game.screens import Push, Screen, ScreenResult -from game.tags import IsItem, IsPlayer, IsDoor, IsActor +from game.tags import IsItem, IsPlayer, IsActor from game.constants import WALL_CHAR from game.screens import menu_screens from game.world_tools import world_pos_to_map_pos, map_pos_to_world_pos @@ -28,10 +28,10 @@ class MainScreen(Screen): map: Map = g.world[None].components[Map] match event: case tcod.event.KeyDown(sym=sym) if sym in ACTION_KEYS: - for door in g.world.Q.all_of(tags=[IsDoor]): + for entity in g.world.Q.all_of(components=[Action, Position]): player_pos = player.components[Position] - if (player_pos - door.components[Position]).length() < 2: - ACTION_KEYS[sym](door) + if (player_pos - entity.components[Position]).length() < 2: + entity.components[Action](entity) cam_map = world_pos_to_map_pos(player_pos) map.compute_fov(cam_map.x, cam_map.y, 100) diff --git a/game/tags.py b/game/tags.py index bfbbe7c..00f8a0d 100644 --- a/game/tags.py +++ b/game/tags.py @@ -12,6 +12,3 @@ IsActor: Final = "IsActor" IsItem: Final = "IsItem" """Entity is an item.""" - -IsDoor: Final = "IsDoor" -"""Entiy is a door.""" diff --git a/game/world_tools.py b/game/world_tools.py index 7e0e844..10b1a68 100644 --- a/game/world_tools.py +++ b/game/world_tools.py @@ -9,8 +9,8 @@ from tcod.map import Map import g -from game.components import Gold, Graphic, Position -from game.tags import IsActor, IsItem, IsPlayer, IsDoor +from game.components import Action, Gold, Graphic, Position +from game.tags import IsActor, IsItem, IsPlayer world_center: Final = Position(50, 50) @@ -30,9 +30,9 @@ def add_door(world, pos): world.new_entity( components={ Position: pos, - Graphic: Graphic(ord('\\')) - }, - tags=[IsDoor] + Graphic: Graphic(ord('\\')), + Action: unlock_door + } ) add_wall(world, pos)