115 lines
3.7 KiB
C#
115 lines
3.7 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;
|
|
|
|
[SerializeField] private AudioSource audioSource;
|
|
[SerializeField] private AudioClip coinClip;
|
|
[SerializeField] private AudioClip deathClip;
|
|
|
|
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";
|
|
|
|
Tween.PunchScale(coinsText.transform, strength: new Vector3(0.2f, 0.2f, 0.2f), duration: 0.2f);
|
|
|
|
if (audioSource && coinClip)
|
|
audioSource.PlayOneShot(coinClip);
|
|
}
|
|
|
|
private void ShowDeathScreen()
|
|
{
|
|
if (audioSource && deathClip)
|
|
audioSource.PlayOneShot(deathClip);
|
|
|
|
deathPanelCanvasGroup.gameObject.SetActive(true);
|
|
|
|
Sequence.Create(useUnscaledTime: true)
|
|
.Group(Tween.Alpha(deathPanelCanvasGroup, 1f, 0.4f))
|
|
.Group(Tween.Scale(deathPanelWindow, startValue: 0.8f, endValue: 1f, duration: 0.5f, ease: Ease.OutBack))
|
|
.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 LeaderboardEntry(currentName, _coins));
|
|
|
|
data.leaderboard.Sort((a, b) => b.score.CompareTo(a.score));
|
|
|
|
if (data.leaderboard.Count > 7)
|
|
{
|
|
data.leaderboard.RemoveRange(7, data.leaderboard.Count - 7);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |