Files
Project-M/Assets/_Project/Scripts/Client/UI/MainMenuController.cs
T
Luis Gonzalez 29e90a5008 First-run onboarding: contextual coach-marks + How-to-Play card + dev replay toggle
Teaches the deep, interlocking loop — especially the inverted win condition
(you win by CLEARING EXPEDITIONS, not by surviving base sieges; DR-042/DR-043).

- OnboardingSystem: client-only observe-only PresentationSystemGroup overlay
  (own UIDocument @ sortingOrder 60), soft-gated 10-beat coach-mark sequence
  with a world-space ▶ pointer; never mutates sim / never destroys a ghost.
- OnboardingStepMath: pure, unit-tested step machine (snapshot + IsSatisfied +
  scheme-aware prompts + pointer kinds + persisted-mask helpers).
- HowToPlayPanel: tabbed reference card (Controls / The Loop / Build / Threats /
  Win-Lose), reachable from the main menu and the pause overlay.
- Per-client client-local state in GameSettings (TutorialHints + OnboardingMask
  bitmask, additive) — a Join client keeps its own; a host save-wipe never
  re-teaches. Settings toggle + menu "Replay Tutorial".
- Dev "Force Each Launch" toggle (GameSettings.ForceOnboardingEachLaunch):
  SettingsService.Boot wipes the mask + forces hints on in-memory every launch
  so the tutorial always replays fresh.
- HudSystem suppresses its own location hint while onboarding is active
  (single prompt voice), via OnboardingState + [UpdateAfter(OnboardingSystem)].

Validated green: 20/20 EditMode; Play smoke confirmed overlay render, clean
U+25B6 pointer glyph, no system sort-cycle, and the force-wipe end-to-end.

Docs: DR-043 + session log; reusable lesson archived in the build-gotchas note.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-29 14:18:22 -07:00

154 lines
5.8 KiB
C#

using ProjectM.Simulation;
using Unity.Entities;
using UnityEngine;
using UnityEngine.UIElements;
namespace ProjectM.Client
{
/// <summary>
/// Drives the front-end main menu (UI Toolkit). Lives on a GameObject (with a <c>UIDocument</c>) in
/// MainMenu.unity — build index 0. On <see cref="Awake"/> it ENSURES a default "menu" world exists (the
/// bootstrap creates one on first boot, but on return-from-game <c>World.DisposeAllWorlds</c> left none and
/// <c>Initialize</c> does not re-run), ensures the EventSystem, and assigns the shared PanelSettings; on
/// enable it builds the menu. Single/Host/Join hand off to <see cref="WorldLauncher"/>.
/// </summary>
[RequireComponent(typeof(UIDocument))]
public class MainMenuController : MonoBehaviour
{
UIDocument _doc;
VisualElement _mainPanel;
VisualElement _settingsPanel;
VisualElement _howToPanel;
TextField _ipField;
Label _classLabel;
void Awake()
{
EnsureMenuWorld();
MenuUi.EnsureEventSystem();
_doc = GetComponent<UIDocument>();
if (_doc.panelSettings == null)
_doc.panelSettings = MenuUi.LoadPanelSettings();
// The menu owns the cursor.
UnityEngine.Cursor.lockState = CursorLockMode.None;
UnityEngine.Cursor.visible = true;
}
void OnEnable()
{
if (_doc == null) _doc = GetComponent<UIDocument>();
var root = _doc.rootVisualElement;
if (root == null) return;
root.Clear();
BuildMain(root);
}
static void EnsureMenuWorld()
{
var w = World.DefaultGameObjectInjectionWorld;
if (w == null || !w.IsCreated)
DefaultWorldInitialization.Initialize("MenuWorld", false);
}
void BuildMain(VisualElement root)
{
_mainPanel = MenuUi.FullScreenRoot(true);
var card = MenuUi.Card("PROJECT M");
card.Add(MenuUi.Caption("Frontier colony — co-op"));
// Slice 2: class picker -> sets WorldLauncher.SelectedClass for the next session (Warrior melee / Ranger ranged).
_classLabel = new Label(ClassName(WorldLauncher.SelectedClass));
_classLabel.style.unityTextAlign = TextAnchor.MiddleCenter;
_classLabel.style.marginTop = 6; _classLabel.style.marginBottom = 2;
card.Add(_classLabel);
var classRow = new VisualElement();
classRow.style.flexDirection = FlexDirection.Row;
classRow.style.justifyContent = Justify.Center;
classRow.Add(MenuUi.Button("Warrior", () => SelectClass((byte)CharacterId.Warrior)));
classRow.Add(MenuUi.Button("Ranger", () => SelectClass((byte)CharacterId.Ranger)));
card.Add(classRow);
card.Add(MenuUi.Button("Single Player", () => Launch(SessionMode.Single, false)));
if (SaveService.HasSave())
card.Add(MenuUi.Button("Continue", () => Launch(SessionMode.Single, true)));
card.Add(MenuUi.Button("Host Co-op (LAN)", () => Launch(SessionMode.Host, SaveService.HasSave())));
_ipField = new TextField("Join IP") { value = "127.0.0.1" };
_ipField.style.marginTop = 8;
card.Add(_ipField);
card.Add(MenuUi.Button("Join", () => Launch(SessionMode.Join, false)));
card.Add(MenuUi.Button("Settings", ShowSettings));
card.Add(MenuUi.Button("How to Play", ShowHowToPlay));
// Re-arm the first-run coach-marks (clears the client-local completed-step mask). The next session
// replays them; the How-to-Play card stays available regardless.
Button replayBtn = null;
replayBtn = MenuUi.Button("Replay Tutorial", () =>
{
var s = SettingsService.Current;
s.OnboardingMask = 0;
s.TutorialHints = 1;
SettingsService.Save(s);
if (replayBtn != null) replayBtn.text = "Tutorial armed ✓";
});
card.Add(replayBtn);
card.Add(MenuUi.Button("Quit", Quit));
_mainPanel.Add(card);
root.Add(_mainPanel);
}
void Launch(SessionMode mode, bool loadSave)
{
string ip = _ipField != null ? _ipField.value : "127.0.0.1";
WorldLauncher.StartSession(mode, ip, loadSave);
}
void SelectClass(byte classId)
{
WorldLauncher.SelectedClass = classId;
if (_classLabel != null) _classLabel.text = ClassName(classId);
}
static string ClassName(byte classId) => classId == (byte)CharacterId.Ranger ? "CLASS: Ranger (ranged)" : "CLASS: Warrior (melee)";
void ShowSettings()
{
_mainPanel.style.display = DisplayStyle.None;
_settingsPanel = SettingsScreen.Build(HideSettings);
_doc.rootVisualElement.Add(_settingsPanel);
}
void HideSettings()
{
if (_settingsPanel != null) { _settingsPanel.RemoveFromHierarchy(); _settingsPanel = null; }
_mainPanel.style.display = DisplayStyle.Flex;
}
void ShowHowToPlay()
{
_mainPanel.style.display = DisplayStyle.None;
_howToPanel = HowToPlayPanel.Build(HideHowToPlay);
_doc.rootVisualElement.Add(_howToPanel);
}
void HideHowToPlay()
{
if (_howToPanel != null) { _howToPanel.RemoveFromHierarchy(); _howToPanel = null; }
_mainPanel.style.display = DisplayStyle.Flex;
}
static void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
}