337 lines
10 KiB
C#
337 lines
10 KiB
C#
using System.Data.Odbc;
|
|
using BITFALL.Player.Equip;
|
|
using BITFALL.Player.Movement;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using UnityEngine.InputSystem;
|
|
using BITKit.StateMachine;
|
|
using UnityEngine;
|
|
|
|
namespace BITFALL.Guns.States
|
|
{
|
|
[System.Serializable]
|
|
public class Movement : GunState, ISelectableCallback
|
|
{
|
|
[SerializeField] private ExpectState<bool> _expectRun;
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
switch (old)
|
|
{
|
|
case Aim:
|
|
root.animator.CrossFade(BITGun._Movement, 0.32f);
|
|
break;
|
|
default:
|
|
root.animator.CrossFade(BITGun._Movement, 0.16f);
|
|
break;
|
|
}
|
|
|
|
root.Entity.RegisterCallback<ISelectableCallback>(this);
|
|
}
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
base.OnStateExit(old, newState);
|
|
root.Entity.UnRegisterCallback<ISelectableCallback>(this);
|
|
}
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
if (root.animator[0].stateName is not BITGun._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>();
|
|
}
|
|
|
|
root.expectAiming.shouldBe = root.aimAction.action.IsPressed();
|
|
}
|
|
public void OnHover(ISelectable selectable)
|
|
{
|
|
}
|
|
|
|
public void OnActive(ISelectable selectable)
|
|
{
|
|
root.animator.Play(BITGun._Interactive);
|
|
}
|
|
|
|
public void OnInactive(ISelectable selectable)
|
|
{
|
|
}
|
|
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
_expectRun = newState is IPlayerRunState or IPlayerSprintState;
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public class Run : GunState
|
|
{
|
|
private ExpectState<bool> _expectRun;
|
|
private ExpectState<bool> _expectSprint;
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.CrossFade(BITGun._Run, 0.16f);
|
|
|
|
root.actionGroup.RegisterCallback(root.aimAction, OnAim);
|
|
}
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
root.actionGroup.UnRegisterCallback(root.aimAction, OnAim);
|
|
}
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
if (root.animator[0].stateName == BITGun._Movement)
|
|
{
|
|
root.animator.CrossFade(BITGun._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 class Sprint : GunState
|
|
{
|
|
private ExpectState<bool> _expectSprint;
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.CrossFade(BITGun._Sprint, 0.32f);
|
|
|
|
root.actionGroup.RegisterCallback(root.aimAction, OnAim);
|
|
}
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
root.actionGroup.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 class Aim : GunState
|
|
{
|
|
private IEntityMovement _entityMovement;
|
|
private IEquipService _equipService;
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
_entityMovement = root.Entity.Get<IEntityMovement>();
|
|
_equipService = root.Entity.Get<IEquipService>();
|
|
}
|
|
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
switch (old)
|
|
{
|
|
case IPlayerRunState:
|
|
case IPlayerSprintState:
|
|
root.animator.CrossFade(BITGun._Aim, 0.32f);
|
|
break;
|
|
default:
|
|
root.animator.CrossFade(BITGun._Aim, 0.16f);
|
|
break;
|
|
}
|
|
_equipService.Zoom.Allow = true;
|
|
_entityMovement.ExecuteCommand<PlayerCancelRunCommand>();
|
|
}
|
|
|
|
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>();
|
|
}
|
|
_equipService.Zoom.Value = root.aimAction.action.ReadValue<float>();
|
|
}
|
|
|
|
public override void OnStateExit(IState old, IState newState)
|
|
{
|
|
base.OnStateExit(old, newState);
|
|
_equipService.Zoom.Allow = false;
|
|
}
|
|
|
|
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 class Equip : GunState
|
|
{
|
|
private IntervalUpdate interval = new(0.16f);
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
interval.Reset();
|
|
root.animator.Play(BITGun._Equip);
|
|
}
|
|
public override void OnStateUpdate(float deltaTime)
|
|
{
|
|
var state = root.animator[0];
|
|
if (!interval.AllowUpdateWithoutReset) return;
|
|
if (state.stateName == BITGun._Equip)
|
|
{
|
|
if (state.currentState.normalizedTime >= 1)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
root.animator.Play(BITGun._Equip);
|
|
}
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class Reload:GunState
|
|
{
|
|
private IEntityMovement _entityMovement;
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
_entityMovement = root.Entity.Get<IEntityMovement>();
|
|
root.animator[0].onStateExit += OnAnimationStateExit;
|
|
}
|
|
private void OnAnimationStateExit(string obj)
|
|
{
|
|
if (Enabled is false) return;
|
|
if(obj is BITGun._Reload)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
base.OnStateEntry(old);
|
|
|
|
_entityMovement.ExecuteCommand<PlayerCancelRunCommand>();
|
|
root.animator.Play(BITGun._Reload);
|
|
}
|
|
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 class Melee : GunState
|
|
{
|
|
private IEntityMovement _entityMovement;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
_entityMovement = root.Entity.Get<IEntityMovement>();
|
|
root.animator[0].onStateExit += OnAnimationStateExit;
|
|
}
|
|
|
|
private void OnAnimationStateExit(string obj)
|
|
{
|
|
if (Enabled is false) return;
|
|
if (obj is BITGun._Melee)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.Play(BITGun._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 class Climb:GunState
|
|
{
|
|
public override void OnStateEntry(IState old)
|
|
{
|
|
root.animator.Play(BITGun._Climb);
|
|
}
|
|
|
|
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
|
|
{
|
|
if(newState is IPlayerClimbState)
|
|
{
|
|
root.TransitionState<Climb>();
|
|
}else if (Enabled)
|
|
{
|
|
root.TransitionState<Movement>();
|
|
}
|
|
}
|
|
}
|
|
} |