rabidus-test/Assets/Scripts/GameManager.cs

95 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 CoinCounter _coinCounter;
[SerializeField]
private Button _startButton;
[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>();
_coinCounter = FindObjectOfType<CoinCounter>();
}
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(_coinCounter.CoinCount);
OnGameEnded?.Invoke();
ChangeState(GameState.None);
}
break;
}
}
public void OnStartGame()
{
ChangeState(GameState.Started);
}
}