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

184 lines
4.4 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-07-13 15:38:07 +00:00
Vector3 m_vehicleParentOffset = Vector3.zero;
2019-07-14 14:56:19 +00:00
//Vector3 m_rootFramePos = Vector3.zero;
2019-07-13 15:38:07 +00:00
2020-05-31 17:07:22 +00:00
2019-05-25 22:08:56 +00:00
public override void OnBecameActive()
{
base.OnBecameActive();
if (m_isServer) // clients will do this when vehicle gets assigned
this.EnterVehicleInternal();
2019-05-25 22:08:56 +00:00
}
2019-04-28 22:37:00 +00:00
public override void OnBecameInactive()
{
2019-07-13 15:38:07 +00:00
m_vehicleParentOffset = Vector3.zero;
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-07-13 16:29:14 +00:00
protected override void GetAdditionalNetworkData(Mirror.NetworkWriter writer)
{
base.GetAdditionalNetworkData(writer);
writer.Write(m_vehicleParentOffset);
}
protected override void ReadNetworkData(Mirror.NetworkReader reader)
{
base.ReadNetworkData(reader);
m_vehicleParentOffset = reader.ReadVector3();
}
protected override void OnVehicleAssigned()
{
this.EnterVehicleInternal();
}
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;
2019-07-13 16:29:14 +00:00
if (m_isServer)
m_vehicleParentOffset = m_model.VehicleParentOffset;
2019-07-14 14:56:19 +00:00
else if (m_isClientOnly)
m_model.VehicleParentOffset = m_vehicleParentOffset;
2020-05-31 17:07:22 +00:00
2019-07-11 23:04:41 +00:00
BaseVehicleState.PreparePedForVehicle(m_ped, vehicle, seat);
2019-04-28 23:46:13 +00:00
2019-07-14 00:20:30 +00:00
// save root frame position
2019-07-14 00:55:46 +00:00
2019-07-14 14:56:19 +00:00
// this.UpdateDriverAnim(); // play driver anim
// m_model.AnimComponent.Sample(); // sample it
// if (m_model.RootFrame != null)
// m_rootFramePos = m_model.RootFrame.transform.localPosition; // save root frame position
// this.UpdateAnimsInternal(); // restore the correct anim
// m_model.AnimComponent.Sample();
// play anims
this.UpdateAnimsInternal();
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-07-11 21:18:25 +00:00
if (!this.IsActiveState)
return;
2019-04-28 22:37:00 +00:00
2019-07-14 00:20:30 +00:00
this.UpdateAnimsInternal();
}
void UpdateAnimsInternal()
{
2019-07-11 21:16:26 +00:00
var seat = this.CurrentVehicleSeat;
2019-07-11 22:02:16 +00:00
if (seat != null)
{
if (seat.IsDriver)
2019-07-14 00:55:46 +00:00
this.UpdateDriverAnim ();
2019-07-11 22:02:16 +00:00
else
2019-07-14 00:55:46 +00:00
this.UpdatePassengerAnim();
2019-07-11 22:02:16 +00:00
}
2020-05-31 17:07:22 +00:00
}
2019-07-14 00:55:46 +00:00
protected virtual void UpdateDriverAnim()
2020-05-31 17:07:22 +00:00
{
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
}
2019-07-14 00:55:46 +00:00
protected virtual void UpdatePassengerAnim()
2019-07-11 21:53:34 +00:00
{
2019-07-11 22:02:16 +00:00
// if (this.CurrentVehicleSeat.IsDriver)
// {
// m_model.PlayAnim(AnimGroup.Car, AnimIndex.Sit, PlayMode.StopAll);
// }
// else
2019-07-11 21:53:34 +00:00
{
2019-07-14 14:56:19 +00:00
2019-07-13 15:44:53 +00:00
// we have to assign offset every frame, because it can be changed when ped model changes
2019-07-14 14:56:19 +00:00
// m_model.VehicleParentOffset = m_vehicleParentOffset;
2019-07-14 00:20:30 +00:00
// same goes for root frame position
2019-07-14 14:56:19 +00:00
if (m_model.RootFrame != null && m_model.UnnamedFrame != null)
{
//m_model.RootFrame.transform.localPosition = m_rootFramePos;
m_model.RootFrame.transform.localPosition = - m_model.UnnamedFrame.transform.localPosition;
}
2019-07-13 15:44:53 +00:00
2019-07-11 21:53:34 +00:00
m_model.PlayAnim(AnimGroup.Car, AnimIndex.SitPassenger, PlayMode.StopAll);
}
}
2020-05-31 17:07:22 +00:00
public override void UpdateCameraZoom()
{
2019-07-29 00:10:59 +00:00
m_ped.CameraDistanceVehicle = Mathf.Clamp(m_ped.CameraDistanceVehicle - m_ped.MouseScrollInput.y, PedManager.Instance.minCameraDistanceFromPed,
PedManager.Instance.maxCameraDistanceFromPed);
2020-05-31 17:07:22 +00:00
}
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
}
}
}