252 lines
10 KiB
C#
252 lines
10 KiB
C#
using System.Globalization;
|
|
using System.Reflection.Emit;
|
|
using Code.App.Loading;
|
|
using Code.App.StateMachine;
|
|
using Code.Audio;
|
|
using Code.Gameplay.Camera;
|
|
using Code.Gameplay.Camera.CameraStates;
|
|
using Code.Gameplay.Character.Core;
|
|
using Code.Gameplay.Orders.Order;
|
|
using Code.Gameplay.Orders.Order;
|
|
using Code.Gameplay.Player;
|
|
using Code.Input;
|
|
using Code.Scenes;
|
|
using Code.Scenes.SceneEnvironments;
|
|
using Code.StateMachine;
|
|
using Code.UI;
|
|
using Code.UI.Panels;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Core;
|
|
using Reflex.Extensions;
|
|
using Reflex.Injectors;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Code.App.AppStates
|
|
{
|
|
public class GameplayState : BaseState<AppStateMachineContext>
|
|
{
|
|
|
|
private PlayerHealth _playerHealth;
|
|
private PlayerMoney _playerMoney;
|
|
|
|
private IScoreService _scoreService;
|
|
private IOrderService _orderService;
|
|
|
|
private CharacterProvider _playerCharacter;
|
|
|
|
public GameplayState(AppStateMachineContext context) : base(context)
|
|
{
|
|
}
|
|
|
|
public override void EnterState()
|
|
{
|
|
Context.LoadingManager
|
|
.RegisterLoading(LoadingWork.LoadScene)
|
|
.RegisterLoading(LoadingWork.WaitForSceneInstaller)
|
|
.RegisterLoading(LoadingWork.LoadUI)
|
|
.RegisterLoading(LoadingWork.LoadModelResource)
|
|
.RegisterLoading(LoadingWork.LoadAudio);
|
|
// Context.UIService.LoadingScreen.Show();
|
|
|
|
_playerMoney = Context.PlayerFactory.CreatePlayerMoney();
|
|
_playerHealth = Context.PlayerFactory.CreatePlayerHealth();
|
|
|
|
_playerHealth.OnDeath += EndGame;
|
|
|
|
Context.AudioService.TransitToSnapshot(AudioSnapshotState.Peaceful);
|
|
|
|
Context.InputService.EnableMap(ActionMappings.Gameplay);
|
|
|
|
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug1, GiveOrderDEBUG);
|
|
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug2, RecieveOrderDEBUG);
|
|
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug3, DeliverOrderDEBUG);
|
|
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug4, RoomPassedDEBUG);
|
|
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.Debug5, Tube);
|
|
// 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 UniTaskVoid WaitForSceneToLoad()
|
|
{
|
|
await Context.SceneLoaderService.LoadSceneAsyncAwaitable((int)SceneIds.Gameplay);
|
|
|
|
await UniTask.WhenAll(
|
|
Context.UIService.AsyncSwitchPanelTo(UIPanel.Gameplay),
|
|
// TODO
|
|
Context.AudioFactory.WarmGameplayMusic(),
|
|
Context.AudioFactory.WarmSFX(),
|
|
Context.AudioFactory.WarmDispatcher(),
|
|
// TODO
|
|
WaitForPlayerArmsToInitialize(),
|
|
WaitForPlayerCharacterLoad()
|
|
);
|
|
|
|
Container sceneContainer = SceneManager.GetActiveScene().GetSceneContainer();
|
|
|
|
GameplaySceneEnvironment sceneEnvironment = SceneEnvironmentVariables(sceneContainer);
|
|
|
|
PlayerInitialize(sceneEnvironment);
|
|
|
|
CameraInitialize();
|
|
|
|
InputServiceInitialize();
|
|
|
|
GameplayServicesInitialize(sceneContainer);
|
|
|
|
MusicInitialize();
|
|
|
|
UIInitialize();
|
|
|
|
_tickEnabled = true;
|
|
|
|
Context.LoadingManager
|
|
.UnregisterLoading(LoadingWork.LoadScene)
|
|
.UnregisterLoading(LoadingWork.LoadUI)
|
|
.UnregisterLoading(LoadingWork.LoadModelResource)
|
|
.UnregisterLoading(LoadingWork.LoadAudio);
|
|
// Context.UIService.LoadingScreen.Hide();
|
|
}
|
|
|
|
private async UniTask WaitForPlayerArmsToInitialize() => _armsMesh = await Context.PlayerFactory.CreateArms(Context.CameraStateMachine.Camera.transform);
|
|
|
|
private void OrderGiven(float time)
|
|
{
|
|
_gameplayPanel.ShowTimer();
|
|
}
|
|
private void OrderFailure()
|
|
{
|
|
_playerHealth.InstaKill();
|
|
}
|
|
private void OrderRecieved(float time)
|
|
{
|
|
Context.AudioService.PlayDispatcher((DispatcherSounds)Random.Range((int)DispatcherSounds.Recieve1,(int)DispatcherSounds.Recieve2));
|
|
}
|
|
private void OrderDelivered(int score, float time)
|
|
{
|
|
_gameplayPanel.HideTimer();
|
|
|
|
if(time < 0f) Context.AudioService.PlayDispatcher((DispatcherSounds)Random.Range((int)DispatcherSounds.Late1,(int)DispatcherSounds.Late2));
|
|
_playerMoney.GiveMoney(score);
|
|
}
|
|
// DEBUG
|
|
private void GiveOrderDEBUG(InputAction.CallbackContext context)
|
|
{
|
|
_orderService.OrderGive();
|
|
}
|
|
|
|
private void RecieveOrderDEBUG(InputAction.CallbackContext context)
|
|
{
|
|
_orderService.OrderRecieved();
|
|
}
|
|
private void DeliverOrderDEBUG(InputAction.CallbackContext context)
|
|
{
|
|
_orderService.OrderDelivered();
|
|
}
|
|
private void RoomPassedDEBUG(InputAction.CallbackContext context)
|
|
{
|
|
_orderService.RoomPassed();
|
|
}
|
|
private OrderDispenser _debugTube;
|
|
|
|
private void Tube(InputAction.CallbackContext context)
|
|
{
|
|
_debugTube.ThrowOrder();
|
|
}
|
|
// DEBUG
|
|
private bool _tickEnabled;
|
|
public override void Tick()
|
|
{
|
|
if (!_tickEnabled) return;
|
|
|
|
_orderService.DecreaseTimer(Time.deltaTime);
|
|
}
|
|
|
|
private void EndGame()
|
|
{
|
|
Context.StateMachine.SwitchState(AppState.GameOver)
|
|
.AllowStateTransition(AppState.MainMenu);
|
|
}
|
|
|
|
public override void ExitState()
|
|
{
|
|
_playerHealth = null;
|
|
_playerMoney = null;
|
|
|
|
_tickEnabled = false;
|
|
|
|
Context.PlayerFactory.ReleasePlayer(_playerCharacter);
|
|
_playerCharacter = null;
|
|
Context.PlayerFactory.ReleaseArms(_armsMesh);
|
|
_armsMesh = null;
|
|
_orderService = 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, GiveOrderDEBUG);
|
|
Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug2, RecieveOrderDEBUG);
|
|
Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug3, DeliverOrderDEBUG);
|
|
Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug4, RoomPassedDEBUG);
|
|
Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug5, Tube);
|
|
// Context.InputService.DebugUnbindAction((int)ActionMappings.Debug, (int)DebugActions.Debug6, PlayDebugDispatcher);
|
|
|
|
base.ExitState();
|
|
}
|
|
|
|
private void InputServiceInitialize()
|
|
{
|
|
Context.InputService.EnableMap(ActionMappings.Gameplay);
|
|
}
|
|
private void CameraInitialize()
|
|
{
|
|
Context.CameraStateMachine.SwitchState(CameraMode.FirstPerson);
|
|
((BaseCameraState)Context.CameraStateMachine.CurrentBaseState).SetFollowTransform(_playerCharacter.GetFollowTransform());
|
|
}
|
|
|
|
private GameplayPanel _gameplayPanel;
|
|
private ArmsMeshProvider _armsMesh;
|
|
|
|
private void UIInitialize()
|
|
{
|
|
_gameplayPanel = (GameplayPanel)Context.UIService.CurrentPanel;
|
|
_gameplayPanel.Initialize(_playerMoney, _playerHealth, _orderService);
|
|
Context.AudioService.OnDispatcherStarted += _gameplayPanel.SummonDispatcher;
|
|
Context.AudioService.OnDispatcherEnded += _gameplayPanel.CloseDispatcher;
|
|
}
|
|
private void MusicInitialize()
|
|
{
|
|
Context.AudioService.PlayRandomMusicUntilHumanityWillBeDestroyedForTheySins(MusicType.Peaceful);
|
|
Context.AudioService.PlayRandomMusicUntilHumanityWillBeDestroyedForTheySins(MusicType.Action);
|
|
}
|
|
private void PlayerInitialize(GameplaySceneEnvironment sceneEnvironment)
|
|
{
|
|
_playerCharacter.Teleport(sceneEnvironment.playerSpawnPoint.position, sceneEnvironment.playerSpawnPoint.rotation);
|
|
_playerCharacter.PlayerAnimator.SetAnimator(_armsMesh.ArmsAnimator);
|
|
}
|
|
private void GameplayServicesInitialize(Container sceneContainer)
|
|
{
|
|
_scoreService = sceneContainer.Resolve<IScoreService>();
|
|
_orderService = sceneContainer.Resolve<IOrderService>();
|
|
|
|
_orderService.OnOrderGiven += OrderGiven;
|
|
_orderService.OnOrderRecieved += OrderRecieved;
|
|
_orderService.OnOrderDelivered += OrderDelivered;
|
|
_orderService.OnOrderFailure += OrderFailure;
|
|
}
|
|
private GameplaySceneEnvironment SceneEnvironmentVariables(Container sceneContainer)
|
|
{
|
|
var sceneEnvironment = (GameplaySceneEnvironment)sceneContainer.Resolve<CurrentSceneEnvironment>();
|
|
|
|
_debugTube = sceneEnvironment.debugTube;
|
|
return sceneEnvironment;
|
|
}
|
|
}
|
|
} |