(Underdepths) Pass 4
This commit is contained in:
29 files changed
+4888
No files matched your search
@@ -0,0 +1,130 @@
|
||||
"""GameSession: the single source of truth for all mutable game state.
|
||||
|
||||
It holds the player, all 10 floors, the message log, turn counter, and game
|
||||
state string. `new_game` builds a fresh dive; `recompute_fov` refreshes the
|
||||
player's sight and merges it into the explored memory.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from data import constants as C
|
||||
from engine import map_gen
|
||||
from engine.fov import compute_fov
|
||||
from entities.player import Player
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class GameSession:
|
||||
player: Player
|
||||
dungeon: list
|
||||
current_floor: int = 0
|
||||
log: list = field(default_factory=list)
|
||||
turn: int = 0
|
||||
state: str = "playing"
|
||||
rng: random.Random = field(default_factory=random.Random)
|
||||
|
||||
|
||||
def new_game(seed: int | None = None) -> GameSession:
|
||||
rng = random.Random(seed)
|
||||
# generate_dungeon draws from the global random module; seed it too so the
|
||||
# entire dive is deterministic from `seed`.
|
||||
random.seed(seed)
|
||||
|
||||
player = Player(0, 0)
|
||||
|
||||
dungeon = []
|
||||
for fi in range(C.FLOORS):
|
||||
floor = map_gen.generate_dungeon(fi)
|
||||
map_gen.populate(floor, fi, rng=rng)
|
||||
dungeon.append(floor)
|
||||
|
||||
# Player starts at the center of the first room on floor 0.
|
||||
spawn = dungeon[0].rooms[0].center
|
||||
if not map_gen.is_walkable(dungeon[0], *spawn):
|
||||
spawn = _first_walkable(dungeon[0])
|
||||
player.x, player.y = spawn
|
||||
|
||||
session = GameSession(
|
||||
player=player,
|
||||
dungeon=dungeon,
|
||||
current_floor=0,
|
||||
log=["You enter the Underdepths. Find the stairs and descend."],
|
||||
turn=0,
|
||||
state="playing",
|
||||
rng=rng,
|
||||
)
|
||||
recompute_fov(session)
|
||||
return session
|
||||
|
||||
|
||||
def recompute_fov(session: GameSession) -> None:
|
||||
floor = session.dungeon[session.current_floor]
|
||||
visible = compute_fov(floor, session.player.x, session.player.y, C.SIGHT_RADIUS)
|
||||
for (x, y) in visible:
|
||||
floor.explored[y][x] = True
|
||||
|
||||
|
||||
def _first_walkable(game_map) -> tuple[int, int]:
|
||||
for y in range(game_map.height):
|
||||
for x in range(game_map.width):
|
||||
if game_map.tiles[y][x] == 1:
|
||||
return (x, y)
|
||||
return (1, 1)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Persistence + permadeath
|
||||
# --------------------------------------------------------------------------- #
|
||||
_GAME_DIR: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
def _save_path(path: str) -> str:
|
||||
"""Resolve a save path relative to the game directory unless it is absolute."""
|
||||
return path if os.path.isabs(path) else os.path.join(_GAME_DIR, path)
|
||||
|
||||
|
||||
def save(session: GameSession, path: str = "save.json") -> str:
|
||||
"""Serialize the session to JSON in the game directory. Returns the file path."""
|
||||
from engine.serializers import session_to_dict
|
||||
full = _save_path(path)
|
||||
with open(full, "w", encoding="utf-8") as f:
|
||||
json.dump(session_to_dict(session), f)
|
||||
return full
|
||||
|
||||
|
||||
def load(path: str = "save.json") -> GameSession:
|
||||
"""Read a saved session JSON and rebuild a live GameSession (FOV recomputed)."""
|
||||
from engine.serializers import dict_to_session
|
||||
with open(_save_path(path), "r", encoding="utf-8") as f:
|
||||
return dict_to_session(json.load(f))
|
||||
|
||||
|
||||
def has_save(path: str = "save.json") -> bool:
|
||||
"""True when a save file exists (drives the menu's 'Continue' entry)."""
|
||||
return os.path.isfile(_save_path(path))
|
||||
|
||||
|
||||
def delete_save(path: str = "save.json") -> None:
|
||||
"""Remove the save file if present. Never raises."""
|
||||
full = _save_path(path)
|
||||
try:
|
||||
if os.path.isfile(full):
|
||||
os.remove(full)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def mark_dead(session: GameSession) -> None:
|
||||
"""End the run on player death: flag the state and delete the save (permadeath)."""
|
||||
session.state = "dead"
|
||||
delete_save()
|
||||
|
||||
|
||||
def mark_victory(session: GameSession) -> None:
|
||||
"""End the run on victory: flag the state and delete the save."""
|
||||
session.state = "victory"
|
||||
delete_save()
|
||||
Reference in new issue
Block a user