extracted states to own files

This commit is contained in:
Lukas Nöllemeyer 2024-08-17 10:05:44 +02:00
parent 8f7a15e17c
commit 72ec02dbae
3 changed files with 51 additions and 44 deletions

View file

@ -1,7 +1,4 @@
"""A collection of game states."""
from __future__ import annotations from __future__ import annotations
from functools import reduce
import attrs import attrs
import tcod.console import tcod.console
@ -10,13 +7,14 @@ import tcod.event
from tcod.event import KeySym from tcod.event import KeySym
import g import g
import game.menus
import game.world_tools import game.world_tools
from game.components import Gold, Graphic, Position from game.components import Gold, Graphic, Position
from game.constants import DIRECTION_KEYS, ACTION_KEYS from game.constants import DIRECTION_KEYS, ACTION_KEYS
from game.state import Push, Reset, State, StateResult from game.state import Push, State, StateResult
from game.tags import IsItem, IsPlayer, IsDoor, IsActor from game.tags import IsItem, IsPlayer, IsDoor, IsActor
from game.constants import WALL_CHAR from game.constants import WALL_CHAR
from game.states import MainMenu
@attrs.define() @attrs.define()
class InGame(State): class InGame(State):
@ -74,41 +72,3 @@ class InGame(State):
draw(player.components[Position], player.components[Graphic]) draw(player.components[Position], player.components[Graphic])
if text := g.world[None].components.get(("Text", str)): 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)) 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()

45
game/states/MainMenu.py Normal file
View file

@ -0,0 +1,45 @@
"""The main menu state"""
from __future__ import annotations
import g
import game.menus
import game.world_tools
from game.state import Reset, StateResult
from game.states.InGame import InGame
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()

View file

@ -11,6 +11,8 @@ import g
import game.state_tools import game.state_tools
import game.states import game.states
from game.states.MainMenu import MainMenu
def main() -> None: def main() -> None:
"""Entry point function.""" """Entry point function."""
tileset = tcod.tileset.load_tilesheet( tileset = tcod.tileset.load_tilesheet(
@ -18,7 +20,7 @@ def main() -> None:
) )
tcod.tileset.procedural_block_elements(tileset=tileset) tcod.tileset.procedural_block_elements(tileset=tileset)
g.states = [game.states.MainMenu()] g.states = [MainMenu()]
g.console = tcod.console.Console(80, 50) g.console = tcod.console.Console(80, 50)
with tcod.context.new(console=g.console, tileset=tileset) as g.context: with tcod.context.new(console=g.console, tileset=tileset) as g.context:
game.state_tools.main_loop() game.state_tools.main_loop()