106 lines
3.3 KiB
C#
106 lines
3.3 KiB
C#
using Code.Saves;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
using PrimeTween;
|
|
|
|
namespace Code.UI
|
|
{
|
|
public class GameplayController : MonoBehaviour
|
|
{
|
|
public static GameplayController Instance;
|
|
|
|
[SerializeField] private TMP_Text coinsText;
|
|
[SerializeField] private CanvasGroup deathPanelCanvasGroup;
|
|
[SerializeField] private RectTransform deathPanelWindow;
|
|
[SerializeField] private TMP_Text finalResultText;
|
|
[SerializeField] private TMP_InputField nameInputField;
|
|
[SerializeField] private Button restartButton;
|
|
[SerializeField] private Button exitButton;
|
|
|
|
private int _coins;
|
|
|
|
private void Awake() => Instance = this;
|
|
|
|
private void Start()
|
|
{
|
|
Time.timeScale = 1;
|
|
|
|
if (deathPanelCanvasGroup != null)
|
|
{
|
|
deathPanelCanvasGroup.gameObject.SetActive(false);
|
|
deathPanelCanvasGroup.alpha = 0;
|
|
}
|
|
|
|
var player = GameObject.FindGameObjectWithTag("Player")?.GetComponent<Gameplay.Player.PlayerController>();
|
|
if (player != null) player.OnPlayerDeath += ShowDeathScreen;
|
|
|
|
restartButton.onClick.AddListener(RestartGame);
|
|
exitButton.onClick.AddListener(ExitGame);
|
|
}
|
|
|
|
public void AddCoin()
|
|
{
|
|
_coins++;
|
|
coinsText.text = $"{_coins} Coins";
|
|
}
|
|
|
|
private void ShowDeathScreen()
|
|
{
|
|
deathPanelCanvasGroup.gameObject.SetActive(true);
|
|
|
|
Tween.Alpha(deathPanelCanvasGroup, startValue: 0f, endValue: 1f, duration: 0.5f, ease: Ease.OutSine, useUnscaledTime: true);
|
|
|
|
deathPanelWindow.localScale = Vector3.zero;
|
|
Tween.Scale(deathPanelWindow, endValue: 1f, duration: 0.6f, ease: Ease.OutBack, useUnscaledTime: true)
|
|
.OnComplete(() => {
|
|
Time.timeScale = 0;
|
|
});
|
|
|
|
finalResultText.text = $"You collected: {_coins} coins";
|
|
}
|
|
|
|
private void RestartGame()
|
|
{
|
|
SaveScore();
|
|
Time.timeScale = 1;
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
|
}
|
|
|
|
private void ExitGame()
|
|
{
|
|
SaveScore();
|
|
Time.timeScale = 1;
|
|
SceneManager.LoadScene("MainMenu");
|
|
}
|
|
|
|
private void SaveScore()
|
|
{
|
|
var data = SaveManager.Load();
|
|
var currentName = string.IsNullOrEmpty(nameInputField.text) ? "Player" : nameInputField.text;
|
|
|
|
data.playerName = currentName;
|
|
|
|
data.leaderboard.Add(new Saves.LeaderboardEntry(currentName, _coins));
|
|
|
|
data.leaderboard.Sort((a, b) => b.score.CompareTo(a.score));
|
|
|
|
if (data.leaderboard.Count > 10)
|
|
{
|
|
data.leaderboard.RemoveRange(10, data.leaderboard.Count - 10);
|
|
}
|
|
|
|
SaveManager.Save(data);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
restartButton.onClick.RemoveAllListeners();
|
|
exitButton.onClick.RemoveAllListeners();
|
|
|
|
var player = GameObject.FindGameObjectWithTag("Player")?.GetComponent<Gameplay.Player.PlayerController>();
|
|
if (player != null) player.OnPlayerDeath -= ShowDeathScreen;
|
|
}
|
|
}
|
|
} |