BITFALL/Assets/Artists/Scripts/Equip/BITGunStates.cs

347 lines
11 KiB
C#
Raw Normal View History

2023-09-01 14:33:54 +08:00
using System.Data.Odbc;
2023-10-02 23:24:56 +08:00
using BITFALL.Player.Equip;
2023-08-23 01:59:40 +08:00
using BITFALL.Player.Movement;
2023-06-08 14:09:50 +08:00
using BITKit;
using BITKit.Entities;
2023-10-20 19:31:12 +08:00
using BITKit.Selection;
2023-06-08 14:09:50 +08:00
using UnityEngine.InputSystem;
using BITKit.StateMachine;
2023-08-23 01:59:40 +08:00
using UnityEngine;
namespace BITFALL.Guns.States
2023-06-08 14:09:50 +08:00
{
[System.Serializable]
2023-10-20 19:31:12 +08:00
public sealed class None:GunState
{
}
[System.Serializable]
public class Movement : GunState
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
[SerializeField] private ExpectState<bool> _expectRun;
2023-10-20 19:31:12 +08:00
[Inject] protected ISelector _selector;
2023-08-23 01:59:40 +08:00
public override void OnStateEntry(IState old)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
switch (old)
{
case Aim:
2023-10-20 19:31:12 +08:00
root.animator.CrossFade(BITConstant.Player.Movement, 0.32f);
2023-08-23 01:59:40 +08:00
break;
default:
2023-10-20 19:31:12 +08:00
root.animator.CrossFade(BITConstant.Player.Movement, 0.16f);
2023-08-23 01:59:40 +08:00
break;
}
2023-10-20 19:31:12 +08:00
_selector.OnActive += OnActive;
2023-06-08 14:09:50 +08:00
}
public override void OnStateExit(IState old, IState newState)
{
base.OnStateExit(old, newState);
2023-10-20 19:31:12 +08:00
_selector.OnActive -= OnActive;
2023-06-08 14:09:50 +08:00
}
2023-08-27 02:58:19 +08:00
public override void OnStateUpdate(float deltaTime)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
if (root.animator[0].stateName is not BITConstant.Player.Movement) return;
2023-08-23 01:59:40 +08:00
if (root.expectFiring.shouldBe && root.fireInterval.AllowUpdate)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
root.Fire();
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
if (root.expectAiming.shouldBe && BITAppForUnity.AllowCursor == false)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
root.TransitionState<Aim>();
}else if (_expectRun)
{
root.TransitionState<Run>();
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
root.expectAiming.shouldBe = root.aimAction.action.IsPressed();
2023-06-08 14:09:50 +08:00
}
public void OnActive(ISelectable selectable)
{
2023-10-20 19:31:12 +08:00
root.animator.Play(BITConstant.Player.Interactive);
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
{
_expectRun = newState is IPlayerRunState or IPlayerSprintState;
2023-10-20 19:31:12 +08:00
if (newState is IPlayerSlideState)
{
root.TransitionState<Movement>();
root.animator.CrossFade(BITConstant.Player.Slide, 0.32f);
}
2023-08-23 01:59:40 +08:00
}
2023-06-08 14:09:50 +08:00
}
[System.Serializable]
2023-10-20 19:31:12 +08:00
public sealed class Run : GunState
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
private ExpectState<bool> _expectRun;
private ExpectState<bool> _expectSprint;
public override void OnStateEntry(IState old)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
root.animator.CrossFade(BITConstant.Player.Run, 0.16f);
2023-06-08 14:09:50 +08:00
2023-10-20 19:31:12 +08:00
root.inputActionGroup.RegisterCallback(root.aimAction, OnAim);
2023-06-08 14:09:50 +08:00
}
public override void OnStateExit(IState old, IState newState)
{
2023-10-20 19:31:12 +08:00
root.inputActionGroup.UnRegisterCallback(root.aimAction, OnAim);
2023-06-08 14:09:50 +08:00
}
2023-08-27 02:58:19 +08:00
public override void OnStateUpdate(float deltaTime)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
if (root.animator[0].stateName == BITConstant.Player.Movement)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
root.animator.CrossFade(BITConstant.Player.Run, 0.32f);
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
if(_expectSprint)
{
root.TransitionState<Sprint>();
}
else if(_expectRun == false)
{
root.TransitionState<Movement>();
}
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
private void OnAim(InputAction.CallbackContext context)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
if (context.started && BITAppForUnity.AllowCursor == false)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
root.TransitionState<Aim>();
}
}
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
{
_expectRun = newState is IPlayerRunState or IPlayerSprintState;
_expectSprint = newState is IPlayerSprintState;
}
}
[System.Serializable]
2023-10-20 19:31:12 +08:00
public sealed class Sprint : GunState
2023-08-23 01:59:40 +08:00
{
private ExpectState<bool> _expectSprint;
public override void OnStateEntry(IState old)
{
2023-10-20 19:31:12 +08:00
root.animator.CrossFade(BITConstant.Player.Sprint, 0.32f);
2023-08-23 01:59:40 +08:00
2023-10-20 19:31:12 +08:00
root.inputActionGroup.RegisterCallback(root.aimAction, OnAim);
2023-08-23 01:59:40 +08:00
}
public override void OnStateExit(IState old, IState newState)
{
2023-10-20 19:31:12 +08:00
root.inputActionGroup.UnRegisterCallback(root.aimAction, OnAim);
2023-08-23 01:59:40 +08:00
}
2023-08-27 02:58:19 +08:00
public override void OnStateUpdate(float deltaTime)
2023-08-23 01:59:40 +08:00
{
if(_expectSprint == false)
{
root.TransitionState<Movement>();
2023-06-08 14:09:50 +08:00
}
}
2023-08-23 01:59:40 +08:00
private void OnAim(InputAction.CallbackContext context)
2023-06-08 14:09:50 +08:00
{
if (context.started && BITAppForUnity.AllowCursor == false)
{
2023-08-23 01:59:40 +08:00
root.TransitionState<Aim>();
2023-06-08 14:09:50 +08:00
}
}
2023-08-23 01:59:40 +08:00
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
{
_expectSprint = newState is IPlayerSprintState;
}
2023-06-08 14:09:50 +08:00
}
[System.Serializable]
2023-10-20 19:31:12 +08:00
public sealed class Aim : GunState
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
[Inject]
2023-10-02 23:24:56 +08:00
private IEquipService _equipService;
2023-08-23 01:59:40 +08:00
public override void OnStateEntry(IState old)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
switch (old)
{
case IPlayerRunState:
case IPlayerSprintState:
2023-10-20 19:31:12 +08:00
root.animator.CrossFade(BITConstant.Player.Aim, 0.32f);
2023-08-23 01:59:40 +08:00
break;
default:
2023-10-20 19:31:12 +08:00
root.animator.CrossFade(BITConstant.Player.Aim, 0.16f);
2023-08-23 01:59:40 +08:00
break;
}
2023-10-02 23:24:56 +08:00
_equipService.Zoom.Allow = true;
2023-08-23 01:59:40 +08:00
_entityMovement.ExecuteCommand<PlayerCancelRunCommand>();
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
2023-08-27 02:58:19 +08:00
public override void OnStateUpdate(float deltaTime)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
if (root.expectFiring.shouldBe && root.fireInterval.AllowUpdate)
2023-06-08 14:09:50 +08:00
{
root.Fire();
}
if (BITAppForUnity.AllowCursor)
{
2023-08-23 01:59:40 +08:00
root.TransitionState<Movement>();
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
else if (root.expectAiming.shouldBe)
2023-06-08 14:09:50 +08:00
{
}
else
{
2023-08-23 01:59:40 +08:00
root.TransitionState<Movement>();
2023-06-08 14:09:50 +08:00
}
2023-10-02 23:24:56 +08:00
_equipService.Zoom.Value = root.aimAction.action.ReadValue<float>();
}
public override void OnStateExit(IState old, IState newState)
{
base.OnStateExit(old, newState);
_equipService.Zoom.Allow = false;
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
if (Enabled is false) return;
2023-09-01 14:33:54 +08:00
if (newState is not (IPlayerRunState or IPlayerSprintState)) return;
root.expectAiming.Reset();
root.TransitionState<Movement>();
2023-06-08 14:09:50 +08:00
}
}
[System.Serializable]
2023-10-20 19:31:12 +08:00
public sealed class Equip : GunState
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
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;
}
2023-08-23 01:59:40 +08:00
public override void OnStateEntry(IState old)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
completed = false;
root.animator.Play(BITConstant.Player.Equip,-1,0);
root.animator.animator.Update(0);
2023-06-08 14:09:50 +08:00
}
2023-08-27 02:58:19 +08:00
public override void OnStateUpdate(float deltaTime)
2023-06-08 14:09:50 +08:00
{
var state = root.animator[0];
2023-10-20 19:31:12 +08:00
if (state.stateName == BITConstant.Player.Equip)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
if (completed)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
root.TransitionState<Movement>();
2023-06-08 14:09:50 +08:00
}
}
2023-08-23 01:59:40 +08:00
else
{
2023-10-20 19:31:12 +08:00
root.animator.Play(BITConstant.Player.Equip);
2023-08-23 01:59:40 +08:00
}
}
}
[System.Serializable]
2023-10-20 19:31:12 +08:00
public sealed class Reload:GunState
2023-08-23 01:59:40 +08:00
{
public override void Initialize()
{
base.Initialize();
root.animator[0].onStateExit += OnAnimationStateExit;
}
private void OnAnimationStateExit(string obj)
{
if (Enabled is false) return;
2023-10-20 19:31:12 +08:00
if(obj is BITConstant.Player.Reload)
2023-08-23 01:59:40 +08:00
{
root.TransitionState<Movement>();
}
}
public override void OnStateEntry(IState old)
{
base.OnStateEntry(old);
_entityMovement.ExecuteCommand<PlayerCancelRunCommand>();
2023-10-20 19:31:12 +08:00
root.animator.Play(BITConstant.Player.Reload);
2023-08-23 01:59:40 +08:00
}
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
{
if (Enabled is false) return;
if (newState is IPlayerRunState or IPlayerSprintState)
{
root.TransitionState<Movement>();
}
2023-06-08 14:09:50 +08:00
}
}
2023-08-27 02:58:19 +08:00
[System.Serializable]
2023-10-20 19:31:12 +08:00
public sealed class Melee : GunState
2023-08-27 02:58:19 +08:00
{
public override void Initialize()
{
base.Initialize();
root.animator[0].onStateExit += OnAnimationStateExit;
}
private void OnAnimationStateExit(string obj)
{
if (Enabled is false) return;
2023-10-20 19:31:12 +08:00
if (obj is BITConstant.Player.Melee)
2023-08-27 02:58:19 +08:00
{
root.TransitionState<Movement>();
}
}
public override void OnStateEntry(IState old)
{
2023-10-20 19:31:12 +08:00
root.animator.Play(BITConstant.Player.Melee);
2023-09-01 14:33:54 +08:00
_entityMovement.ExecuteCommand(new PlayerDisableRunCommand(this));
2023-08-27 02:58:19 +08:00
}
2023-09-01 14:33:54 +08:00
public override void OnStateExit(IState old, IState newState)
{
_entityMovement.ExecuteCommand(new PlayerEnableRunCommand(this));
}
2023-08-27 02:58:19 +08:00
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]
2023-10-20 19:31:12 +08:00
public sealed class Climb:GunState
2023-08-27 02:58:19 +08:00
{
public override void OnStateEntry(IState old)
{
2023-10-20 19:31:12 +08:00
root.animator.Play(BITConstant.Player.Climb);
2023-08-27 02:58:19 +08:00
}
public override void OnMovementStateChanged(IEntityMovementState old, IEntityMovementState newState)
{
2023-10-20 19:31:12 +08:00
if(newState is IPlayerClimbState || (newState is IPlayerLinkState linkState && linkState.LinkArea == 4) )
2023-08-27 02:58:19 +08:00
{
root.TransitionState<Climb>();
}else if (Enabled)
{
root.TransitionState<Movement>();
}
}
}
2023-10-20 19:31:12 +08:00
[System.Serializable]
public sealed class Holster : GunState
{
public override void OnStateEntry(IState old)
{
root.animator.CrossFade(BITConstant.Player.Holster,0.16f);
}
}
2023-06-08 14:09:50 +08:00
}