125 lines
2.5 KiB
C#
125 lines
2.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
public enum GameState
|
|
{
|
|
None,
|
|
Started,
|
|
Ended,
|
|
}
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public static GameManager Instance { get; private set; }
|
|
|
|
private EnergyController _energyController;
|
|
private vTimerCounter _timerCounter;
|
|
private EndGameModule _endGameModule;
|
|
|
|
private bool _successEnd;
|
|
|
|
public UnityEvent OnGameStarted;
|
|
public UnityEvent OnGameEnded;
|
|
|
|
|
|
[SerializeField]
|
|
private UITextShow _UITextShow;
|
|
|
|
public GameState CurrentGameState = GameState.None;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
if (!Instance)
|
|
{
|
|
Instance = this;
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
InitReferences();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
_timerCounter.OnTimeEnded.AddListener(OnEndTime);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_timerCounter.OnTimeEnded.AddListener(OnEndTime);
|
|
}
|
|
|
|
private void InitReferences()
|
|
{
|
|
_endGameModule = FindObjectOfType<EndGameModule>();
|
|
_timerCounter = FindObjectOfType<vTimerCounter>();
|
|
}
|
|
|
|
public void ChangeState(GameState newState)
|
|
{
|
|
if (CurrentGameState == newState)
|
|
return;
|
|
|
|
CurrentGameState = newState;
|
|
|
|
Debug.Log(newState);
|
|
|
|
switch (CurrentGameState)
|
|
{
|
|
case GameState.Started:
|
|
{
|
|
PlayerSetup.Instance.CreateNewPlayer();
|
|
|
|
_UITextShow?.ShowTimer
|
|
(
|
|
3,
|
|
"ÑÒÀÐÒ!",
|
|
1,
|
|
null,
|
|
() => { OnGameStarted?.Invoke(); }
|
|
);
|
|
}
|
|
break;
|
|
case GameState.Ended:
|
|
{
|
|
OnGameEnded?.Invoke();
|
|
_endGameModule.ShowEndMenu(_successEnd);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private bool _startedOnce = false;
|
|
|
|
[ContextMenu("Debug Start")]
|
|
public void OnStartGame()
|
|
{
|
|
if (_startedOnce)
|
|
return;
|
|
|
|
_startedOnce = true;
|
|
ChangeState(GameState.Started);
|
|
}
|
|
|
|
public void OnEndTime()
|
|
{
|
|
_successEnd = false;
|
|
ChangeState(GameState.Ended);
|
|
}
|
|
|
|
public void OnFinish()
|
|
{
|
|
_successEnd = true;
|
|
ChangeState(GameState.Ended);
|
|
}
|
|
}
|