| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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<float> OnTimeSpeedChanged;
- public static event Action<bool> 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";
- }
- }
|