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

120 lines
2.6 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Behaviours.Vehicles;
using SanAndreasUnity.Importing.Animation;
2019-04-28 23:46:13 +00:00
using System.Linq;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.Behaviours.Peds.States
{
public class VehicleSittingState : BaseVehicleState
{
2019-05-25 22:08:56 +00:00
public override void OnBecameActive()
{
base.OnBecameActive();
this.EnterVehicleInternal();
}
2019-04-28 22:37:00 +00:00
public override void OnBecameInactive()
{
2019-04-28 23:15:24 +00:00
this.Cleanup();
2020-05-31 17:07:22 +00:00
2019-04-28 22:37:00 +00:00
base.OnBecameInactive();
2020-05-31 17:07:22 +00:00
}
2019-04-28 23:46:13 +00:00
public void EnterVehicle(Vehicle vehicle, Vehicle.SeatAlignment seatAlignment)
{
this.EnterVehicle(vehicle, vehicle.GetSeat(seatAlignment));
}
2020-05-31 17:07:22 +00:00
public void EnterVehicle(Vehicle vehicle, Vehicle.Seat seat)
{
this.CurrentVehicle = vehicle;
2019-07-11 20:54:48 +00:00
this.CurrentVehicleSeatAlignment = seat.Alignment;
2020-05-31 17:07:22 +00:00
m_ped.SwitchState<VehicleSittingState> ();
2019-05-25 22:08:56 +00:00
}
void EnterVehicleInternal()
{
Vehicle vehicle = this.CurrentVehicle;
Vehicle.Seat seat = this.CurrentVehicleSeat;
2020-05-31 17:07:22 +00:00
2019-04-29 00:26:44 +00:00
VehicleEnteringState.PreparePedForVehicle(m_ped, vehicle, seat);
2019-04-28 23:46:13 +00:00
2020-05-31 17:07:22 +00:00
if (seat.IsDriver)
{
2019-04-28 22:25:45 +00:00
m_model.PlayAnim(AnimGroup.Car, AnimIndex.Sit, PlayMode.StopAll);
2020-05-31 17:07:22 +00:00
}
else
{
2019-04-28 22:25:45 +00:00
m_model.PlayAnim(AnimGroup.Car, AnimIndex.SitPassenger, PlayMode.StopAll);
2020-05-31 17:07:22 +00:00
}
}
2019-04-28 22:37:00 +00:00
public override void OnSubmitPressed()
{
2020-05-31 17:07:22 +00:00
// exit the vehicle
2019-04-28 22:37:00 +00:00
if (m_isServer)
m_ped.ExitVehicle();
else
base.OnSubmitPressed();
2020-05-31 17:07:22 +00:00
}
public override void UpdateState() {
base.UpdateState();
2019-04-28 22:37:00 +00:00
// check if this is still active state ?
2019-07-11 21:16:26 +00:00
var seat = this.CurrentVehicleSeat;
if (seat != null && seat.IsDriver)
2020-05-31 17:07:22 +00:00
this.UpdateWheelTurning ();
}
protected virtual void UpdateWheelTurning()
{
2019-04-28 22:25:45 +00:00
m_model.VehicleParentOffset = Vector3.zero;
2020-05-31 17:07:22 +00:00
2019-04-28 22:25:45 +00:00
var driveState = this.CurrentVehicle.Steering > 0 ? AnimIndex.DriveRight : AnimIndex.DriveLeft;
2020-05-31 17:07:22 +00:00
2019-04-28 22:25:45 +00:00
var state = m_model.PlayAnim(AnimGroup.Car, driveState, PlayMode.StopAll);
2020-05-31 17:07:22 +00:00
state.speed = 0.0f;
state.wrapMode = WrapMode.ClampForever;
2019-04-28 22:25:45 +00:00
state.time = Mathf.Lerp(0.0f, state.length, Mathf.Abs(this.CurrentVehicle.Steering));
2020-05-31 17:07:22 +00:00
}
public override void UpdateCameraZoom()
{
m_ped.CameraDistanceVehicle = Mathf.Clamp(m_ped.CameraDistanceVehicle - m_ped.MouseScrollInput.y, 2.0f, 32.0f);
}
public override void CheckCameraCollision ()
{
BaseScriptState.CheckCameraCollision(m_ped, this.GetCameraFocusPos(), -m_ped.Camera.transform.forward,
m_ped.CameraDistanceVehicle);
}
public new Vector3 GetCameraFocusPos()
{
2019-07-11 21:00:08 +00:00
if (m_ped.CurrentVehicle != null)
return m_ped.CurrentVehicle.transform.position;
else
return base.GetCameraFocusPos();
2020-05-31 17:07:22 +00:00
}
}
}