SanAndreasUnity/Assets/Scripts/Behaviours/Ped/States/BaseMovementState.cs

163 lines
3.8 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Importing.Animation;
namespace SanAndreasUnity.Behaviours.Peds.States
{
/// <summary>
/// Base class for all movement states.
/// </summary>
public abstract class BaseMovementState : BaseScriptState
{
public abstract AnimId movementAnim { get; }
public abstract AnimId movementWeaponAnim { get; }
public virtual float TimeUntilStateCanBeSwitchedToOtherMovementState => PedManager.Instance.timeUntilMovementStateCanBeSwitchedToOtherMovementState;
public virtual float TimeUntilStateCanBeEnteredFromOtherMovementState => PedManager.Instance.timeUntilMovementStateCanBeEnteredFromOtherMovementState;
2020-05-31 17:07:22 +00:00
public override void UpdateState() {
base.UpdateState ();
if (!this.IsActiveState)
return;
if (m_isServer)
this.SwitchToMovementState ();
2020-05-31 17:07:22 +00:00
if (!this.IsActiveState)
return;
if (m_isServer)
this.SwitchToAimState ();
2020-05-31 17:07:22 +00:00
}
protected virtual void SwitchToMovementState()
{
System.Type type = BaseMovementState.GetMovementStateToSwitchToBasedOnInput(m_ped);
var state = (BaseMovementState) m_ped.GetStateOrLogError(type);
2021-12-19 20:17:31 +00:00
if (!EnoughTimePassedToSwitchBetweenMovementStates(this, state))
return;
m_ped.SwitchState(type);
2020-05-31 17:07:22 +00:00
}
2021-12-19 20:17:31 +00:00
public static bool EnoughTimePassedToSwitchBetweenMovementStates(
BaseMovementState currentState,
BaseMovementState targetState)
{
if (currentState.TimeSinceActivated <= currentState.TimeUntilStateCanBeSwitchedToOtherMovementState)
return false;
if (targetState.TimeSinceDeactivated <= targetState.TimeUntilStateCanBeEnteredFromOtherMovementState)
return false;
return true;
}
2020-05-31 17:07:22 +00:00
public static void SwitchToMovementStateBasedOnInput (Ped ped)
{
2021-12-19 00:17:00 +00:00
System.Type type = GetMovementStateToSwitchToBasedOnInput(ped);
ped.SwitchState(type);
}
2020-05-31 17:07:22 +00:00
2021-12-19 00:17:00 +00:00
public static System.Type GetMovementStateToSwitchToBasedOnInput(Ped ped)
{
2020-05-31 17:07:22 +00:00
if (ped.IsJumpOn && ped.GetStateOrLogError<JumpState>().CanJump())
{
2021-12-19 00:17:00 +00:00
return typeof(JumpState);
2020-05-31 17:07:22 +00:00
}
else if (ped.IsWalkOn)
{
2021-12-19 00:17:00 +00:00
return typeof(WalkState);
2020-05-31 17:07:22 +00:00
}
else if (ped.IsRunOn)
{
2021-12-19 00:17:00 +00:00
return typeof(RunState);
2020-05-31 17:07:22 +00:00
}
else if (ped.IsSprintOn)
{
if (ped.CurrentWeapon != null && !ped.CurrentWeapon.CanSprintWithIt)
2021-12-19 00:17:00 +00:00
return typeof(RunState);
2020-05-31 17:07:22 +00:00
else
2021-12-19 00:17:00 +00:00
return typeof(SprintState);
2020-05-31 17:07:22 +00:00
}
else
{
2021-12-19 00:17:00 +00:00
return typeof(StandState);
2020-05-31 17:07:22 +00:00
}
}
protected virtual void SwitchToAimState()
{
if (m_ped.IsAimOn && m_ped.IsHoldingWeapon && EnoughTimePassedToSwitchToAimState(m_ped))
2020-05-31 17:07:22 +00:00
{
BaseAimMovementState.SwitchToAimMovementStateBasedOnInput (m_ped);
}
}
public static bool EnoughTimePassedToSwitchToAimState(Ped ped)
{
var aimStates = ped.CachedAimStates;
for (int i = 0; i < aimStates.Count; i++)
{
if (Time.time - aimStates[i].LastTimeWhenDeactivated < PedManager.Instance.minTimeToReturnToAimState)
return false;
}
return true;
}
2020-05-31 17:07:22 +00:00
protected override void UpdateAnims ()
{
if (m_ped.CurrentWeapon != null)
{
m_ped.PlayerModel.PlayAnim (this.movementWeaponAnim);
}
else
{
m_ped.PlayerModel.PlayAnim (this.movementAnim);
}
}
public override void OnSubmitPressed() {
// try to enter vehicle
2019-04-30 21:29:21 +00:00
if (m_isServer)
m_ped.TryEnterVehicleInRange ();
else
base.OnSubmitPressed();
2020-05-31 17:07:22 +00:00
}
public override void OnCrouchButtonPressed ()
{
2019-04-25 22:46:35 +00:00
if (m_isServer)
m_ped.SwitchState<CrouchState>();
2019-04-28 17:13:01 +00:00
else
base.OnCrouchButtonPressed();
2020-05-31 17:07:22 +00:00
}
public override void OnFlyButtonPressed ()
{
2019-04-25 22:46:35 +00:00
if (m_isServer)
m_ped.GetStateOrLogError<FlyState> ().EnterState (false);
2020-05-31 17:07:22 +00:00
}
public override void OnFlyThroughButtonPressed ()
{
2019-04-25 22:46:35 +00:00
if (m_isServer)
m_ped.GetStateOrLogError<FlyState> ().EnterState (true);
2020-05-31 17:07:22 +00:00
}
}
}