first commit

This commit is contained in:
Lukas Nöllemeyer 2024-08-16 17:20:07 +02:00
commit 85be45e2b8
18 changed files with 605 additions and 0 deletions

1
game/__init__.py Normal file
View file

@ -0,0 +1 @@
"""Game namespace package."""

45
game/components.py Normal file
View file

@ -0,0 +1,45 @@
"""Collection of common components."""
from __future__ import annotations
from typing import Final, Self
import attrs
import tcod.ecs.callbacks
from tcod.ecs import Entity
@attrs.define(frozen=True)
class Position:
"""An entities position."""
x: int
y: int
def __add__(self, direction: tuple[int, int]) -> Self:
"""Add a vector to this position."""
x, y = direction
return self.__class__(self.x + x, self.y + y)
@tcod.ecs.callbacks.register_component_changed(component=Position)
def on_position_changed(entity: Entity, old: Position | None, new: Position | None) -> None:
"""Mirror position components as a tag."""
if old == new:
return
if old is not None:
entity.tags.discard(old)
if new is not None:
entity.tags.add(new)
@attrs.define(frozen=True)
class Graphic:
"""An entities icon and color."""
ch: int = ord("!")
fg: tuple[int, int, int] = (255, 255, 255)
Gold: Final = ("Gold", int)
"""Amount of gold."""

36
game/constants.py Normal file
View file

@ -0,0 +1,36 @@
"""Global constants are stored here."""
from typing import Final
from tcod.event import KeySym
DIRECTION_KEYS: Final = {
# Arrow keys
KeySym.LEFT: (-1, 0),
KeySym.RIGHT: (1, 0),
KeySym.UP: (0, -1),
KeySym.DOWN: (0, 1),
# Arrow key diagonals
KeySym.HOME: (-1, -1),
KeySym.END: (-1, 1),
KeySym.PAGEUP: (1, -1),
KeySym.PAGEDOWN: (1, 1),
# Keypad
KeySym.KP_4: (-1, 0),
KeySym.KP_6: (1, 0),
KeySym.KP_8: (0, -1),
KeySym.KP_2: (0, 1),
KeySym.KP_7: (-1, -1),
KeySym.KP_1: (-1, 1),
KeySym.KP_9: (1, -1),
KeySym.KP_3: (1, 1),
# VI keys
KeySym.h: (-1, 0),
KeySym.l: (1, 0),
KeySym.k: (0, -1),
KeySym.j: (0, 1),
KeySym.y: (-1, -1),
KeySym.b: (-1, 1),
KeySym.u: (1, -1),
KeySym.n: (1, 1),
}

101
game/menus.py Normal file
View file

@ -0,0 +1,101 @@
"""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
import game.state_tools
from game.constants import DIRECTION_KEYS
from game.state import Pop, State, StateResult
class MenuItem(Protocol):
"""Menu item protocol."""
__slots__ = ()
def on_event(self, event: tcod.event.Event) -> StateResult:
"""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[[], StateResult]
def on_event(self, event: tcod.event.Event) -> StateResult:
"""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(State):
"""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) -> StateResult:
"""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) -> StateResult:
"""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) -> StateResult:
"""Handle escape or right click being pressed on menus."""
return Pop()
def on_draw(self, console: tcod.console.Console) -> None:
"""Render the menu."""
game.state_tools.draw_previous_state(self, console)
for i, item in enumerate(self.items):
item.on_draw(console, x=self.x, y=self.y + i, highlight=i == self.selected)

44
game/state.py Normal file
View file

