Files
CourierOld/Game/CourierOld/Assets/Code/App/AppStates/GameOverState.cs
T

86 lines
2.8 KiB
C#

using Code.App.StateMachine;
using Code.Audio;
using Code.Gameplay.Player;
using Code.Input;
using Code.Scenes;
using Code.Scenes.SceneEnvironments;
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);
Context.AudioService.PlayRandomMusicUntilHumanityWillBeDestroyedForTheySins(MusicType.Death);
Context.AudioService.ApplyStun(5000,0.5f);
GameOver().Forget();
base.EnterState();
}
private void TryToExit(InputAction.CallbackContext context)
{
ToMainMenu();
}
private async UniTaskVoid GameOver()
{
await Context.UIService.AsyncSwitchPanelTo(UIPanel.GameOver);
_gameOverPanel = (GameOverPanel)Context.UIService.CurrentPanel;
_gameOverPanel.Show();
}
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);
Context.AudioService.GladlyAskToStopMusic(MusicType.Death, callback: Context.AudioFactory.ReleaseDeathMusic);
Context.AudioService.TransitToSnapshot(AudioSnapshotState.None);
base.ExitState();
}
}
}