rabidus-test/Assets/Scripts/GameManager.cs

107 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 PlayerSetup _playerSetup;
private CoinCounter _coinCounter;
private GameTimeController _timeController;
private vTimerCounter _timerCounter;
private UIController _UIController;
[SerializeField]
private Button _startButton;
[SerializeField]
private TMPro.TMP_InputField _inputField;
public event UnityAction OnGameStarted;
public GameState CurrentGameState = GameState.None;
private void OnEnable()
{
_startButton.onClick.AddListener(OnStartGame);
}
private void OnDisable()
{
_startButton.onClick.RemoveListener(OnStartGame);
}
private void Awake()
{
if (!Instance)
{
Instance = this;
}
else
{
Destroy(gameObject);
return;
}
InitReferences();
}
private void InitReferences()
{
_playerSetup = FindObjectOfType<PlayerSetup>();
_coinCounter = FindObjectOfType<CoinCounter>();
_timeController = FindObjectOfType<GameTimeController>();
_timerCounter = FindObjectOfType<vTimerCounter>();
_UIController = FindObjectOfType<UIController>();
}
private void Start()
{
_inputField.text = $"Player_{Random.Range(0, 1000)}";
}
public void ChangeState(GameState newState)
{
CurrentGameState = newState;
switch (CurrentGameState)
{
case GameState.None:
{
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
}
break;
case GameState.Started:
{
_playerSetup.CreateNewPlayer(_inputField.text);
_timeController.ChangeTime(1);
_timerCounter.StartTimer();
_UIController.HideMenu();
}
break;
case GameState.Ended:
{
_playerSetup.AddScore(_coinCounter.CoinCount);
_UIController.ShowMenu();
ChangeState(GameState.None);
}
break;
}
}
private void OnStartGame()
{
ChangeState(GameState.Started);
}
}