using System.Collections.Generic; using System.Linq; using System.Threading; using Code.Game.Cleaning.Player; using Code.Infrastructure.App; using Code.Infrastructure.Camera; using Code.Infrastructure.Generators; using Code.Infrastructure.Input; using Code.Infrastructure.SceneLoader; using Code.OfficeGame.BattleMode.Actions; using Code.OfficeGame.BattleMode.Actions.Player; using Code.OfficeGame.BattleMode.Timeline; using Code.OfficeGame.BattleMode.UI; using Code.OfficeGame.Player; using Cysharp.Threading.Tasks; using KinematicCharacterController; using Reflex.Core; using Reflex.Injectors; using UnityEngine; using UnityEngine.InputSystem; namespace Code.Game.OfficeGame.GameStates { [State(typeof(AppStates), typeof(AppStateMachineContext))] public partial class BattleState { public BattleState(AppStateMachineContext context) : base(context) { } public override AppStates MyState => AppStates.BattleState; private BattleTimeline _battleTimeline; private CancellationTokenSource _battleTokenSource = new CancellationTokenSource(); public override async UniTask EnterState() { #if UNITY_EDITOR Context.InputService.EnableMap(ActionMappings.Debug); Context.InputService.BindActionPerformed((int)ActionMappings.Debug, (int)DebugActions.ToggleBattleMode, ToggleBattleMode); #endif Context.InputService.EnableMap(ActionMappings.BattleMode); var container = Context.SceneContainerManager.ScenesContainers[SceneId.Testing]; // T var entities = Object.FindObjectsByType().ToList(); if (Context.StateMachine.CurrentState == AppStates.Testing) { _battleTokenSource = new(); _battleTimeline = ConstructorInjector.Construct(typeof(BattleTimeline), container) as BattleTimeline; Physics.simulationMode = SimulationMode.Script; KinematicCharacterSystem.EnsureCreation(); KinematicCharacterSystem.Settings.AutoSimulation = false; KinematicCharacterSystem.Settings.Interpolate = false; TogglePlayerBattleMode(container, false); await Context.Camera.SwitchState(CameraMode.BattleMode); foreach (var entity in entities) { entity.animator.enabled = false; } } _battleTimeline?.RunBattleLoopAsync(entities,_battleTokenSource.Token).Forget(); await container.Resolve().Open(); } private void TogglePlayerBattleMode(Container container, bool enabled) { container.Resolve().Animator.enabled = enabled; container.Resolve().enabled = enabled; } private void ToggleBattleMode(InputAction.CallbackContext obj) { Context.StateMachine.SwitchState(AppStates.Testing).Forget(); } public override async UniTask ExitState() { if (Context.StateMachine.NextState == AppStates.Testing) { _battleTokenSource.Cancel(); _battleTokenSource.Dispose(); Physics.simulationMode = SimulationMode.FixedUpdate; KinematicCharacterSystem.Settings.AutoSimulation = true; KinematicCharacterSystem.Settings.Interpolate = true; var container = Context.SceneContainerManager.ScenesContainers[SceneId.Testing]; TogglePlayerBattleMode(container, true); await container.Resolve().Close(); // T var entities = Object.FindObjectsByType().ToList(); foreach (var entity in entities) { entity.animator.enabled = false; } } #if UNITY_EDITOR Context.InputService.DisableMap(ActionMappings.Debug); Context.InputService.UnbindActionPerformed((int)ActionMappings.Debug, (int)DebugActions.ToggleBattleMode, ToggleBattleMode); #endif Context.InputService.DisableMap(ActionMappings.BattleMode); } } }