From abac2b54f53c9a5b479341cf95f8146be1740409 Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 26 Dec 2021 23:16:14 +0100 Subject: [PATCH] smooth out switching between aim movement states --- .../Ped/States/BaseAimMovementState.cs | 28 +++++++++++++++++-- .../Behaviours/Ped/States/StandAimState.cs | 4 ++- Assets/Scripts/Behaviours/PedManager.cs | 2 ++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs b/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs index 810620fe..9ac36474 100644 --- a/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs @@ -23,9 +23,13 @@ namespace SanAndreasUnity.Behaviours.Peds.States protected bool m_wasAimingBackWithAWAWeapon = false; protected float m_timeSinceAimingBackWithAWAWeapon = 0f; + public virtual float TimeUntilStateCanBeSwitchedToOtherAimMovementState => PedManager.Instance.timeUntilAimMovementStateCanBeSwitchedToOtherAimMovementState; + public virtual float TimeUntilStateCanBeEnteredFromOtherAimMovementState => PedManager.Instance.timeUntilAimMovementStateCanBeEnteredFromOtherAimMovementState; - public override void OnBecameActive() + + + public override void OnBecameActive() { base.OnBecameActive(); @@ -108,10 +112,30 @@ namespace SanAndreasUnity.Behaviours.Peds.States protected virtual bool SwitchToOtherAimMovementState () { - BaseAimMovementState.SwitchToAimMovementStateBasedOnInput (m_ped); + System.Type type = GetAimMovementStateToSwitchToBasedOnInput(m_ped); + var state = (BaseAimMovementState)m_ped.GetStateOrLogError(type); + + if (!EnoughTimePassedToSwitchBetweenAimMovementStates(this, state)) + return false; + + m_ped.SwitchState(type); + return ! this.IsActiveState; } + public static bool EnoughTimePassedToSwitchBetweenAimMovementStates( + BaseAimMovementState currentState, + BaseAimMovementState targetState) + { + if (currentState.TimeSinceActivated < currentState.TimeUntilStateCanBeSwitchedToOtherAimMovementState) + return false; + + if (targetState.TimeSinceDeactivated < targetState.TimeUntilStateCanBeEnteredFromOtherAimMovementState) + return false; + + return true; + } + protected virtual bool SwitchToFallingState () { return false; diff --git a/Assets/Scripts/Behaviours/Ped/States/StandAimState.cs b/Assets/Scripts/Behaviours/Ped/States/StandAimState.cs index 282d6c49..25bf2195 100644 --- a/Assets/Scripts/Behaviours/Ped/States/StandAimState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/StandAimState.cs @@ -9,8 +9,10 @@ namespace SanAndreasUnity.Behaviours.Peds.States { public override AnimId aimWithArm_LowerAnim { get { return m_ped.CurrentWeapon.IdleAnim; } } + public override float TimeUntilStateCanBeSwitchedToOtherAimMovementState => 0f; - public override void OnBecameActive () + + public override void OnBecameActive () { base.OnBecameActive (); // m_ped.PlayerModel.PlayAnim (AnimGroup.MyWalkCycle, AnimIndex.GUN_STAND); diff --git a/Assets/Scripts/Behaviours/PedManager.cs b/Assets/Scripts/Behaviours/PedManager.cs index ad26adaf..86714191 100644 --- a/Assets/Scripts/Behaviours/PedManager.cs +++ b/Assets/Scripts/Behaviours/PedManager.cs @@ -24,6 +24,8 @@ namespace SanAndreasUnity.Behaviours public float minTimeToReturnToNonAimStateFromAimState = 0.33f; public float timeUntilMovementStateCanBeSwitchedToOtherMovementState = 0.166f; public float timeUntilMovementStateCanBeEnteredFromOtherMovementState = 0.166f; + public float timeUntilAimMovementStateCanBeSwitchedToOtherAimMovementState = 0.166f; + public float timeUntilAimMovementStateCanBeEnteredFromOtherAimMovementState = 0.166f; [Header("Camera")]