using System.Collections.Generic; using UnityEngine; #if ENABLE_INPUT_SYSTEM using UnityEngine.InputSystem; #endif /// /// Switches the game between high-level modes (Player Control, Paint). The switch /// key (Tab) cycles to the next registered mode. Register additional modes in /// . Attach to a single, persistent scene GameObject. /// [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; [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; private readonly List _modes = new List(); private int _activeIndex = -1; /// The currently active mode (null before startup). public GameMode ActiveMode => (_activeIndex >= 0 && _activeIndex < _modes.Count) ? _modes[_activeIndex] : null; private void Awake() { if (!player) player = FindFirstObjectByType(); if (!cameraInput) cameraInput = FindFirstObjectByType(); if (!painter) painter = FindFirstObjectByType(); BuildModes(); } private void Start() { if (_modes.Count > 0) SwitchTo(Mathf.Clamp(startModeIndex, 0, _modes.Count - 1)); } /// Registers all available modes. Add new modes here. private void BuildModes() { _modes.Clear(); _modes.Add(new PlayerControlMode()); _modes.Add(new PaintMode()); foreach (var m in _modes) m.Bind(this); } private void Update() { if (_modes.Count == 0) return; if (WasSwitchPressed()) SwitchTo((_activeIndex + 1) % _modes.Count); ActiveMode?.Tick(); } /// Activates the mode at (Exit old, Enter new). 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); } private bool WasSwitchPressed() { #if ENABLE_INPUT_SYSTEM return Keyboard.current != null && Keyboard.current[switchKey].wasPressedThisFrame; #else return Input.GetKeyDown(switchKey); #endif } }