131 lines
3.2 KiB
C#
131 lines
3.2 KiB
C#
|
|
using Boxes;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using TMPro;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
[Header("Ссылки на UI")]
|
|
public TextMeshProUGUI timerText;
|
|
public TextMeshProUGUI boxesText;
|
|
public GameObject endGameScreen;
|
|
public TextMeshProUGUI finalScoreText;
|
|
|
|
private int _boxesCollected;
|
|
private float _timeAlive;
|
|
private bool _isPlaying = true;
|
|
private int _scoreMultiplier = 1;
|
|
|
|
public static GameManager Instance { get; private set; }
|
|
|
|
void Awake()
|
|
{
|
|
if (Instance == null) Instance = this;
|
|
else Destroy(gameObject);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
GameEvents.OnBoxCollected += OnBoxCollected;
|
|
GameEvents.OnPlayerDied += EndGame;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
GameEvents.OnBoxCollected -= OnBoxCollected;
|
|
GameEvents.OnPlayerDied -= EndGame;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
_scoreMultiplier = 1;
|
|
_timeAlive = 0f;
|
|
_isPlaying = true;
|
|
_boxesCollected = 0;
|
|
|
|
if (endGameScreen != null) endGameScreen.SetActive(false);
|
|
|
|
Time.timeScale = 1f;
|
|
UpdateUI();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!_isPlaying) return;
|
|
_timeAlive += Time.deltaTime;
|
|
|
|
UpdateUI();
|
|
}
|
|
|
|
private void OnBoxCollected(BoxData boxData)
|
|
{
|
|
if (!_isPlaying) return;
|
|
|
|
switch (boxData.effectType)
|
|
{
|
|
case BoxData.BoxEffectType.AddScore:
|
|
int addAmount = (int)boxData.effectValue * _scoreMultiplier;
|
|
for(int i = 0; i < addAmount; i++)
|
|
{
|
|
_boxesCollected++;
|
|
GameEvents.OnBoxAddedToTruck?.Invoke();
|
|
}
|
|
break;
|
|
|
|
case BoxData.BoxEffectType.LoseScore:
|
|
int loseAmount = (int)boxData.effectValue;
|
|
for(int i = 0; i < loseAmount; i++)
|
|
{
|
|
if (_boxesCollected > 0)
|
|
{
|
|
_boxesCollected--;
|
|
GameEvents.OnBoxLostFromTruck?.Invoke();
|
|
}
|
|
}
|
|
break;
|
|
|
|
case BoxData.BoxEffectType.SpecialFurniture:
|
|
|
|
Debug.Log("Собрана мебель!");
|
|
break;
|
|
}
|
|
|
|
UpdateUI();
|
|
}
|
|
|
|
public void SetScoreMultiplier(int multiplier)
|
|
{
|
|
_scoreMultiplier = multiplier;
|
|
}
|
|
|
|
void UpdateUI()
|
|
{
|
|
if (timerText != null)
|
|
{
|
|
timerText.text = $"Время: {Mathf.FloorToInt(_timeAlive / 60):00}:{Mathf.FloorToInt(_timeAlive % 60):00}";
|
|
}
|
|
if (boxesText != null)
|
|
{
|
|
boxesText.text = $"Собрано: {_boxesCollected}";
|
|
}
|
|
}
|
|
|
|
void EndGame()
|
|
{
|
|
if (!_isPlaying) return;
|
|
_isPlaying = false;
|
|
|
|
if (endGameScreen != null) endGameScreen.SetActive(true);
|
|
if (finalScoreText != null)
|
|
{
|
|
finalScoreText.text = $"Вы собрали: {_boxesCollected} коробок!";
|
|
}
|
|
}
|
|
|
|
public void RestartGame()
|
|
{
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
}
|