120 lines
2.4 KiB
C#
120 lines
2.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class KitchenGameManager : MonoBehaviour
|
|
{
|
|
public static KitchenGameManager Instance { get; private set; }
|
|
public event EventHandler OnStateChanged;
|
|
public event EventHandler OnGamePaused;
|
|
public event EventHandler OnGameUnpaused;
|
|
[SerializeField] private const float gamePlayingTimerMax = 120f;
|
|
|
|
private enum State
|
|
{
|
|
WaitingToStart,
|
|
CountdownToStart,
|
|
GamePlaying,
|
|
GameOver
|
|
}
|
|
|
|
private State state;
|
|
public float CountdownToStartTimer { get; private set; } = 3f;
|
|
public float GamePlayingTimer { get; private set; }
|
|
private bool isGamePaused;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
state = State.WaitingToStart;
|
|
GamePlayingTimer = gamePlayingTimerMax;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
GameInput.Instance.OnPauseAction += GameInput_OnPauseAction;
|
|
GameInput.Instance.OnInteractAction += GameInput_OnInteractAction;
|
|
}
|
|
|
|
private void GameInput_OnPauseAction(object sender, System.EventArgs e)
|
|
{
|
|
TogglePauseGame();
|
|
}
|
|
|
|
private void GameInput_OnInteractAction(object sender, System.EventArgs e)
|
|
{
|
|
if (state == State.WaitingToStart)
|
|
{
|
|
state = State.CountdownToStart;
|
|
OnStateChanged?.Invoke(this, System.EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public void TogglePauseGame()
|
|
{
|
|
isGamePaused = !isGamePaused;
|
|
if (isGamePaused)
|
|
{
|
|
Time.timeScale = 0f;
|
|
OnGamePaused?.Invoke(this, System.EventArgs.Empty);
|
|
}
|
|
else
|
|
{
|
|
Time.timeScale = 1f;
|
|
OnGameUnpaused?.Invoke(this, System.EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
switch (state)
|
|
{
|
|
case State.WaitingToStart:
|
|
break;
|
|
case State.CountdownToStart:
|
|
CountdownToStartTimer -= Time.deltaTime;
|
|
if (CountdownToStartTimer > 0f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
state = State.GamePlaying;
|
|
OnStateChanged?.Invoke(this, System.EventArgs.Empty);
|
|
break;
|
|
case State.GamePlaying:
|
|
GamePlayingTimer -= Time.deltaTime;
|
|
if (GamePlayingTimer > 0f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
state = State.GameOver;
|
|
GamePlayingTimer = gamePlayingTimerMax;
|
|
OnStateChanged?.Invoke(this, System.EventArgs.Empty);
|
|
break;
|
|
case State.GameOver:
|
|
break;
|
|
}
|
|
|
|
// Debug.Log(state);
|
|
}
|
|
|
|
public bool IsGamePlaying()
|
|
{
|
|
return state == State.GamePlaying;
|
|
}
|
|
|
|
public bool IsCountToStartActive()
|
|
{
|
|
return state == State.CountdownToStart;
|
|
}
|
|
|
|
public bool IsGameOver()
|
|
{
|
|
return state == State.GameOver;
|
|
}
|
|
|
|
public float GetGamePlayingTimerNormalized()
|
|
{
|
|
return 1 - (GamePlayingTimer / gamePlayingTimerMax);
|
|
}
|
|
} |