rabidus-test/Assets/Scripts/GameManager.cs

128 lines
2.8 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; }
2023-07-28 11:48:57 +03:00
private EnergyController _energyController;
2023-08-15 17:38:54 +03:00
private vTimerCounter _timerCounter;
private EndGameModule _endGameModule;
private LeaderboardController _leaderboardController;
private bool _successEnd;
2023-07-24 16:38:13 +03:00
2023-07-26 14:13:07 +03:00
public UnityEvent OnGameStarted;
public UnityEvent OnGameEnded;
2023-08-15 17:38:54 +03:00
[SerializeField]
private UITextShow _UITextShow;
2023-07-26 14:13:07 +03:00
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();
}
2023-08-15 17:38:54 +03:00
private void OnEnable()
{
_timerCounter.OnTimeEnded.AddListener(OnEndTime);
}
private void OnDisable()
{
_timerCounter.OnTimeEnded.AddListener(OnEndTime);
}
2023-07-24 16:38:13 +03:00
private void InitReferences()
{
2023-08-15 17:38:54 +03:00
_leaderboardController = FindObjectOfType<LeaderboardController>();
_endGameModule = FindObjectOfType<EndGameModule>();
_timerCounter = FindObjectOfType<vTimerCounter>();
2023-07-24 16:38:13 +03:00
}
public void ChangeState(GameState newState)
{
2023-08-15 17:38:54 +03:00
if (CurrentGameState == newState)
return;
2023-07-24 16:38:13 +03:00
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.Started:
{
2023-08-15 17:38:54 +03:00
var player = PlayerSetup.Instance.CreateNewPlayer();
_leaderboardController.InitPlayerInfo(player);
_UITextShow?.ShowTimer
(
3,
"<22><><EFBFBD><EFBFBD><EFBFBD>!",
0,
null,
() => { OnGameStarted?.Invoke(); }
);
2023-07-24 16:38:13 +03:00
}
break;
case GameState.Ended:
{
2023-07-26 14:13:07 +03:00
OnGameEnded?.Invoke();
2023-08-15 17:38:54 +03:00
_endGameModule.ShowEndMenu(_successEnd);
2023-07-24 16:38:13 +03:00
}
break;
}
}
2023-08-15 17:38:54 +03:00
private bool _startedOnce = false;
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
{
2023-08-15 17:38:54 +03:00
if (_startedOnce)
return;
_startedOnce = true;
2023-07-24 16:38:13 +03:00
ChangeState(GameState.Started);
}
2023-08-15 17:38:54 +03:00
public void OnEndTime()
{
_successEnd = false;
ChangeState(GameState.Ended);
}
public void OnFinish()
{
_successEnd = true;
ChangeState(GameState.Ended);
}
2023-07-24 16:38:13 +03:00
}