"""A collection of game states.""" from __future__ import annotations import attrs import tcod.console import tcod.constants import tcod.event from tcod.event import KeySym import g import game.menus import game.world_tools from game.components import Gold, Graphic, Position from game.constants import DIRECTION_KEYS, ACTION_KEYS from game.state import Push, Reset, State, StateResult from game.tags import IsItem, IsPlayer, IsWall, IsDoor, IsActor @attrs.define() class InGame(State): """Primary in-game state.""" def on_event(self, event: tcod.event.Event) -> StateResult: """Handle events for the in-game state.""" (player,) = g.world.Q.all_of(tags=[IsPlayer]) match event: case tcod.event.KeyDown(sym=sym) if sym in ACTION_KEYS: for door in g.world.Q.all_of(tags=[IsDoor]): if (player.components[Position] - door.components[Position]).length() < 2: ACTION_KEYS[sym](door) case tcod.event.Quit(): raise SystemExit() case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS: new_pos = player.components[Position] + DIRECTION_KEYS[sym] if g.world.Q.all_of(tags=[new_pos, IsWall]) or g.world.Q.all_of(tags=[new_pos, IsDoor]): return None player.components[Position] = new_pos # Auto pickup gold for gold in g.world.Q.all_of(components=[Gold], tags=[player.components[Position], IsItem]): player.components[Gold] += gold.components[Gold] text = f"Picked up {gold.components[Gold]}g, total: {player.components[Gold]}g" g.world[None].components[("Text", str)] = text gold.clear() return None case tcod.event.KeyDown(sym=KeySym.ESCAPE): return Push(MainMenu()) case _: return None def on_draw(self, console: tcod.console.Console) -> None: """Draw the standard screen.""" def draw(e): pos = e.components[Position] if (0 <= pos.x < console.width and 0 <= pos.y < console.height): graphic = e.components[Graphic] console.rgb[["ch", "fg"]][pos.y, pos.x] = graphic.ch, graphic.fg for entity in g.world.Q.all_of(components=[Position, Graphic]).none_of(tags=[IsActor]): draw(entity) for actor in g.world.Q.all_of(components=[Position, Graphic], tags=[IsActor]).none_of(tags=[IsPlayer]): draw(actor) for player in g.world.Q.all_of(tags=[IsPlayer]): draw(player) if text := g.world[None].components.get(("Text", str)): console.print(x=0, y=console.height - 1, string=text, fg=(255, 255, 255), bg=(0, 0, 0)) class MainMenu(game.menus.ListMenu): """Main/escape menu.""" __slots__ = () def __init__(self) -> None: """Initialize the main menu.""" items = [ game.menus.SelectItem("New game", self.new_game), game.menus.SelectItem("Quit", self.quit), ] if hasattr(g, "world"): items.insert(0, game.menus.SelectItem("Continue", self.continue_)) super().__init__( items=tuple(items), selected=0, x=5, y=5, ) @staticmethod def continue_() -> StateResult: """Return to the game.""" return Reset(InGame()) @staticmethod def new_game() -> StateResult: """Begin a new game.""" g.world = game.world_tools.new_world() return Reset(InGame()) @staticmethod def quit() -> StateResult: """Close the program.""" raise SystemExit()