first commit
This commit is contained in:
commit
85be45e2b8
18 changed files with 605 additions and 0 deletions
92
game/states.py
Normal file
92
game/states.py
Normal 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue