using UnityEngine; using UnityEngine.InputSystem; using System; public class TimeManager : MonoBehaviour { [Header("Time Settings")] [SerializeField] private float[] speedMultipliers = { 1f, 2f, 10f }; [SerializeField] private int currentSpeedIndex = 0; [SerializeField] private bool isPaused = false; public static event Action OnTimeSpeedChanged; public static event Action OnPauseStateChanged; public float CurrentTimeScale => isPaused ? 0f : speedMultipliers[currentSpeedIndex]; public bool IsPaused => isPaused; public int CurrentSpeedIndex => currentSpeedIndex; void Start() { ApplyTimeScale(); } void Update() { // Handle speed control inputs var keyboard = UnityEngine.InputSystem.Keyboard.current; if (keyboard.digit1Key.wasPressedThisFrame) { SetSpeed(0); } else if (keyboard.digit2Key.wasPressedThisFrame) { SetSpeed(1); } else if (keyboard.digit3Key.wasPressedThisFrame) { SetSpeed(2); } else if (keyboard.tabKey.wasPressedThisFrame) { CycleSpeed(); } } public void SetSpeed(int speedIndex) { if (speedIndex >= 0 && speedIndex < speedMultipliers.Length) { currentSpeedIndex = speedIndex; ApplyTimeScale(); OnTimeSpeedChanged?.Invoke(CurrentTimeScale); Debug.Log($"Game speed set to {speedMultipliers[currentSpeedIndex]}x"); } } public void CycleSpeed() { currentSpeedIndex = (currentSpeedIndex + 1) % speedMultipliers.Length; ApplyTimeScale(); OnTimeSpeedChanged?.Invoke(CurrentTimeScale); Debug.Log($"Game speed cycled to {speedMultipliers[currentSpeedIndex]}x"); } public void SetPaused(bool paused) { isPaused = paused; ApplyTimeScale(); OnPauseStateChanged?.Invoke(isPaused); Debug.Log($"Game {(isPaused ? "paused" : "unpaused")}"); } public void TogglePause() { SetPaused(!isPaused); } private void ApplyTimeScale() { Time.timeScale = CurrentTimeScale; } public float GetSpeedMultiplier() { return speedMultipliers[currentSpeedIndex]; } public string GetSpeedText() { if (isPaused) return "Paused"; return $"{speedMultipliers[currentSpeedIndex]}x"; } }