135 lines
5.4 KiB
C#
135 lines
5.4 KiB
C#
using Code.Game.Cleaning.Player;
|
|
using Code.Game.Gameplay.Player;
|
|
using Code.Game.Gameplay.Player.Factory;
|
|
using Code.Infrastructure.App;
|
|
using Code.Infrastructure.Camera;
|
|
using Code.Infrastructure.Generators;
|
|
using Code.Infrastructure.Input;
|
|
using Code.Infrastructure.SceneLoader;
|
|
using Code.Infrastructure.Utils;
|
|
using Code.OfficeGame.BattleMode.Actions.Player;
|
|
using Code.OfficeGame.Player;
|
|
using Cysharp.Threading.Tasks;
|
|
using KinematicCharacterController;
|
|
using ObservableCollections;
|
|
using R3;
|
|
using Reflex.Core;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Code.Game.TestingLevel
|
|
{
|
|
[State(typeof(AppStates), typeof(AppStateMachineContext))]
|
|
public partial class TestingState
|
|
{
|
|
public TestingState(AppStateMachineContext context) : base(context)
|
|
{
|
|
}
|
|
|
|
public override AppStates MyState => AppStates.Testing;
|
|
|
|
public override async UniTask EnterState()
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
#if UNITY_EDITOR
|
|
Context.InputService.EnableMap(ActionMappings.Debug);
|
|
|
|
Context.InputService.BindActionPerformed((int)ActionMappings.Debug, (int)DebugActions.ToggleBattleMode, ToggleBattleMode);
|
|
#endif
|
|
|
|
Context.InputService.EnableMap(ActionMappings.OpenMovement);
|
|
|
|
await Context.Camera.SwitchState(CameraMode.OpenMovement);
|
|
|
|
if (Context.StateMachine.CurrentState == AppStates.BattleState) return;
|
|
|
|
Context.AudioService.SetAudioListener(Context.Camera.Camera.GetComponent<AudioListener>());
|
|
|
|
var waitForContainer =
|
|
Context.SceneContainerManager.ScenesContainers
|
|
.ObserveDictionaryAdd()
|
|
.Where(x => x.Key == SceneId.Testing)
|
|
.Take(1)
|
|
.WaitAsync()
|
|
.AsUniTask();
|
|
|
|
var factoryConfig = new SingleConfig<PlayerFactorySettings>();
|
|
|
|
await factoryConfig.LoadConfig();
|
|
|
|
var factory = new PlayerFactory(factoryConfig);
|
|
var player = await factory.CreatePlayer();
|
|
var playerKinematicCharacterMotor = player.GetComponent<KinematicCharacterMotor>();
|
|
var playerMovementController = player.GetComponent<PlayerMovementController>();
|
|
var playerControlsProvider = player.GetComponent<PlayerControlsProvider>();
|
|
var playerAnimatorProvider = player.GetComponent<PlayerAnimator>();
|
|
var playerTimelineActor = player.GetComponent<PlayerTimelineActor>();
|
|
|
|
playerMovementController.Freeze = true;
|
|
|
|
// ((OpenMovementCameraState)Context.Camera.CurrentBaseState).SetTarget(player.transform);
|
|
|
|
void InstallExtra(Scene scene, ContainerBuilder builder)
|
|
{
|
|
builder.RegisterValue(factoryConfig);
|
|
builder.RegisterValue(factory, new []{typeof(IPlayerFactory)});
|
|
builder.RegisterValue(playerKinematicCharacterMotor);
|
|
builder.RegisterValue(playerMovementController);
|
|
builder.RegisterValue(playerControlsProvider);
|
|
builder.RegisterValue(playerAnimatorProvider);
|
|
builder.RegisterValue(playerTimelineActor);
|
|
}
|
|
|
|
await UniTask.WhenAll(
|
|
Context.SceneLoader
|
|
.LoadSceneAsync(
|
|
SceneId.Testing,
|
|
LoadSceneMode.Single,
|
|
true,
|
|
InstallExtra),
|
|
Context.SceneLoader
|
|
.LoadSceneAsync(
|
|
SceneId.FirstTestingSegment,
|
|
LoadSceneMode.Additive,
|
|
false),
|
|
Context.SceneLoader
|
|
.LoadSceneAsync(
|
|
SceneId.SecondTestingSegment,
|
|
LoadSceneMode.Additive,
|
|
false),
|
|
waitForContainer
|
|
);
|
|
|
|
var container = Context.SceneContainerManager.ScenesContainers[SceneId.Testing];
|
|
|
|
container.Resolve<KinematicCharacterMotor>()
|
|
.SetPositionAndRotation(
|
|
container.Resolve<TestingLevelData>().playerSpawnPoint.position,
|
|
container.Resolve<TestingLevelData>().playerSpawnPoint.rotation);
|
|
|
|
playerMovementController.Freeze = false;
|
|
}
|
|
|
|
private void ToggleBattleMode(InputAction.CallbackContext obj)
|
|
{
|
|
Context.StateMachine.SwitchState(AppStates.BattleState).Forget();
|
|
}
|
|
|
|
public override async UniTask ExitState()
|
|
{
|
|
#if UNITY_EDITOR
|
|
Context.InputService.DisableMap(ActionMappings.Debug);
|
|
Context.InputService.UnbindActionPerformed((int)ActionMappings.Debug, (int)DebugActions.ToggleBattleMode, ToggleBattleMode);
|
|
#endif
|
|
Context.InputService.DisableMap(ActionMappings.OpenMovement);
|
|
|
|
if (Context.StateMachine.NextState == AppStates.BattleState) return;
|
|
|
|
await Context.SceneLoader.UnloadSceneAsync(SceneId.Testing);
|
|
await Context.SceneLoader.UnloadSceneAsync(SceneId.FirstTestingSegment);
|
|
await Context.SceneLoader.UnloadSceneAsync(SceneId.SecondTestingSegment);
|
|
}
|
|
}
|
|
} |