Replace the connection-approval role selection with a server-authoritative match flow built on NGO additive scene management: - Scenes: Main is the persistent foundation (NetworkManager, GameModeManager, cameras, HUD); Lobby and Test Map load/unload additively. Add Lobby + Test Map to the build settings; remove the obsolete SampleScene. - MatchManager (plain MonoBehaviour singleton): loads Lobby on server start, StartMatchTrigger lets the host start the match, rolls one random Hunter (rest Hider), swaps Lobby -> Test Map (strictly serialized per NGO), respawns each player as its role variant. Forces additive client sync mode so clients keep Main instead of single-loading it (fixes "Destination scene is not valid"). - Server-driven spawning: null out NetworkConfig.PlayerPrefab at runtime to disable auto-spawn, then spawn players after the lobby floor exists (fixes falling through the floor) with double-spawn and transition guards. - StartMatchTrigger: server-gated OnTriggerEnter, only the host's own player starts the match. - NetworkBootstrap: drop role selection / connection approval; everyone spawns as the neutral Player prefab. Host/Client/Server/Shutdown buttons stay. - NetworkPlayerSetup.ScopedModes flag (baked on Hunter/Hider prefabs): lobby avatar gets the full mode set, match variants get the role-scoped set. - GameModeManager.BindLocalPlayer reads ScopedModes and rebinds the painter unconditionally (fixes stale painter binding on respawn -> brush painting in every mode). - MeshPainter.PaintingEnabled defaults to false (unbound/pre-spawn/remote never paints); PaintHud visibility now also gates on a bound painter (fixes the HUD showing at startup before any avatar exists). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
209 lines
8.7 KiB
C#
209 lines
8.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Unity.Cinemachine;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Switches the game between high-level modes. The switch key (Tab) cycles to the next registered
|
|
/// mode. The mode set is scoped to the local avatar's <see cref="PlayerRole"/> when it spawns
|
|
/// (Hunter: First Person + Paint; Hider: Player Control + Paint + Free Cam); a non-networked scene
|
|
/// uses the full set. Attach to a single, persistent scene GameObject.
|
|
/// </summary>
|
|
[DisallowMultipleComponent]
|
|
public class GameModeManager : MonoBehaviour
|
|
{
|
|
[Header("Controlled Systems")]
|
|
[Tooltip("Player movement controller. Empty = found automatically.")]
|
|
public PlayerCharacterController player;
|
|
|
|
[Tooltip("Third-person orbit camera input. Empty = found automatically.")]
|
|
public ThirdPersonOrbitInput cameraInput;
|
|
|
|
[Tooltip("Mesh painter. Empty = found automatically.")]
|
|
public MeshPainter painter;
|
|
|
|
[Tooltip("Free-fly spectator camera. Empty = found automatically. Absent = Free Cam is left out of the cycle.")]
|
|
public FreeCamController freeCam;
|
|
|
|
[Tooltip("First-person camera. Empty = found automatically. Absent = First Person is left out of the cycle.")]
|
|
public FirstPersonController firstPerson;
|
|
|
|
//[Tooltip("Insect / surface-walk controller. Empty = found automatically.")]
|
|
//public InsectController insect;
|
|
|
|
[Header("Switching")]
|
|
[Tooltip("Key that cycles to the next mode.")]
|
|
#if ENABLE_INPUT_SYSTEM
|
|
public Key switchKey = Key.Tab;
|
|
#else
|
|
public KeyCode switchKey = KeyCode.Tab;
|
|
#endif
|
|
|
|
[Tooltip("Mode active at startup (0 = Player Control).")]
|
|
public int startModeIndex = 0;
|
|
|
|
public PlayerCharacterController Player => player;
|
|
public ThirdPersonOrbitInput CameraInput => cameraInput;
|
|
public MeshPainter Painter => painter;
|
|
public FreeCamController FreeCam => freeCam;
|
|
public FirstPersonController FirstPerson => firstPerson;
|
|
//public InsectController Insect => insect;
|
|
|
|
/// <summary>Raised after a mode switch finishes (the new mode's Enter has already run).
|
|
/// Argument = the now-active mode. Used by UI (e.g. the paint HUD) to follow the mode.</summary>
|
|
public event System.Action<GameMode> ModeChanged;
|
|
|
|
/// <summary>Raised when the painter the HUD should edit changes - i.e. when the local
|
|
/// networked avatar spawns and <see cref="BindLocalPlayer"/> binds its per-avatar painter.
|
|
/// With spawn-on-connect there is no painter at scene start, so the paint HUD follows this
|
|
/// to (re)bind its widgets to the live painter once it exists.</summary>
|
|
public event System.Action<MeshPainter> PainterChanged;
|
|
|
|
private readonly List<GameMode> _modes = new List<GameMode>();
|
|
private int _activeIndex = -1;
|
|
|
|
/// <summary>The currently active mode (null before startup).</summary>
|
|
public GameMode ActiveMode =>
|
|
(_activeIndex >= 0 && _activeIndex < _modes.Count) ? _modes[_activeIndex] : null;
|
|
|
|
private void Awake()
|
|
{
|
|
if (!player) player = FindFirstObjectByType<PlayerCharacterController>();
|
|
if (!cameraInput) cameraInput = FindFirstObjectByType<ThirdPersonOrbitInput>();
|
|
if (!painter) painter = FindFirstObjectByType<MeshPainter>();
|
|
// Include inactive so a freecam rig that starts disabled is still found and wired.
|
|
if (!freeCam) freeCam = FindFirstObjectByType<FreeCamController>(FindObjectsInactive.Include);
|
|
if (!firstPerson) firstPerson = FindFirstObjectByType<FirstPersonController>(FindObjectsInactive.Include);
|
|
//if (!insect) insect = FindFirstObjectByType<InsectController>();
|
|
|
|
BuildModes();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (_modes.Count > 0)
|
|
SwitchTo(Mathf.Clamp(startModeIndex, 0, _modes.Count - 1));
|
|
}
|
|
|
|
/// <summary>Registers the modes for the current context. Without a role (no networked avatar yet,
|
|
/// or a non-networked scene) the full set is built from whichever rigs are present. With a role the
|
|
/// set is scoped to that side and ordered so index 0 is its intended start mode. Add new modes here.</summary>
|
|
private void BuildModes() => BuildModes(null);
|
|
|
|
private void BuildModes(PlayerRole? role)
|
|
{
|
|
_modes.Clear();
|
|
|
|
switch (role)
|
|
{
|
|
case PlayerRole.Hunter:
|
|
// Hunter: first person + paint. Index 0 (First Person, if its rig exists) is the start mode.
|
|
if (firstPerson) _modes.Add(new FirstPersonMode());
|
|
_modes.Add(new PaintMode());
|
|
break;
|
|
|
|
case PlayerRole.Hider:
|
|
// Hider: third person (player control) + paint + free cam. Index 0 (Player Control) is the start.
|
|
if (player) _modes.Add(new PlayerControlMode());
|
|
_modes.Add(new PaintMode());
|
|
if (freeCam) _modes.Add(new FreeCamMode());
|
|
break;
|
|
|
|
default:
|
|
// No role: legacy full set, gated on whichever rigs exist. Player Control only joins when a
|
|
// gravity player is present (player-less pre-spawn stays in Paint, not a dead frozen mode);
|
|
// Free Cam / First Person only when their rigs exist, so non-networked scenes are unchanged.
|
|
if (player) _modes.Add(new PlayerControlMode());
|
|
_modes.Add(new PaintMode());
|
|
if (freeCam) _modes.Add(new FreeCamMode());
|
|
if (firstPerson) _modes.Add(new FirstPersonMode());
|
|
break;
|
|
}
|
|
|
|
foreach (var m in _modes)
|
|
m.Bind(this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Binds the scene singletons (this manager, the orbit/first-person cameras, the painter) to the
|
|
/// locally-owned networked player after it spawns, then enters Player Control. Called from
|
|
/// <see cref="NetworkPlayerSetup"/> on the owning client. Before this runs the scene has no player,
|
|
/// so the cycle sits in a player-less mode (Paint); this promotes it once the avatar exists.
|
|
/// </summary>
|
|
public void BindLocalPlayer(PlayerCharacterController localPlayer)
|
|
{
|
|
if (!localPlayer) return;
|
|
player = localPlayer;
|
|
|
|
// The painter lives on the player (per-avatar), so bind the local one - UNCONDITIONALLY. On a
|
|
// lobby->match respawn the previous avatar is despawned and the role variant spawned in the same
|
|
// server frame; the old painter is Destroy-pending (still non-null) when this runs, so a
|
|
// `if (!painter)` guard would keep the stale reference and the new avatar's painter would never be
|
|
// mode-toggled - it would paint in every mode. Always re-point to the freshly spawned avatar.
|
|
painter = localPlayer.GetComponentInChildren<MeshPainter>(true);
|
|
PainterChanged?.Invoke(painter);
|
|
|
|
Transform camTarget = localPlayer.transform.Find("CameraTarget");
|
|
|
|
// Re-point the third-person orbit camera (Cinemachine Follow/LookAt) at the local avatar.
|
|
if (cameraInput)
|
|
{
|
|
var vcam = cameraInput.GetComponent<CinemachineCamera>();
|
|
if (vcam) { vcam.Follow = camTarget; vcam.LookAt = camTarget; }
|
|
}
|
|
|
|
if (firstPerson)
|
|
firstPerson.SetPlayer(localPlayer);
|
|
|
|
// Tear down the pre-spawn (player-less) mode and rebuild scoped to this avatar's role.
|
|
ActiveMode?.Exit();
|
|
_activeIndex = -1;
|
|
|
|
// Lobby/neutral avatar (base Player prefab) -> full mode set; match avatar (Hunter/Hider
|
|
// variant) -> only its role's modes. Baked per prefab, so no replicated-phase timing race.
|
|
var setup = localPlayer.GetComponent<NetworkPlayerSetup>();
|
|
if (setup && setup.ScopedModes)
|
|
BuildModes(setup.Role);
|
|
else
|
|
BuildModes(null);
|
|
|
|
// Role lists are ordered so index 0 is the start mode (Hunter -> First Person, Hider -> Player
|
|
// Control); the full lobby set starts at Player Control too.
|
|
SwitchTo(0);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_modes.Count == 0) return;
|
|
|
|
if (WasSwitchPressed())
|
|
SwitchTo((_activeIndex + 1) % _modes.Count);
|
|
|
|
ActiveMode?.Tick();
|
|
}
|
|
|
|
/// <summary>Activates the mode at <paramref name="index"/> (Exit old, Enter new).</summary>
|
|
public void SwitchTo(int index)
|
|
{
|
|
if (index < 0 || index >= _modes.Count || index == _activeIndex) return;
|
|
|
|
ActiveMode?.Exit();
|
|
_activeIndex = index;
|
|
ActiveMode.Enter();
|
|
|
|
Debug.Log($"[GameModeManager] Mode -> {ActiveMode.DisplayName}", this);
|
|
ModeChanged?.Invoke(ActiveMode);
|
|
}
|
|
|
|
private bool WasSwitchPressed()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
return Keyboard.current != null && Keyboard.current[switchKey].wasPressedThisFrame;
|
|
#else
|
|
return Input.GetKeyDown(switchKey);
|
|
#endif
|
|
}
|
|
}
|