rabidus-test/Assets/Scripts/GameManager.cs

93 lines
2.0 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;
[SerializeField]
private TMPro.TMP_InputField _inputField;
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>();
}
private void Start()
{
_inputField.text = $"Player_{Random.Range(0, 1000)}";
}
public void ChangeState(GameState newState)
{
CurrentGameState = newState;
Debug.Log(newState);
switch (CurrentGameState)
{
case GameState.None:
{
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
}
break;
case GameState.Started:
{
_playerSetup.CreateNewPlayer(_inputField.text);
OnGameStarted?.Invoke();
}
break;
case GameState.Ended:
{
_playerSetup.AddScore(_energyController.Energy);
OnGameEnded?.Invoke();
ChangeState(GameState.None);
}
break;
}
}
[ContextMenu("Debug Start")]
public void OnStartGame()
{
ChangeState(GameState.Started);
}
}