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

344 lines
7.1 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using UnityEngine;
using SanAndreasUnity.Utilities;
2019-04-28 17:13:01 +00:00
using SanAndreasUnity.Net;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.Behaviours.Peds.States
{
/// <summary>
/// Base class for all states that are scripts.
/// </summary>
public abstract class BaseScriptState : MonoBehaviour, IPedState
{
protected Ped m_ped;
protected PedModel m_model { get { return m_ped.PlayerModel; } }
// protected StateMachine m_stateMachine;
protected new Transform transform { get { return m_ped.transform; } }
public bool IsActiveState { get { return m_ped.CurrentState == this; } }
protected bool m_isServer { get { return Net.NetStatus.IsServer; } }
2019-07-14 14:56:19 +00:00
protected bool m_isClientOnly => Net.NetStatus.IsClientOnly;
2019-04-28 17:13:01 +00:00
protected bool m_shouldSendButtonEvents { get { return !m_isServer && m_ped.IsControlledByLocalPlayer; } }
2020-05-31 17:07:22 +00:00
protected virtual void Awake ()
{
m_ped = this.GetComponentInParent<Ped> ();
}
protected virtual void OnEnable ()
{
}
protected virtual void OnDisable ()
{
}
protected virtual void Start ()
{
}
public virtual void OnBecameActive ()
{
}
public virtual void OnBecameInactive ()
{
}
public virtual bool RepresentsState (System.Type type)
{
var myType = this.GetType ();
return myType.Equals (type) || myType.IsSubclassOf (type);
}
public bool RepresentsState<T> () where T : IState
{
return this.RepresentsState (typeof(T));
}
public virtual void UpdateState() {
this.ConstrainPosition();
this.ConstrainRotation();
2020-05-31 17:07:22 +00:00
}
public virtual void PostUpdateState()
{
2020-05-31 17:07:22 +00:00
}
public virtual void LateUpdateState()
{
if (m_ped.Camera)
this.UpdateCamera ();
2020-05-31 17:07:22 +00:00
if (m_ped.shouldPlayAnims)
this.UpdateAnims ();
}
public virtual void FixedUpdateState()
{
this.UpdateHeading();
this.UpdateRotation();
this.UpdateMovement();
}
protected virtual void ConstrainPosition()
{
if (m_isServer)
m_ped.ConstrainPosition();
2020-05-31 17:07:22 +00:00
}
protected virtual void ConstrainRotation ()
{
if (m_isServer)
m_ped.ConstrainRotation();
2020-05-31 17:07:22 +00:00
}
protected virtual void UpdateHeading()
{
2019-04-25 22:10:23 +00:00
if (m_isServer)
m_ped.UpdateHeading ();
2020-05-31 17:07:22 +00:00
}
protected virtual void UpdateRotation()
{
2019-04-25 22:17:54 +00:00
if (m_isServer)
m_ped.UpdateRotation ();
2020-05-31 17:07:22 +00:00
}
protected virtual void UpdateMovement()
{
2019-04-25 22:21:05 +00:00
if (m_isServer)
m_ped.UpdateMovement ();
2020-05-31 17:07:22 +00:00
}
public virtual void UpdateCamera()
{
this.RotateCamera();
this.UpdateCameraZoom();
this.CheckCameraCollision ();
}
public virtual void RotateCamera()
{
BaseScriptState.RotateCamera(m_ped, m_ped.MouseMoveInput, m_ped.CameraClampValue.y);
}
public static void RotateCamera(Ped ped, Vector2 mouseDelta, float xAxisClampValue)
{
Camera cam = ped.Camera;
if (mouseDelta.sqrMagnitude < float.Epsilon)
return;
// cam.transform.Rotate( new Vector3(-mouseDelta.y, mouseDelta.x, 0f), Space.World );
var eulers = cam.transform.eulerAngles;
// eulers.z = 0f;
eulers.x += - mouseDelta.y;
eulers.y += mouseDelta.x;
// adjust x
if (eulers.x > 180f)
eulers.x -= 360f;
// clamp
if (xAxisClampValue > 0)
eulers.x = Mathf.Clamp(eulers.x, -xAxisClampValue, xAxisClampValue);
cam.transform.rotation = Quaternion.AngleAxis(eulers.y, Vector3.up)
* Quaternion.AngleAxis(eulers.x, Vector3.right);
}
public virtual Vector3 GetCameraFocusPos()
{
return m_ped.transform.position + Vector3.up * 0.5f;
}
public virtual float GetCameraDistance()
{
return m_ped.CameraDistance;
}
public virtual void UpdateCameraZoom()
{
2019-07-29 00:10:59 +00:00
m_ped.CameraDistance = Mathf.Clamp(m_ped.CameraDistance - m_ped.MouseScrollInput.y, PedManager.Instance.minCameraDistanceFromPed,
PedManager.Instance.maxCameraDistanceFromPed);
2020-05-31 17:07:22 +00:00
}
public virtual void CheckCameraCollision()
{
BaseScriptState.CheckCameraCollision (m_ped, this.GetCameraFocusPos (), -m_ped.Camera.transform.forward,
this.GetCameraDistance ());
}
public static void CheckCameraCollision(Ped ped, Vector3 castFrom, Vector3 castDir, float cameraDistance)
{
// cast a ray from ped to camera to see if it hits anything
// if so, then move the camera to hit point
Camera cam = ped.Camera;
float distance = cameraDistance;
var castRay = new Ray(castFrom, castDir);
RaycastHit hitInfo;
int ignoredLayerMask = PedManager.Instance.cameraRaycastIgnoredLayerMask;
2020-05-31 17:07:22 +00:00
if (Physics.SphereCast(castRay, 0.25f, out hitInfo, distance, ~ ignoredLayerMask))
2020-05-31 17:07:22 +00:00
{
distance = hitInfo.distance;
}
cam.transform.position = castRay.GetPoint(distance);
}
protected virtual void UpdateAnims()
{
}
public virtual void OnFireButtonPressed()
{
2019-07-08 00:07:40 +00:00
if (m_shouldSendButtonEvents)
PedSync.Local.OnFireButtonPressed();
2020-05-31 17:07:22 +00:00
}
public virtual void OnAimButtonPressed()
{
2019-07-08 00:07:40 +00:00
if (m_shouldSendButtonEvents)
PedSync.Local.OnAimButtonPressed();
2020-05-31 17:07:22 +00:00
}
public virtual void OnSubmitPressed()
{
2019-04-29 18:34:13 +00:00
if (m_shouldSendButtonEvents)
PedSync.Local.OnSubmitButtonPressed();
2020-05-31 17:07:22 +00:00
}
public virtual void OnJumpPressed()
{
}
public virtual void OnCrouchButtonPressed()
{
2019-04-28 17:13:01 +00:00
if (m_shouldSendButtonEvents)
PedSync.Local.OnCrouchButtonPressed();
2020-05-31 17:07:22 +00:00
}
public virtual void OnNextWeaponButtonPressed()
{
if (m_isServer)
m_ped.WeaponHolder.SwitchWeapon (true);
else if (m_shouldSendButtonEvents)
PedSync.Local.OnNextWeaponButtonPressed();
2020-05-31 17:07:22 +00:00
}
public virtual void OnPreviousWeaponButtonPressed()
{
if (m_isServer)
m_ped.WeaponHolder.SwitchWeapon (false);
else if (m_shouldSendButtonEvents)
PedSync.Local.OnPreviousWeaponButtonPressed();
2020-05-31 17:07:22 +00:00
}
public virtual void OnButtonPressed(string buttonName)
{
2020-02-11 17:15:49 +00:00
if (m_isServer)
this.OnButtonPressedOnServer(buttonName);
else if (m_shouldSendButtonEvents)
PedSync.Local.OnButtonPressed(buttonName);
}
2020-02-11 17:15:49 +00:00
protected virtual void OnButtonPressedOnServer(string buttonName)
{
}
2020-05-31 17:07:22 +00:00
public virtual void OnFlyButtonPressed()
{
}
public virtual void OnFlyThroughButtonPressed()
{
}
public virtual void OnSwitchedStateByServer(byte[] data)
{
m_ped.SwitchState(this.GetType());
}
public virtual byte[] GetAdditionalNetworkData()
{
return null;
}
2019-07-06 22:46:47 +00:00
public virtual void OnChangedWeaponByServer(int newSlot)
{
2019-07-06 22:46:47 +00:00
m_ped.WeaponHolder.SwitchWeapon(newSlot);
}
2019-07-08 14:17:55 +00:00
public virtual void OnWeaponFiredFromServer(Weapon weapon, Vector3 firePos)
{
2019-07-12 20:17:22 +00:00
// if (m_ped.IsControlledByLocalPlayer)
// return;
2019-07-12 16:41:38 +00:00
2019-07-08 14:17:55 +00:00
// update gun flash
if (weapon.GunFlash != null)
weapon.GunFlash.gameObject.SetActive (true);
weapon.UpdateGunFlashRotation ();
weapon.PlayFireSound();
}
2021-02-19 01:55:09 +00:00
public virtual Ped.DamageResult OnDamaged(DamageInfo damageInfo)
{
float amount = damageInfo.raycastHitTransform != null
? m_model.GetAmountOfDamageForBone(damageInfo.raycastHitTransform, damageInfo.amount)
: damageInfo.amount;
m_ped.Health -= amount;
if (m_ped.Health <= 0)
{
2021-01-04 00:28:58 +00:00
m_ped.KillingDamageInfo = damageInfo;
m_ped.Kill();
}
// notify clients
m_ped.SendDamagedEventToClients(damageInfo, amount);
2021-02-19 01:55:09 +00:00
return new Ped.DamageResult(amount);
}
2021-02-06 20:11:14 +00:00
public virtual void KillPed()
{
if (m_model != null)
{
m_model.DetachRagdoll(m_ped.KillingDamageInfo);
}
Object.Destroy(m_ped.gameObject);
}
}
2020-05-31 17:07:22 +00:00
}