refactored state -> screen

This commit is contained in:
Lukas Nöllemeyer 2024-08-17 10:56:55 +02:00
parent 2560a4dcd9
commit bfabba3d82
7 changed files with 130 additions and 129 deletions

99
game/screens/__init__.py Normal file
View file

@ -0,0 +1,99 @@
"""Package for game state stuff."""
from __future__ import annotations
from typing import Protocol, TypeAlias
import attrs
import tcod.console
import tcod.event
import g
class Screen(Protocol):
"""An abstract game screen."""
__slots__ = ()
def on_event(self, event: tcod.event.Event) -> ScreenResult:
"""Called on events."""
def on_draw(self, console: tcod.console.Console) -> None:
"""Called when the screen is being drawn."""
@attrs.define()
class Push:
"""Push a new screen on top of the stack."""
screen: Screen
@attrs.define()
class Pop:
"""Remove the current screen from the stack."""
@attrs.define()
class Reset:
"""Replace the entire stack with a new screen."""
screen: Screen
ScreenResult: TypeAlias = "Push | Pop | Reset | None"
"""Union of screen results."""
def main_draw() -> None:
"""Render and present the active screen."""
if not g.screens:
return
g.console.clear()
g.screens[-1].on_draw(g.console)
g.context.present(g.console)
def apply_state_result(result: ScreenResult) -> None:
"""Apply a StateResult to `g.states`."""
match result:
case Push(screen=screen):
g.screens.append(screen)
case Pop():
g.screens.pop()
case Reset(screen=screen):
while g.screens:
apply_state_result(Pop())
apply_state_result(Push(screen))
case None:
pass
case _:
raise TypeError(result)
def main_loop() -> None:
"""Run the active state forever."""
while g.screens:
main_draw()
for event in tcod.event.wait():
tile_event = g.context.convert_event(event)
if g.screens:
apply_state_result(g.screens[-1].on_event(tile_event))
def get_previous_screen(screen: Screen) -> Screen | None:
"""Return the state before `state` in the stack if it exists."""
current_index = next(index for index, value in enumerate(g.screens) if value is screen)
return g.screens[current_index - 1] if current_index > 0 else None
def draw_previous_screens(screen: Screen, console: tcod.console.Console, dim: bool = True) -> None:
"""Draw previous states, optionally dimming all but the active state."""
prev_screen = get_previous_screen(screen)
if prev_screen is None:
return
prev_screen.on_draw(console)
if dim and screen is g.screens[-1]:
console.rgb["fg"] //= 4
console.rgb["bg"] //= 4

View file

