rabidus-test/Assets/Scripts/GameManager.cs

93 lines
2.0 KiB
C#
Raw Normal View History

2023-07-24 16:38:13 +03:00
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;
2023-07-28 11:48:57 +03:00
private EnergyController _energyController;
2023-07-24 16:38:13 +03:00
[SerializeField]
private TMPro.TMP_InputField _inputField;
2023-07-26 14:13:07 +03:00
public UnityEvent OnGameStarted;
public UnityEvent OnGameEnded;
public GameState CurrentGameState = GameState.None;
2023-07-24 16:38:13 +03:00
private void Awake()
{
if (!Instance)
{
Instance = this;
}
else
{
Destroy(gameObject);
return;
}
InitReferences();
}
private void InitReferences()
{
_playerSetup = FindObjectOfType<PlayerSetup>();
2023-07-28 11:48:57 +03:00
_energyController = FindObjectOfType<EnergyController>();
2023-07-24 16:38:13 +03:00
}
private void Start()
{
_inputField.text = $"Player_{Random.Range(0, 1000)}";
}
2023-07-28 11:48:57 +03:00
2023-07-24 16:38:13 +03:00
public void ChangeState(GameState newState)
{
CurrentGameState = newState;
2023-07-26 14:13:07 +03:00
Debug.Log(newState);
2023-07-24 16:38:13 +03:00
switch (CurrentGameState)
{
case GameState.None:
{
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
}
break;
case GameState.Started:
{
_playerSetup.CreateNewPlayer(_inputField.text);
2023-07-26 14:13:07 +03:00
OnGameStarted?.Invoke();
2023-07-24 16:38:13 +03:00
}
break;
case GameState.Ended:
{
2023-07-28 11:48:57 +03:00
_playerSetup.AddScore(_energyController.Energy);
2023-07-26 14:13:07 +03:00
OnGameEnded?.Invoke();
2023-07-24 16:38:13 +03:00
ChangeState(GameState.None);
}
break;
}
}
2023-07-28 11:48:57 +03:00
[ContextMenu("Debug Start")]
2023-07-26 14:13:07 +03:00
public void OnStartGame()
2023-07-24 16:38:13 +03:00
{
ChangeState(GameState.Started);
}
}