77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using Code.App.StateMachine;
|
|
using Code.Gameplay.Player;
|
|
using Code.Input;
|
|
using Code.StateMachine;
|
|
using Code.UI;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Core;
|
|
using Reflex.Extensions;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Code.App.AppStates
|
|
{
|
|
public class GameOverState : BaseState<AppStateMachineContext>
|
|
{
|
|
public GameOverState(AppStateMachineContext context) : base(context)
|
|
{
|
|
}
|
|
private IPlayerScore _playerScore;
|
|
|
|
private GameOverPanel _gameOverPanel;
|
|
|
|
public override void EnterState()
|
|
{
|
|
Container sceneContainer = SceneManager.GetActiveScene().GetSceneContainer();
|
|
_playerScore = sceneContainer.Resolve<IPlayerScore>();
|
|
_scoreCounter = 0;
|
|
|
|
Context.InputService.EnableMap(ActionMappings.UI);
|
|
Context.InputService.DebugBindAction((int)ActionMappings.UI,(int)UIActions.Continue, TryToExit);
|
|
|
|
GameOver();
|
|
|
|
base.EnterState();
|
|
}
|
|
|
|
private void TryToExit(InputAction.CallbackContext context)
|
|
{
|
|
ToMainMenu();
|
|
}
|
|
|
|
private async UniTaskVoid GameOver()
|
|
{
|
|
await Context.UIService.AsyncSwitchPanelTo(UIPanel.GameOver);
|
|
|
|
_gameOverPanel = (GameOverPanel)Context.UIService.CurrentPanel;
|
|
|
|
}
|
|
|
|
private void ToMainMenu()
|
|
{
|
|
Context.StateMachine.SwitchState(AppState.MainMenu)
|
|
.AllowStateTransition(AppState.Gameplay);
|
|
}
|
|
private float _scoreCounter;
|
|
public override void Tick()
|
|
{
|
|
if (!_gameOverPanel) return;
|
|
|
|
_scoreCounter += Time.deltaTime * 100;
|
|
// UnityEngine.Debug.Log($"Score: {_scoreCounter}");
|
|
if((int)_scoreCounter >= _playerScore.Score)
|
|
{
|
|
_gameOverPanel.UpdateScoreText(_playerScore.Score);
|
|
}
|
|
else _gameOverPanel.UpdateScoreText((int)_scoreCounter);
|
|
}
|
|
public override void ExitState()
|
|
{
|
|
Context.InputService.DisableMap(ActionMappings.UI);
|
|
Context.InputService.DebugUnbindAction((int)ActionMappings.UI,(int)UIActions.Continue, TryToExit);
|
|
|
|
base.ExitState();
|
|
}
|
|
}
|
|
} |