119 lines
4.1 KiB
C#
119 lines
4.1 KiB
C#
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.Player;
|
|
using Cysharp.Threading.Tasks;
|
|
using KinematicCharacterController;
|
|
using Reflex.Core;
|
|
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 = new 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<EntityTimelineActor>().ToList();
|
|
|
|
if (Context.StateMachine.CurrentState == AppStates.Testing)
|
|
{
|
|
_battleTokenSource = new();
|
|
|
|
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();
|
|
}
|
|
|
|
private void TogglePlayerBattleMode(Container container, bool enabled)
|
|
{
|
|
container.Resolve<EntityAnimator>().Animator.enabled = enabled;
|
|
container.Resolve<PlayerControlsProvider>().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);
|
|
|
|
// T
|
|
var entities =
|
|
Object.FindObjectsByType<EntityTimelineActor>().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);
|
|
}
|
|
}
|
|
} |