Files
BITFALL/Assets/Artists/Scripts/Equip/BITGunStates.cs
CortexCore cd02761be7 init
2023-06-08 14:09:50 +08:00

178 lines
5.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using BITKit.Entities;
using UnityEngine.UIElements;
using UnityEngine.InputSystem;
using BITKit.Animations;
using BITKit.StateMachine;
using BITFALL;
namespace BITFALL.Guns
{
[System.Serializable]
public class Idle : GunState, ISelectableCallback
{
public override void OnStateEnter(IState old)
{
root.animator.CrossFade(BITGun._Movement, 0.16f);
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()
{
if (root.animator[0].stateName is BITGun._Movement)
{
if (root.isFiring.shouldBe && root.fireUpdater)
{
root.Fire();
}
if (root.isAiming.shouldBe && BITAppForUnity.AllowCursor == false)
{
root.stateMachine.StateMachine.TransitionState<Aim>();
}
}
}
public override void OnMovementCallback(IMovementCallback callback)
{
switch (callback)
{
case OnRunCallback runCallback:
if (runCallback.started || runCallback.updated)
{
root.stateMachine.StateMachine.TransitionState<Run>();
}
break;
}
}
public void OnHover(ISelectable selectable)
{
}
public void OnActive(ISelectable selectable)
{
root.animator.Play(BITGun._Interactive);
}
public void OnInactive(ISelectable selectable)
{
}
}
[System.Serializable]
public class Run : GunState
{
public override void OnStateEnter(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()
{
if (root.animator[0].stateName == BITGun._Movement)
{
root.animator.CrossFade(BITGun._Run, 0.32f);
}
}
public override void OnMovementCallback(IMovementCallback callback)
{
switch (callback)
{
case OnRunCallback runCallback:
if (runCallback.canceled || runCallback.updated is false)
{
root.stateMachine.StateMachine.TransitionState<Idle>();
}
break;
}
}
void OnAim(InputAction.CallbackContext context)
{
if (context.started && BITAppForUnity.AllowCursor == false)
{
root.stateMachine.StateMachine.TransitionState<Aim>();
}
}
}
[System.Serializable]
public class Aim : GunState
{
public override void OnStateEnter(IState old)
{
root.animator.CrossFade(BITGun._Aim, 0.16f);
root.Entity
.Invoke<IMovementCancelAction>(
new CancelRunOrSprint()
);
}
public override void OnStateUpdate()
{
if (root.isFiring.shouldBe && root.fireUpdater)
{
root.Fire();
}
if (BITAppForUnity.AllowCursor)
{
root.stateMachine.StateMachine.TransitionState<Idle>();
}
else if (root.isAiming.shouldBe)
{
}
else
{
root.stateMachine.StateMachine.TransitionState<Idle>();
}
}
public override void OnMovementCallback(IMovementCallback callback)
{
switch (callback)
{
case OnRunCallback runCallback:
if (runCallback.started)
{
root.stateMachine.StateMachine.TransitionState<Idle>();
}
break;
}
}
}
[System.Serializable]
public class Equip : GunState
{
IntervalUpdate updater = new(0.16f);
public override void OnStateEnter(IState old)
{
updater.Reset();
root.animator.Play(BITGun._Equip);
}
public override void OnStateUpdate()
{
var state = root.animator[0];
if (updater.canUpdate)
{
if (state.stateName == BITGun._Equip)
{
if (state.currentState.normalizedTime >= 1)
{
root.stateMachine.StateMachine.TransitionState<Idle>();
}
}
else
{
root.animator.Play(BITGun._Equip);
}
}
}
}
}