"""The main menu (Dillon, 2026-11-17: "make a main menu with the new game load game features as seen in most games"). Panda3D, before the base opens. Continue the save you played last New Game a new campaign, in a save of its own (the others are kept) Load Game every save, newest first: its name, the day, when it was saved Quit Saves live in SAVE_DIR, one file a campaign. The save from before the menu (campaignview.SAVE_FILE, also the 2D window's) is listed too, and played in place: it is never moved or deleted. """ from __future__ import annotations import datetime import json import random from pathlib import Path from . import campaign as K SAVE_DIR = Path.home() / "Project02 saves" # ── the saves (pure: no Panda) ── def saves(save_dir=SAVE_DIR, legacy=None): """Every save, newest first -> [dict(path, name, day, alive, saved: datetime, ok: loads)].""" paths = sorted(Path(save_dir).glob("*.json")) if Path(save_dir).is_dir() else [] if legacy is not None and Path(legacy).is_file(): paths.append(Path(legacy)) out = [] for p in paths: info = {"path": p, "name": p.stem, "saved": datetime.datetime.fromtimestamp(p.stat().st_mtime), "day": None, "alive": None, "ok": True} try: d = json.loads(p.read_text()) info["day"] = d["day"] info["alive"] = sum(1 for s in d["roster"] if not s.get("dead")) except (OSError, ValueError, KeyError, TypeError): info["ok"] = False out.append(info) return sorted(out, key=lambda i: i["saved"], reverse=True) def label(info): """One line in Load Game.""" when = info["saved"].strftime("%Y-%m-%d %H:%M") if not info["ok"]: return f"{info['name']} (can't be read) saved {when}" return f"{info['name']} day {info['day']}, {info['alive']} soldiers saved {when}" def new_slot(save_dir=SAVE_DIR): """A file name for a new campaign: Campaign 1.json, Campaign 2.json, ... (never one that exists).""" d = Path(save_dir) d.mkdir(parents=True, exist_ok=True) n = 1 while (d / f"Campaign {n}.json").exists(): n += 1 return d / f"Campaign {n}.json" def new_game(save_dir=SAVE_DIR, seed=None): """A new campaign, saved at once in a new slot -> (campaign, path).""" path = new_slot(save_dir) c = K.new_campaign(seed if seed is not None else random.randrange(10 ** 6)) K.save(c, path) return c, path # ── the screen ── class MainMenu: """The title and the buttons, over a dark backdrop. on_start(campaign, path): play it, saving there.""" def __init__(self, base, on_start, save_dir=SAVE_DIR, legacy=None, on_quit=None): from direct.gui.DirectGui import DirectButton, DirectFrame, DirectLabel self.base, self.on_start, self.save_dir, self.legacy = base, on_start, save_dir, legacy self.on_quit = on_quit or base.userExit base.setBackgroundColor(0.07, 0.075, 0.06) ar = base.getAspectRatio() self.root = DirectFrame(frameColor=(0.07, 0.075, 0.06, 1), frameSize=(-ar - 1, ar + 1, -1, 1)) # its own backdrop self.root.setName("main menu") DirectLabel(parent=self.root, text="PROJECT 02", scale=0.16, pos=(0, 0, 0.62), frameColor=(0, 0, 0, 0), text_fg=(0.92, 0.86, 0.66, 1)) DirectLabel(parent=self.root, text="FORT LEONARD WOOD - THE OZARK COUNTIES", scale=0.045, pos=(0, 0, 0.5), frameColor=(0, 0, 0, 0), text_fg=(0.7, 0.68, 0.6, 1)) style = dict(scale=0.065, pad=(0.6, 0.3), text_fg=(0.95, 0.92, 0.82, 1), frameColor=(0.24, 0.27, 0.16, 1), parent=self.root) found = [s for s in saves(save_dir, legacy) if s["ok"]] self.buttons = {} z = 0.22 for key, text, cmd, live in (("continue", "Continue", self.cont, bool(found)), ("new", "New Game", self.new, True), ("load", "Load Game", self.show_saves, bool(found)), ("quit", "Quit", self.on_quit, True)): b = DirectButton(text=text, pos=(0, 0, z), command=cmd, **style) b["frameSize"] = (-3.2, 3.2, -0.6, 1.0) if not live: b["state"] = "disabled" b["text_fg"] = (0.5, 0.5, 0.45, 1) self.buttons[key] = b z -= 0.16 if found: DirectLabel(parent=self.root, text=f"Last played: {label(found[0])}", scale=0.035, pos=(0, 0, z - 0.02), frameColor=(0, 0, 0, 0), text_fg=(0.7, 0.68, 0.6, 1)) self.list_root = None self.save_buttons = [] def cont(self): found = [s for s in saves(self.save_dir, self.legacy) if s["ok"]] if found: self.load(found[0]["path"]) def new(self): c, path = new_game(self.save_dir) self.start(c, path) def show_saves(self): """Load Game: every save, newest first; click one to play it.""" from direct.gui.DirectGui import DirectButton, DirectFrame, DirectLabel if self.list_root is not None: self.list_root.destroy() ar = self.base.getAspectRatio() self.list_root = DirectFrame(parent=self.root, frameColor=(0.05, 0.05, 0.045, 1), frameSize=(-ar + 0.2, ar - 0.2, -0.95, 0.42)) DirectLabel(parent=self.list_root, text="Load Game", scale=0.06, pos=(0, 0, 0.32), frameColor=(0, 0, 0, 0), text_fg=(0.92, 0.86, 0.66, 1)) self.save_buttons = [] z = 0.2 for info in saves(self.save_dir, self.legacy)[:9]: b = DirectButton(parent=self.list_root, text=label(info), scale=0.042, pos=(0, 0, z), text_fg=(0.95, 0.92, 0.82, 1) if info["ok"] else (0.55, 0.5, 0.45, 1), frameColor=(0.2, 0.22, 0.15, 1), pad=(0.4, 0.2), command=(lambda p=info["path"]: self.load(p)) if info["ok"] else None) b["frameSize"] = (-20, 20, -0.5, 1.0) self.save_buttons.append((info, b)) z -= 0.12 DirectButton(parent=self.list_root, text="Back", scale=0.05, pos=(0, 0, -0.85), pad=(0.4, 0.2), text_fg=(0.95, 0.92, 0.82, 1), frameColor=(0.24, 0.27, 0.16, 1), command=self.hide_saves) def hide_saves(self): if self.list_root is not None: self.list_root.destroy() self.list_root = None def load(self, path): self.start(K.load(path), Path(path)) def start(self, c, path): self.root.destroy() self.on_start(c, path)