using System.Collections.Generic; using Code.App.StateMachine; using Code.Audio; using Code.Gameplay.Camera; using Code.Gameplay.Camera.CameraStates; using Code.Gameplay.Character.Core; using Code.Gameplay.Enemies; 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.Extensions; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; namespace Code.App.AppStates { public class GameplayState : BaseState { // DEBUG private Transform _testSFXTransform; // DEBUG private PlayerHealth _playerHealth; private PlayerMoney _playerMoney; private CharacterProvider _playerCharacter; private List _enemies; public GameplayState(AppStateMachineContext context) : base(context) { } public override void EnterState() { Context.UIService.LoadingScreen.Show(); _playerMoney = Context.PlayerFactory.CreatePlayerMoney(); //_playerHealth = Context.PlayerProvider.Health; //_playerHealth.OnDeath += EndGame; Context.AudioService.TransitToSnapshot(AudioSnapshotState.Peaceful); Context.InputService.EnableMap(ActionMappings.Gameplay); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug1, GiveHealth); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug3, GiveMoney); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug2, LooseHealth); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug4, LooseMoney); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug5, PlayDebugSFX); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug6, PlayDebugDispatcher); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed WaitForSceneToLoad(); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } private async UniTask WaitForPlayerCharacterLoad() => _playerCharacter = await Context.PlayerFactory.CreatePlayer(); private async UniTask WaitForEnemyLoad(EnemyType type) => _enemies.Add(await Context.EnemyFactory.CreateEnemy(type)); private async UniTaskVoid WaitForSceneToLoad() { await UniTask.WhenAll( Context.SceneLoaderService.LoadSceneAsyncAwaitable((int)SceneIds.Gameplay), Context.UIService.AsyncSwitchPanelTo(UIPanel.Gameplay), Context.AudioFactory.WarmGameplayMusic(), Context.AudioFactory.WarmSFX(), WaitForPlayerCharacterLoad() ); _playerHealth = Context.PlayerProvider.Health; _playerHealth.OnDeath += EndGame; Context.AudioService.PlayRandomMusicUntilHumanityWillBeDestroyedForTheySins(MusicType.Peaceful); Context.AudioService.PlayRandomMusicUntilHumanityWillBeDestroyedForTheySins(MusicType.Action); var sceneEnvironment = ((GameplaySceneEnvironment)SceneManager.GetActiveScene() .GetSceneContainer().Resolve()); _testSFXTransform = sceneEnvironment.debugSfxTarget; _playerCharacter.Teleport(sceneEnvironment.playerSpawnPoint.position, sceneEnvironment.playerSpawnPoint.rotation); ((GameplayPanel)Context.UIService.CurrentPanel).Initialize(_playerMoney, _playerHealth); Context.CameraStateMachine.SwitchState(CameraMode.FirstPerson); ((BaseCameraState)Context.CameraStateMachine.CurrentBaseState).SetFollowTransform(_playerCharacter.GetFollowTransform()); Context.InputService.EnableMap(ActionMappings.Gameplay); Context.UIService.LoadingScreen.Hide(); } private void OnCharacterSpawn(CharacterProvider characterProvider) { ((BaseCameraState)Context.CameraStateMachine.CurrentBaseState).SetFollowTransform(_playerCharacter.GetFollowTransform()); } private void PlayDebugSFX(InputAction.CallbackContext context) { Context.AudioService.PlaySFX(SFXSounds.Crowbar, _testSFXTransform.position); } private void PlayDebugDispatcher(InputAction.CallbackContext context) { Context.AudioService.PlayDispatcher(DispatcherSounds.AhFreeman); } private void GiveHealth(InputAction.CallbackContext context) { _playerHealth.GetDamage(-1); } private void LooseHealth(InputAction.CallbackContext context) { _playerHealth.GetDamage(1); } private void GiveMoney(InputAction.CallbackContext context) { _playerMoney.GiveMoney(100); ((GameplayPanel)(Context.UIService.CurrentPanel)).SummonDispatcher(); } private void LooseMoney(InputAction.CallbackContext context) { _playerMoney.LooseMoney(100); ((GameplayPanel)(Context.UIService.CurrentPanel)).CloseDispatcher(); } // DEBUG public override void Tick() { } private void EndGame() { Context.StateMachine.SwitchState(AppState.GameOver) .AllowStateTransition(AppState.MainMenu); } public override void ExitState() { _playerHealth = null; _playerMoney = null; Context.PlayerFactory.ReleasePlayer(_playerCharacter); _playerCharacter = null; Context.InputService.DisableMap(ActionMappings.Gameplay); Context.AudioService.GladlyAskToStopMusic(MusicType.Action, callback: Context.AudioFactory.ReleaseActionMusic); Context.AudioService.GladlyAskToStopMusic(MusicType.Peaceful, callback: Context.AudioFactory.ReleasePeacefulMusic); Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug1, GiveHealth); Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug3, GiveHealth); Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug2, LooseHealth); Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug4, LooseMoney); Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug5, PlayDebugSFX); Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug6, PlayDebugDispatcher); base.ExitState(); } } }