364 lines
11 KiB
C#
364 lines
11 KiB
C#
using System.Data.Odbc;
|
|
using BITFALL.Player.Equip;
|
|
using BITFALL.Player.Movement;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using BITKit.Selection;
|
|
using UnityEngine.InputSystem;
|
|
using BITKit.StateMachine;
|
|
using UnityEngine;
|
|
// ReSharper disable UnassignedField.Local
|
|
|
|
namespace BITFALL.Guns.States
|
|
{
|
|
[System.Serializable]
|
|
public sealed class None:GunState
|
|
{
|
|
}
|
|
[System.Serializable]
|
|
public class Movement : GunState
|
|
{
|
|
[SerializeField] private ExpectState<bool> _expectRun;
|
|
[Inject] protected ISelector _selector;
|
|
private float elapsedTime;
|
|
private bool boltActionImmediately;
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
switch (old)
|
|
{
|
|
case Aim:
|
|
root.animator.CrossFade(BITConstant.Player.Movement, 0.32f);
|
|
break;
|
|
default:
|
|
root.animator.CrossFade(BITConstant.Player.Movement, 0.16f);
|
|
break;
|
|
}
|
|
_selector.OnActive += OnActive;
|
|
boltActionImmediately = root.RequireBolt;
|
|
}
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
elapsedTime = 0;
|
|
base.OnStateExit(old, newState);
|
|
_selector.OnActive -= OnActive;
|
|
}
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
elapsedTime += deltaTime;
|
|
if (root.animator[0].stateName is not BITConstant.Player.Movement) return;
|
|
if (root.expectFiring.shouldBe && root.fireInterval.AllowUpdate)
|
|
{
|
|
root.Fire();
|
|
}
|
|
if (root.expectAiming.shouldBe && BITAppForUnity.AllowCursor == false)
|
|
{
|
|
root.TransitionState<Aim>();
|
|
}else if (_expectRun)
|
|
{
|
|
root.TransitionState<Run>();
|
|
}else if (root.RequireBolt && elapsedTime > 0.5f || boltActionImmediately)
|
|
{
|
|
root.TransitionState<Reload>();
|
|
}
|
|
}
|
|
public void OnActive(ISelectable selectable)
|
|
{
|
|
root.animator.Play(BITConstant.Player.Interactive);
|
|
}
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
_expectRun = newState is IPlayerRunState or IPlayerSprintState;
|
|
if (newState is IPlayerSlideState)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
root.animator.CrossFade(BITConstant.Player.Slide, 0.32f);
|
|
}
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class Run : GunState
|
|
{
|
|
private ExpectState<bool> _expectRun;
|
|
private ExpectState<bool> _expectSprint;
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.CrossFade(BITConstant.Player.Run, 0.16f);
|
|
|
|
root.inputActionGroup.RegisterCallback(root.aimAction, OnAim);
|
|
}
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
root.inputActionGroup.UnRegisterCallback(root.aimAction, OnAim);
|
|
}
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
if (root.animator[0].stateName == BITConstant.Player.Movement)
|
|
{
|
|
root.animator.CrossFade(BITConstant.Player.Run, 0.32f);
|
|
}
|
|
if(_expectSprint)
|
|
{
|
|
root.TransitionState<Sprint>();
|
|
}
|
|
else if(_expectRun == false)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
private void OnAim(InputAction.CallbackContext context)
|
|
{
|
|
if (context.started && BITAppForUnity.AllowCursor == false)
|
|
{
|
|
root.TransitionState<Aim>();
|
|
}
|
|
}
|
|
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
_expectRun = newState is IPlayerRunState or IPlayerSprintState;
|
|
_expectSprint = newState is IPlayerSprintState;
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class Sprint : GunState
|
|
{
|
|
private ExpectState<bool> _expectSprint;
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.CrossFade(BITConstant.Player.Sprint, 0.32f);
|
|
|
|
root.inputActionGroup.RegisterCallback(root.aimAction, OnAim);
|
|
}
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
root.inputActionGroup.UnRegisterCallback(root.aimAction, OnAim);
|
|
}
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
if(_expectSprint == false)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
private void OnAim(InputAction.CallbackContext context)
|
|
{
|
|
if (context.started && BITAppForUnity.AllowCursor == false)
|
|
{
|
|
root.TransitionState<Aim>();
|
|
}
|
|
}
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
_expectSprint = newState is IPlayerSprintState;
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class Aim : GunState
|
|
{
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
switch (old)
|
|
{
|
|
case IPlayerRunState:
|
|
case IPlayerSprintState:
|
|
root.animator.CrossFade(BITConstant.Player.Aim, 0.32f);
|
|
break;
|
|
default:
|
|
root.animator.CrossFade(BITConstant.Player.Aim, 0.16f);
|
|
break;
|
|
}
|
|
_entityMovement.ExecuteCommand<PlayerDisableRunCommand>(new(this));
|
|
}
|
|
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
base.OnStateExit(old, newState);
|
|
_entityMovement.ExecuteCommand<PlayerEnableRunCommand>(new(this));
|
|
}
|
|
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
if (root.expectFiring.shouldBe && root.fireInterval.AllowUpdate)
|
|
{
|
|
root.Fire();
|
|
}
|
|
|
|
if (BITAppForUnity.AllowCursor)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
else if (root.expectAiming.shouldBe)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
if (Enabled is false) return;
|
|
if (newState is not (IPlayerRunState or IPlayerSprintState)) return;
|
|
root.expectAiming.Reset();
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class Equip : GunState
|
|
{
|
|
private bool completed;
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
root.animator[0].onStateExit += OnAnimationStateExit;
|
|
}
|
|
|
|
|
|
private void OnAnimationStateExit(string obj)
|
|
{
|
|
if (obj is BITConstant.Player.Equip) completed = true;
|
|
}
|
|
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
completed = false;
|
|
root.animator.Play(BITConstant.Player.Equip,-1,0);
|
|
root.animator.animator.Update(0);
|
|
}
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
var state = root.animator[0];
|
|
if (state.stateName == BITConstant.Player.Equip)
|
|
{
|
|
if (completed)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
root.animator.Play(BITConstant.Player.Equip);
|
|
}
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public sealed class Reload:GunState
|
|
{
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
root.animator[0].onStateExit += OnAnimationStateExit;
|
|
}
|
|
private void OnAnimationStateExit(string obj)
|
|
{
|
|
if (Enabled is false) return;
|
|
if(obj is BITConstant.Player.Reload)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
base.OnStateEntry(old);
|
|
|
|
_entityMovement.ExecuteCommand<PlayerCancelRunCommand>();
|
|
|
|
if (root.RequireBolt)
|
|
{
|
|
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.BoltAction);
|
|
}
|
|
else
|
|
{
|
|
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.Reload);
|
|
}
|
|
|
|
}
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
if (Enabled is false) return;
|
|
if (newState is IPlayerRunState or IPlayerSprintState)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
root.RequireBolt = false;
|
|
base.OnStateExit(old, newState);
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class Melee : GunState
|
|
{
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
root.animator[0].onStateExit += OnAnimationStateExit;
|
|
}
|
|
|
|
private void OnAnimationStateExit(string obj)
|
|
{
|
|
if (Enabled is false) return;
|
|
if (obj is BITConstant.Player.Melee)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.Melee);
|
|
_entityMovement.ExecuteCommand(new PlayerDisableRunCommand(this));
|
|
}
|
|
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
_entityMovement.ExecuteCommand(new PlayerEnableRunCommand(this));
|
|
}
|
|
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
_entityMovement.ExecuteCommand<PlayerCancelRunCommand>();
|
|
}
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
if (Enabled is false) return;
|
|
if (newState is IPlayerRunState or IPlayerSprintState)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public sealed class Climb:GunState
|
|
{
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.Play(BITConstant.Player.Climb);
|
|
}
|
|
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
if(newState is IPlayerClimbState || (newState is IPlayerLinkState linkState && linkState.LinkArea == 4) )
|
|
{
|
|
root.TransitionState<Climb>();
|
|
}else if (Enabled)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public sealed class Holster : GunState
|
|
{
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.CrossFade(BITConstant.Player.Holster,0.16f);
|
|
}
|
|
}
|
|
} |