@ -0,0 +1,44 @@
"""Base classes for states."""
from __future__ import annotations
from typing import Protocol, TypeAlias
import attrs
import tcod.console
import tcod.event
class State(Protocol):
"""An abstract game state."""
__slots__ = ()
def on_event(self, event: tcod.event.Event) -> StateResult:
"""Called on events."""
def on_draw(self, console: tcod.console.Console) -> None:
"""Called when the state is being drawn."""
@attrs.define()
class Push:
"""Push a new state on top of the stack."""
state: State
@attrs.define()
class Pop:
"""Remove the current state from the stack."""
@attrs.define()
class Reset:
"""Replace the entire stack with a new state."""
state: State
StateResult: TypeAlias = "Push | Pop | Reset | None"
"""Union of state results."""

61
game/state_tools.py Normal file
View file

@ -0,0 +1,61 @@
"""State handling functions."""
from __future__ import annotations
import tcod.console
import g
from game.state import Pop, Push, Reset, State, StateResult
def main_draw() -> None:
"""Render and present the active state."""
if not g.states:
return
g.console.clear()
g.states[-1].on_draw(g.console)
g.context.present(g.console)
def apply_state_result(result: StateResult) -> None:
"""Apply a StateResult to `g.states`."""
match result:
case Push(state=state):
g.states.append(state)
case Pop():
g.states.pop()
case Reset(state=state):
while g.states:
apply_state_result(Pop())
apply_state_result(Push(state))
case None:
pass
case _:
raise TypeError(result)
def main_loop() -> None:
"""Run the active state forever."""
while g.states:
main_draw()
for event in tcod.event.wait():
tile_event = g.context.convert_event(event)
if g.states:
apply_state_result(g.states[-1].on_event(tile_event))
def get_previous_state(state: State) -> State | None:
"""Return the state before `state` in the stack if it exists."""
current_index = next(index for index, value in enumerate(g.states) if value is state)
return g.states[current_index - 1] if current_index > 0 else None
def draw_previous_state(state: State, console: tcod.console.Console, dim: bool = True) -> None:
"""Draw previous states, optionally dimming all but the active state."""
prev_state = get_previous_state(state)
if prev_state is None:
return
prev_state.on_draw(console)
if dim and state is g.states[-1]:
console.rgb["fg"] //= 4
console.rgb["bg"] //= 4

92
game/states.py Normal file
View file

@ -0,0 +1,92 @@
"""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
from game.state import Push, Reset, State, StateResult
from game.tags import IsItem, IsPlayer
@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.Quit():
raise SystemExit()
case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS:
player.components[Position] += DIRECTION_KEYS[sym]
# 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."""
for entity in g.world.Q.all_of(components=[Position, Graphic]):
pos = entity.components[Position]
if not (0 <= pos.x < console.width and 0 <= pos.y < console.height):
continue
graphic = entity.components[Graphic]
console.rgb[["ch", "fg"]][pos.y, pos.x] = graphic.ch, graphic.fg
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()

14
game/tags.py Normal file
View file

@ -0,0 +1,14 @@
"""Collection of common tags."""
from __future__ import annotations
from typing import Final
IsPlayer: Final = "IsPlayer"
"""Entity is the player."""
IsActor: Final = "IsActor"
"""Entity is an actor."""
IsItem: Final = "IsItem"
"""Entity is an item."""

32
game/world_tools.py Normal file
View file

@ -0,0 +1,32 @@
"""Functions for working with worlds."""
from __future__ import annotations
from random import Random
from tcod.ecs import Registry
from game.components import Gold, Graphic, Position
from game.tags import IsActor, IsItem, IsPlayer
def new_world() -> Registry:
"""Return a freshly generated world."""
world = Registry()
rng = world[None].components[Random] = Random()
player = world[object()]
player.components[Position] = Position(5, 5)
player.components[Graphic] = Graphic(ord("@"))
player.components[Gold] = 0
player.tags |= {IsPlayer, IsActor}
for _ in range(10):
gold = world[object()]
gold.components[Position] = Position(rng.randint(0, 20), rng.randint(0, 20))
gold.components[Graphic] = Graphic(ord("$"), fg=(255, 255, 0))
gold.components[Gold] = rng.randint(1, 10)
gold.tags |= {IsItem}
return world