@ -0,0 +1,75 @@
"""All states the are used in-game"""
from __future__ import annotations
import attrs
import tcod.console
import tcod.constants
import tcod.event
from tcod.event import KeySym
import g
from game.components import 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.constants import WALL_CHAR
from game.screens import menu_screens
@attrs.define()
class MainScreen(Screen):
"""Primary in-game state."""
def on_event(self, event: tcod.event.Event) -> ScreenResult:
"""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 not g.world_map.walkable[g.world_center.y+new_pos.y][g.world_center.x+new_pos.x]:
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(menu_screens())
case _:
return None
def on_draw(self, console: tcod.console.Console) -> None:
"""Draw the standard screen."""
centers = [a.components[Position] for a in g.world.Q.all_of(tags=[IsPlayer])]
camera_pos = sum(centers, start=Position(0,0))
camera_pos = camera_pos.mod(len(centers))
h = console.height//2
w = console.width//2
def draw(e_pos, e_graph):
screen_pos = e_pos - camera_pos
if (-w <= screen_pos.x < w\
and -h <= screen_pos.y < h):
graphic = e_graph
console.rgb[["ch", "fg"]][screen_pos.y + h, screen_pos.x + w] = graphic.ch, graphic.fg
for (y, row) in enumerate(g.world_map.walkable):
for (x, val) in enumerate(row):
if not val:
draw(Position(x,y)-g.world_center, 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))

View file

@ -0,0 +1,45 @@
"""The main menu state"""
from __future__ import annotations
import g
import game.screens.menus
import game.world_tools
from game.screens import Reset, ScreenResult
from game.screens.game_screens import MainScreen
class MainMenu(game.screens.menus.ListMenu):
"""Main/escape menu."""
__slots__ = ()
def __init__(self) -> None:
"""Initialize the main menu."""
items = [
game.screens.menus.SelectItem("New game", self.new_game),
game.screens.menus.SelectItem("Quit", self.quit),
]
if hasattr(g, "world"):
items.insert(0, game.screens.menus.SelectItem("Continue", self.continue_))
super().__init__(
items=tuple(items),
selected=0,
x=5,
y=5,
)
@staticmethod
def continue_() -> ScreenResult:
"""Return to the game."""
return Reset(MainScreen())
@staticmethod
def new_game() -> ScreenResult:
"""Begin a new game."""
g.world = game.world_tools.new_world()
return Reset(MainScreen())
@staticmethod
def quit() -> ScreenResult:
"""Close the program."""
raise SystemExit()

100
game/screens/menus.py Normal file
View file

@ -0,0 +1,100 @@
"""Menu UI classes."""
from __future__ import annotations
from collections.abc import Callable
from typing import Protocol
import attrs
import tcod.console
import tcod.event
from tcod.event import KeySym
from game.constants import DIRECTION_KEYS
from game.screens import Pop, Screen, ScreenResult, draw_previous_screens
class MenuItem(Protocol):
"""Menu item protocol."""
__slots__ = ()
def on_event(self, event: tcod.event.Event) -> ScreenResult:
"""Handle events passed to the menu item."""
def on_draw(self, console: tcod.console.Console, x: int, y: int, highlight: bool) -> None:
"""Draw is item at the given position."""
@attrs.define()
class SelectItem(MenuItem):
"""Clickable menu item."""
label: str
callback: Callable[[], ScreenResult]
def on_event(self, event: tcod.event.Event) -> ScreenResult:
"""Handle events selecting this item."""
match event:
case tcod.event.KeyDown(sym=sym) if sym in {KeySym.RETURN, KeySym.RETURN2, KeySym.KP_ENTER}:
return self.callback()
case tcod.event.MouseButtonUp(button=tcod.event.MouseButton.LEFT):
return self.callback()
case _:
return None
def on_draw(self, console: tcod.console.Console, x: int, y: int, highlight: bool) -> None:
"""Render this items label."""
console.print(x, y, self.label, fg=(255, 255, 255), bg=(64, 64, 64) if highlight else (0, 0, 0))
@attrs.define()
class ListMenu(Screen):
"""Simple list menu state."""
items: tuple[MenuItem, ...]
selected: int | None = 0
x: int = 0
y: int = 0
def on_event(self, event: tcod.event.Event) -> ScreenResult:
"""Handle events for menus."""
match event:
case tcod.event.Quit():
raise SystemExit()
case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS:
dx, dy = DIRECTION_KEYS[sym]
if dx != 0 or dy == 0:
return self.activate_selected(event)
if self.selected is not None:
self.selected += dy
self.selected %= len(self.items)
else:
self.selected = 0 if dy == 1 else len(self.items) - 1
return None
case tcod.event.MouseMotion(position=(_, y)):
y -= self.y
self.selected = y if 0 <= y < len(self.items) else None
return None
case tcod.event.KeyDown(sym=KeySym.ESCAPE):
return self.on_cancel()
case tcod.event.MouseButtonUp(button=tcod.event.MouseButton.RIGHT):
return self.on_cancel()
case _:
return self.activate_selected(event)
def activate_selected(self, event: tcod.event.Event) -> ScreenResult:
"""Call the selected menu items callback."""
if self.selected is not None:
return self.items[self.selected].on_event(event)
return None
def on_cancel(self) -> ScreenResult:
"""Handle escape or right click being pressed on menus."""
return Pop()
def on_draw(self, console: tcod.console.Console) -> None:
"""Render the menu."""
draw_previous_screens(self, console)
for i, item in enumerate(self.items):
item.on_draw(console, x=self.x, y=self.y + i, highlight=i == self.selected)