94 lines
2.9 KiB
C#
94 lines
2.9 KiB
C#
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 playerName = nameInputField.text;
|
|
if (string.IsNullOrEmpty(playerName))
|
|
playerName = "Player";
|
|
Debug.Log($"Saved: {playerName} - {_coins}");
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
restartButton.onClick.RemoveAllListeners();
|
|
exitButton.onClick.RemoveAllListeners();
|
|
|
|
var player = GameObject.FindGameObjectWithTag("Player")?.GetComponent<Gameplay.Player.PlayerController>();
|
|
if (player != null) player.OnPlayerDeath -= ShowDeathScreen;
|
|
}
|
|
}
|
|
} |