rabidus-test/Assets/Scripts/GameManager.cs

96 lines
2.0 KiB
C#
Raw Permalink 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-10-11 11:58:06 +03:00
public UnityEvent OnGameInit;
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 == null)
2023-07-24 16:38:13 +03:00
{
Instance = this;
}
else if (Instance != this)
2023-07-24 16:38:13 +03:00
{
Destroy(gameObject);
return;
}
InitReferences();
}
private void Start()
{
LeaderboardController.Instance.RefreshEntries();
}
2023-07-24 16:38:13 +03:00
private void InitReferences()
{
2023-10-11 11:58:06 +03:00
OnGameInit?.Invoke();
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;
2024-05-31 18:39:23 +03:00
Debug.Log($"Change GameState [{CurrentGameState}]=>[{newState}]", this);
2023-07-24 16:38:13 +03:00
CurrentGameState = newState;
2024-05-31 18:39:23 +03:00
2023-07-24 16:38:13 +03:00
switch (CurrentGameState)
{
case GameState.Started:
{
2023-09-04 12:48:24 +03:00
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-07-24 16:38:13 +03:00
}
break;
}
}
2023-08-15 17:38:54 +03:00
private bool _startedOnce = false;
2023-10-02 19:12:35 +03:00
public UITextShow TextShow;
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
{
2024-05-31 18:39:23 +03:00
Debug.Log($"Try start. Flag: {_startedOnce}", this);
2023-08-15 17:38:54 +03:00
if (_startedOnce)
return;
_startedOnce = true;
2024-05-31 18:39:23 +03:00
Debug.Log($"Try run timer", this);
TextShow.ShowTimer(3, "СТАРТ!", 1, null, () =>
{
Debug.Log($"End timer. Change game to start", this);
ChangeState(GameState.Started);
});
2023-07-24 16:38:13 +03:00
}
}