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

124 lines
2.3 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 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()
{
BaseMovementState.SwitchToMovementStateBasedOnInput (m_ped);
}
public static void SwitchToMovementStateBasedOnInput (Ped ped)
{
if (ped.IsJumpOn && ped.GetStateOrLogError<JumpState>().CanJump())
{
ped.GetState<JumpState>().Jump();
}
else if (ped.IsWalkOn)
{
ped.SwitchState<WalkState> ();
}
else if (ped.IsRunOn)
{
ped.SwitchState<RunState> ();
}
else if (ped.IsSprintOn)
{
if (ped.CurrentWeapon != null && !ped.CurrentWeapon.CanSprintWithIt)
ped.SwitchState<StandState> ();
else
ped.SwitchState<SprintState> ();
}
else
{
ped.SwitchState<StandState> ();
}
}
protected virtual void SwitchToAimState()
{
if (m_ped.IsAimOn && m_ped.IsHoldingWeapon)
{
BaseAimMovementState.SwitchToAimMovementStateBasedOnInput (m_ped);
}
}
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
}
}
}