45 lines
2.2 KiB
C#
45 lines
2.2 KiB
C#
using UnityEngine;
|
|
namespace FlappyBird.Core
|
|
{
|
|
/// <summary>
|
|
/// Pure C# service — central authority for all game-state transitions.
|
|
/// No MonoBehaviour, no Unity lifecycle. Injected via Zenject.
|
|
///
|
|
/// Transition side-effects:
|
|
/// Playing -> Time.timeScale = 1 (unfreeze)
|
|
/// GameOver -> Time.timeScale = 0 (freeze everything)
|
|
/// Menu -> Time.timeScale = 0 (frozen until game starts)
|
|
/// </summary>
|
|
public sealed class GameStateManager : IGameStateManager
|
|
{
|
|
// ── IGameStateManager ──────────────────────────────────────────────
|
|
/// <inheritdoc/>
|
|
public GameState CurrentState { get; private set; } = GameState.Menu;
|
|
/// <inheritdoc/>
|
|
public event System.Action<GameState> OnStateChanged;
|
|
// ── Constructor ────────────────────────────────────────────────────
|
|
public GameStateManager()
|
|
{
|
|
// Start frozen; gameplay begins only when ChangeState(Playing) is called.
|
|
Time.timeScale = 0f;
|
|
}
|
|
// ── IGameStateManager ──────────────────────────────────────────────
|
|
/// <inheritdoc/>
|
|
public void ChangeState(GameState newState)
|
|
{
|
|
if (CurrentState == newState) return;
|
|
CurrentState = newState;
|
|
ApplyTimeScale(newState);
|
|
OnStateChanged?.Invoke(newState);
|
|
}
|
|
// ── Private helpers ────────────────────────────────────────────────
|
|
/// <summary>
|
|
/// Only Playing runs at normal speed.
|
|
/// Menu and GameOver both freeze Time so no physics or Update logic runs.
|
|
/// </summary>
|
|
private static void ApplyTimeScale(GameState state)
|
|
{
|
|
Time.timeScale = state == GameState.Playing ? 1f : 0f;
|
|
}
|
|
}
|
|
} |