(Underdepths) Pass 7
This commit is contained in:
1 parent
13512b0476
commit
952ff4b734
39 files changed
+7988
No files matched your search
@@ -0,0 +1,89 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from data.constants import FINAL_FLOOR, MESSAGE_LOG_LIMIT
|
||||
from engine.fov import compute_fov
|
||||
from engine.map_gen import GameMap, generate_map
|
||||
from entities.item import Item
|
||||
from entities.player import Player
|
||||
|
||||
STATE_PLAYING = "playing"
|
||||
STATE_DEAD = "dead"
|
||||
STATE_VICTORY = "victory"
|
||||
STATE_MENU = "menu"
|
||||
STATE_SAVED = "saved"
|
||||
|
||||
OVERLAY_HELP = "help"
|
||||
OVERLAY_INVENTORY = "inventory"
|
||||
OVERLAY_EQUIPMENT = "equipment"
|
||||
OVERLAY_QUAFF = "quaff"
|
||||
OVERLAY_READ = "read"
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class GameSession:
|
||||
"""Single source of truth for all mutable state; the unit of save/load."""
|
||||
|
||||
player: Player
|
||||
dungeon: list[GameMap]
|
||||
current_floor: int = 0
|
||||
log: list[str] = field(default_factory=list)
|
||||
turn: int = 0
|
||||
state: str = STATE_PLAYING
|
||||
overlay: str | None = None
|
||||
overlay_index: int = 0
|
||||
menu_index: int = 0
|
||||
save_available: bool = False
|
||||
|
||||
@property
|
||||
def current_map(self) -> GameMap:
|
||||
return self.dungeon[self.current_floor]
|
||||
|
||||
def log_message(self, message: str) -> None:
|
||||
self.log.append(message)
|
||||
del self.log[:-MESSAGE_LOG_LIMIT]
|
||||
|
||||
def refresh_fov(self) -> None:
|
||||
compute_fov(self.current_map, self.player.position)
|
||||
|
||||
def context_prompt(self) -> str | None:
|
||||
"""The contextual key hint: pickup while on an item, descent on stairs."""
|
||||
if self.state != STATE_PLAYING or not self.dungeon:
|
||||
return None
|
||||
position = self.player.position
|
||||
game_map = self.current_map
|
||||
for entity in game_map.entities:
|
||||
if isinstance(entity, Item) and entity.position == position:
|
||||
return f"Press G to pick up the {entity.name}."
|
||||
if game_map.stairs_down == position:
|
||||
return "Press F to descend."
|
||||
return None
|
||||
|
||||
def descend(self) -> bool:
|
||||
game_map = self.current_map
|
||||
if game_map.stairs_down is None or self.player.position != game_map.stairs_down:
|
||||
return False
|
||||
self.current_floor += 1
|
||||
self.player.x, self.player.y = self.current_map.spawn
|
||||
self.refresh_fov()
|
||||
self.log_message(f"You descend to floor {self.current_floor + 1}.")
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def for_menu(cls, save_available: bool = False) -> GameSession:
|
||||
"""A menu-only session; a real dungeon arrives with the next run."""
|
||||
return cls(player=Player(), dungeon=[], state=STATE_MENU, save_available=save_available)
|
||||
|
||||
|
||||
def new_game(seed: int | None = None) -> GameSession:
|
||||
dungeon = [
|
||||
generate_map(floor, seed=None if seed is None else seed + floor)
|
||||
for floor in range(FINAL_FLOOR + 1)
|
||||
]
|
||||
spawn = dungeon[0].spawn
|
||||
session = GameSession(player=Player(x=spawn[0], y=spawn[1]), dungeon=dungeon)
|
||||
session.overlay = OVERLAY_HELP # How to Play screens the very first turn
|
||||
session.log_message("You descend into the Underdepths. Slay the dragon below.")
|
||||
session.refresh_fov()
|
||||
return session
|
||||
Reference in new issue
Block a user