rabidus-test/Assets/Scripts/GameManager.cs

85 lines
1.8 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 PlayerSetup _playerSetup;
private EnergyController _energyController;
public UnityEvent OnGameStarted;
public UnityEvent OnGameEnded;
public GameState CurrentGameState = GameState.None;
private void Awake()
{
if (!Instance)
{
Instance = this;
}
else
{
Destroy(gameObject);
return;
}
InitReferences();
}
private void InitReferences()
{
_playerSetup = FindObjectOfType<PlayerSetup>();
_energyController = FindObjectOfType<EnergyController>();
}
public void ChangeState(GameState newState)
{
CurrentGameState = newState;
Debug.Log(newState);
switch (CurrentGameState)
{
case GameState.None:
{
SceneManager.LoadSceneAsync(1);
}
break;
case GameState.Started:
{
_playerSetup.CreateNewPlayer();
OnGameStarted?.Invoke();
}
break;
case GameState.Ended:
{
_playerSetup.SetPlayerScore(_energyController.Energy);
OnGameEnded?.Invoke();
ChangeState(GameState.None);
}
break;
}
}
[ContextMenu("Debug Start")]
public void OnStartGame()
{
ChangeState(GameState.Started);
}
}