pyrogue/game/states.py

114 lines
4.4 KiB
Python

"""A collection of game states."""
from __future__ import annotations
from functools import reduce
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
from game.constants import WALL_CHAR
@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."""
centers = g.world.Q.all_of(tags=[IsPlayer])
centers = [a.components[Position] for a in centers]
center = sum(centers, start=Position(0,0))
center = center.mod(len(centers))
def draw(e_pos, e_graph):
pos = e_pos - center
if (-console.width//2 <= pos.x < console.width//2\
and -console.height//2 <= pos.y < console.height//2):
graphic = e_graph
console.rgb[["ch", "fg"]][pos.y + console.height//2, pos.x + console.width//2] = graphic.ch, graphic.fg
for (y, row) in enumerate(g.world_map.walkable):
for (x, val) in enumerate(row):
if val:
draw(Position(x,y), Graphic(WALL_CHAR))
for entity in g.world.Q.all_of(components=[Position, Graphic]).none_of(tags=[IsActor]):
draw(entity.components[Position], entity.components[Graphic])
for actor in g.world.Q.all_of(components=[Position, Graphic], tags=[IsActor]).none_of(tags=[IsPlayer]):
draw(actor.components[Position], entity.components[Graphic])
for player in g.world.Q.all_of(tags=[IsPlayer]):
draw(player.components[Position], player.components[Graphic])
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()