127 lines
4.0 KiB
C#
127 lines
4.0 KiB
C#
using Code.DI;
|
|
using Code.Gameplay.Character.Core;
|
|
using Code.Gameplay.Enemy;
|
|
using Code.Input;
|
|
using DG.Tweening;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Code.UI
|
|
{
|
|
public class GameplayController : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject endScreen;
|
|
[SerializeField] private TMP_Text endText;
|
|
[SerializeField] private Button restartButton;
|
|
[SerializeField] private Button exitButton;
|
|
|
|
[SerializeField] private float fadeDuration = 0.5f;
|
|
[SerializeField] private float buttonDelay = 0.2f;
|
|
[SerializeField] private Ease showEase = Ease.OutBack;
|
|
|
|
private IInputService _inputService;
|
|
private CharacterProvider _player;
|
|
|
|
private void Start()
|
|
{
|
|
var container = FindFirstObjectByType<DIContainer>();
|
|
_inputService = container.Resolve<IInputService>();
|
|
_inputService.EnableMap(ActionMapping.Gameplay);
|
|
Cursor.visible = false;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
_player = FindFirstObjectByType<CharacterProvider>();
|
|
|
|
if (_player != null && _player.HealthSystem != null)
|
|
{
|
|
_player.HealthSystem.OnDied += HandlePlayerDeath;
|
|
}
|
|
|
|
restartButton.onClick.AddListener(RestartGame);
|
|
exitButton.onClick.AddListener(ExitToMenu);
|
|
|
|
endScreen.SetActive(false);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!endScreen.activeInHierarchy)
|
|
{
|
|
CheckVictoryCondition();
|
|
}
|
|
}
|
|
|
|
private void CheckVictoryCondition()
|
|
{
|
|
var enemies = FindObjectsByType<SimpleEnemy>(FindObjectsSortMode.None);
|
|
if (enemies.Length == 0)
|
|
{
|
|
ShowEndScreen("You Won!", Color.green);
|
|
}
|
|
}
|
|
|
|
private void HandlePlayerDeath()
|
|
{
|
|
ShowEndScreen("You Died!", Color.red);
|
|
}
|
|
|
|
private void ShowEndScreen(string message, Color color)
|
|
{
|
|
_inputService.DisableMap(ActionMapping.Gameplay);
|
|
Cursor.visible = true;
|
|
Cursor.lockState = CursorLockMode.None;
|
|
|
|
endScreen.SetActive(true);
|
|
|
|
endText.text = message;
|
|
endText.color = color;
|
|
|
|
var sequence = DOTween.Sequence();
|
|
|
|
endScreen.transform.localScale = Vector3.zero;
|
|
endText.rectTransform.localScale = Vector3.zero;
|
|
restartButton.transform.localScale = Vector3.zero;
|
|
exitButton.transform.localScale = Vector3.zero;
|
|
|
|
sequence.Append(endScreen.transform.DOScale(1f, fadeDuration).SetEase(showEase))
|
|
.Join(endText.rectTransform.DOScale(1f, fadeDuration).SetEase(showEase))
|
|
.Append(restartButton.transform.DOScale(1f, fadeDuration).SetEase(showEase))
|
|
.Append(exitButton.transform.DOScale(1f, fadeDuration).SetEase(showEase));
|
|
|
|
endText.rectTransform.DOScale(1.1f, 1f)
|
|
.SetLoops(-1, LoopType.Yoyo)
|
|
.SetEase(Ease.InOutSine);
|
|
}
|
|
|
|
private void RestartGame()
|
|
{
|
|
restartButton.transform.DOPunchScale(Vector3.one * 0.2f, 0.1f);
|
|
|
|
SceneManager.LoadScene("Gameplay");
|
|
}
|
|
|
|
private void ExitToMenu()
|
|
{
|
|
exitButton.transform.DOPunchScale(Vector3.one * 0.2f, 0.1f);
|
|
|
|
SceneManager.LoadScene("MainMenu");
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_inputService.DisableMap(ActionMapping.Gameplay);
|
|
|
|
if (_player != null && _player.HealthSystem != null)
|
|
{
|
|
_player.HealthSystem.OnDied -= HandlePlayerDeath;
|
|
}
|
|
|
|
restartButton?.onClick.RemoveListener(RestartGame);
|
|
exitButton?.onClick.RemoveListener(ExitToMenu);
|
|
|
|
DOTween.KillAll();
|
|
}
|
|
}
|
|
} |