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

445 lines
14 KiB
C#
Raw Normal View History

2023-11-30 00:23:23 +08:00
using System;
2023-11-21 18:05:18 +08:00
using System.Collections.Generic;
2023-09-01 14:33:54 +08:00
using System.Data.Odbc;
2023-11-21 18:05:18 +08:00
using System.Linq;
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;
2023-10-31 18:07:15 +08:00
// ReSharper disable UnassignedField.Local
2023-08-23 01:59:40 +08:00
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 sealed class Movement : GunState
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
[SerializeField] private ExpectState<bool> _expectRun;
[Inject] private ISelector _selector;
2023-11-15 23:54:54 +08:00
private float requestBoltActionElapsedTime;
2023-10-31 18:07:15 +08:00
private bool boltActionImmediately;
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)
{
2023-11-15 23:54:54 +08:00
case Movement:
case Equip:
break;
2023-08-23 01:59:40 +08:00
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-10-31 18:07:15 +08:00
boltActionImmediately = root.RequireBolt;
2023-06-08 14:09:50 +08:00
}
public override void OnStateExit(IState old, IState newState)
{
2023-11-15 23:54:54 +08:00
requestBoltActionElapsedTime = 0;
2023-06-08 14:09:50 +08:00
base.OnStateExit(old, newState);
2023-10-20 19:31:12 +08:00
_selector.OnActive -= OnActive;
2023-06-08 14:09:50 +08:00
}
2023-11-15 23:54:54 +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-11-15 23:54:54 +08:00
if (root.RequireBolt)
requestBoltActionElapsedTime += deltaTime;
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>();
2023-11-15 23:54:54 +08:00
return;
}
if (_expectRun)
2023-08-23 01:59:40 +08:00
{
root.TransitionState<Run>();
2023-11-15 23:54:54 +08:00
return;
}
switch (root.RequireBolt)
2023-10-31 18:07:15 +08:00
{
2023-11-15 23:54:54 +08:00
case var _ when boltActionImmediately:
case var _ when requestBoltActionElapsedTime > 1f:
case var _ when root.animator[4].stateName == BITConstant.Player.Empty && requestBoltActionElapsedTime>0.32f:
root.TransitionState<Reload>();
break;
2023-06-08 14:09:50 +08:00
}
}
2023-11-15 23:54:54 +08:00
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);
}else if (Enabled && newState is IPlayerDodgeState)
{
root.animator.Play(BITConstant.Player.Dodge);
2023-10-20 19:31:12 +08:00
}
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-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-11-15 23:54:54 +08:00
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,true));
2023-11-30 00:23:23 +08:00
_entityMovement.ExecuteCommand(new PlayerLimitMoveSpeedCommand()
{
Id = BITHash.Player.Aim,
Limit =true,
Speed = root._gun.InitialAimMovementSpeed,
});
2023-11-02 20:58:55 +08:00
}
public override void OnStateExit(IState old, IState newState)
{
base.OnStateExit(old, newState);
2023-11-15 23:54:54 +08:00
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,false));
2023-11-30 00:23:23 +08:00
_entityMovement.ExecuteCommand(new PlayerLimitMoveSpeedCommand()
{
Id = BITHash.Player.Aim,
Limit =false,
Speed = root._gun.InitialAimMovementSpeed,
});
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-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;
2023-11-21 18:05:18 +08:00
[Inject]
private IPlayerEquipSelector _equipSelector;
2023-10-20 19:31:12 +08:00
public override void Initialize()
{
base.Initialize();
root.animator[0].onStateExit += OnAnimationStateExit;
2023-11-21 18:05:18 +08:00
_equipSelector.OnUpdateEquip += OnUpdateEquip;
2023-10-20 19:31:12 +08:00
}
2023-11-21 18:05:18 +08:00
private readonly List<int> _drawed = new();
private void OnUpdateEquip(IDictionary<int, IBasicItem> obj)
{
foreach (var x in _drawed.ToArray())
{
if (obj.Any(pair => _drawed.Contains(pair.Value.Id)) is false)
{
_drawed.Remove(x);
}
}
}
2023-10-20 19:31:12 +08:00
private void OnAnimationStateExit(string obj)
{
2023-11-21 18:05:18 +08:00
if (obj is BITConstant.Player.Draw) completed = true;
2023-10-20 19:31:12 +08:00
}
2023-08-23 01:59:40 +08:00
public override void OnStateEntry(IState old)
2023-06-08 14:09:50 +08:00
{
2023-11-21 18:05:18 +08:00
base.OnStateEntry(old);
2023-10-20 19:31:12 +08:00
completed = false;
2023-11-21 18:05:18 +08:00
if (_drawed.TryAdd(root.Item.Id))
{
2023-11-30 00:23:23 +08:00
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.Draw);
//root.animator.Play(BITConstant.Player.Draw, -1, 0);
2023-11-21 18:05:18 +08:00
}
else
{
2023-11-30 00:23:23 +08:00
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.QuickDraw);
//root.animator.Play(BITConstant.Player.QuickDraw,-1,0);
2023-11-21 18:05:18 +08:00
}
2023-11-15 23:54:54 +08:00
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,true));
2023-06-08 14:09:50 +08:00
}
2023-11-15 23:54:54 +08:00
public override void OnStateExit(IState old, IState newState)
{
base.OnStateExit(old, newState);
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,false));
}
2023-08-27 02:58:19 +08:00
public override void OnStateUpdate(float deltaTime)
2023-06-08 14:09:50 +08:00
{
2023-11-15 23:54:54 +08:00
if (completed)
2023-06-08 14:09:50 +08:00
{
2023-11-15 23:54:54 +08:00
root.TransitionState<Movement>();
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);
2023-11-15 23:54:54 +08:00
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,true));
2023-10-31 18:07:15 +08:00
if (root.RequireBolt)
{
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.BoltAction);
}
else
{
root.UnityEntity.Invoke(Constant.Animation.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
2023-10-31 18:07:15 +08:00
public override void OnStateExit(IState old, IState newState)
{
root.RequireBolt = false;
base.OnStateExit(old, newState);
2023-11-15 23:54:54 +08:00
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,false));
2023-10-31 18:07:15 +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-11-15 23:54:54 +08:00
base.OnStateEntry(old);
2023-10-30 01:25:53 +08:00
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.Melee);
2023-11-15 23:54:54 +08:00
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,true));
2023-11-30 00:23:23 +08:00
_entityMovement.ExecuteCommand(new PlayerFocusCommand()
{
Focus = true,
Sender = 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)
{
2023-11-15 23:54:54 +08:00
base.OnStateExit(old,newState);
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,false));
2023-11-30 00:23:23 +08:00
_entityMovement.ExecuteCommand(new PlayerFocusCommand()
{
Focus = false,
Sender = this
});
2023-09-01 14:33:54 +08:00
}
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-11-15 23:54:54 +08:00
base.OnStateEntry(old);
2023-10-20 19:31:12 +08:00
root.animator.Play(BITConstant.Player.Climb);
2023-11-15 23:54:54 +08:00
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,true));
}
public override void OnStateExit(IState old, IState newState)
{
base.OnStateExit(old, newState);
_entityMovement.ExecuteCommand<PlayerPauseRunCommand>(new(this,false));
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)
{
2023-11-30 00:23:23 +08:00
//root.animator.CrossFade(BITConstant.Player.Holster,0.16f);
root.UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.Holster);
2023-10-20 19:31:12 +08:00
}
}
2023-06-08 14:09:50 +08:00
}