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

136 lines
3.3 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.Behaviours.World;
using SanAndreasUnity.Importing.Animation;
2019-04-28 22:02:46 +00:00
using System.Linq;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.Behaviours.Peds.States
{
public class VehicleEnteringState : BaseVehicleState
{
2019-04-28 21:33:47 +00:00
Coroutine m_coroutine;
2019-05-25 19:50:07 +00:00
bool m_immediate = false;
2019-04-28 21:33:47 +00:00
2020-05-31 17:07:22 +00:00
2019-05-25 19:50:07 +00:00
public override void OnBecameActive()
{
base.OnBecameActive();
if (m_isServer) // clients will do this when vehicle gets assigned
this.EnterVehicleInternal();
2019-05-25 19:50:07 +00:00
}
2019-04-28 20:57:04 +00:00
public override void OnBecameInactive()
{
// restore everything
2019-05-25 19:50:07 +00:00
m_immediate = false;
2019-04-28 23:15:24 +00:00
this.Cleanup();
2019-04-28 20:57:04 +00:00
2019-04-28 21:33:47 +00:00
if (m_coroutine != null)
StopCoroutine(m_coroutine);
m_coroutine = null;
base.OnBecameInactive();
2020-05-31 17:07:22 +00:00
}
protected override void OnVehicleAssigned()
{
this.EnterVehicleInternal();
}
2020-05-31 17:07:22 +00:00
public bool TryEnterVehicle(Vehicle vehicle, Vehicle.SeatAlignment seatAlignment, bool immediate = false)
{
2019-04-28 20:17:37 +00:00
Net.NetStatus.ThrowIfNotOnServer();
2020-05-31 17:07:22 +00:00
if (!this.CanEnterVehicle (vehicle, seatAlignment))
return false;
2019-04-28 21:36:40 +00:00
this.EnterVehicle(vehicle, seatAlignment, immediate);
2020-05-31 17:07:22 +00:00
2019-04-28 21:36:40 +00:00
return true;
}
2020-05-31 17:07:22 +00:00
2019-04-28 22:22:41 +00:00
internal void EnterVehicle(Vehicle vehicle, Vehicle.SeatAlignment seatAlignment, bool immediate)
2019-04-28 21:36:40 +00:00
{
// first assign params
2019-05-25 19:50:07 +00:00
this.CurrentVehicle = vehicle;
2019-07-11 20:54:48 +00:00
this.CurrentVehicleSeatAlignment = seatAlignment;
2019-05-25 19:50:07 +00:00
m_immediate = immediate;
2020-05-31 17:07:22 +00:00
2019-05-25 21:38:12 +00:00
// switch state
2020-05-31 17:07:22 +00:00
m_ped.SwitchState<VehicleEnteringState>();
2019-05-25 19:50:07 +00:00
}
2020-05-31 17:07:22 +00:00
2019-05-25 19:50:07 +00:00
void EnterVehicleInternal()
{
Vehicle vehicle = this.CurrentVehicle;
Vehicle.Seat seat = this.CurrentVehicleSeat;
bool immediate = m_immediate;
2019-04-28 23:46:13 +00:00
2019-05-25 19:50:07 +00:00
2019-07-11 23:04:41 +00:00
BaseVehicleState.PreparePedForVehicle(m_ped, vehicle, seat);
2020-05-31 17:07:22 +00:00
2019-04-28 20:57:04 +00:00
if (seat.IsDriver)
2020-05-31 17:07:22 +00:00
{
2019-04-29 15:24:08 +00:00
// TODO: this should be done when ped enters the car - or, it should be removed, because
// vehicle should know if it has a driver
2020-05-31 17:07:22 +00:00
vehicle.StartControlling();
2019-05-05 21:12:42 +00:00
2019-05-22 16:59:16 +00:00
// if (m_isServer) {
// var p = Net.Player.GetOwningPlayer(m_ped);
// if (p != null)
// Net.NetManager.AssignAuthority(vehicle.gameObject, p);
// }
2020-05-31 17:07:22 +00:00
}
2019-04-28 21:33:47 +00:00
m_coroutine = StartCoroutine (EnterVehicleAnimation (seat, immediate));
2020-05-31 17:07:22 +00:00
}
private System.Collections.IEnumerator EnterVehicleAnimation(Vehicle.Seat seat, bool immediate)
{
var animIndex = seat.IsLeftHand ? AnimIndex.GetInLeft : AnimIndex.GetInRight;
2019-04-28 20:26:12 +00:00
m_model.VehicleParentOffset = Vector3.Scale(m_model.GetAnim(AnimGroup.Car, animIndex).RootEnd, new Vector3(-1, -1, -1));
2020-05-31 17:07:22 +00:00
if (!immediate)
{
2019-04-28 20:26:12 +00:00
var animState = m_model.PlayAnim(AnimGroup.Car, animIndex, PlayMode.StopAll);
2020-05-31 17:07:22 +00:00
animState.wrapMode = WrapMode.Once;
2019-04-29 15:52:03 +00:00
// wait until anim is finished or vehicle is destroyed
2019-07-11 21:26:45 +00:00
while (animState != null && animState.enabled && this.CurrentVehicle != null)
2020-05-31 17:07:22 +00:00
{
yield return new WaitForEndOfFrame();
}
}
2019-04-29 15:52:03 +00:00
// check if vehicle is alive
if (null == this.CurrentVehicle)
{
// vehicle destroyed in the meantime ? hmm... ped is a child of vehicle, so it should be
// destroyed as well ?
// anyway, switch to stand state
if (m_isServer)
m_ped.SwitchState<StandState>();
yield break;
}
2020-05-31 17:07:22 +00:00
2019-04-28 20:26:12 +00:00
// ped now completely entered the vehicle
2020-05-31 17:07:22 +00:00
2019-04-28 20:26:12 +00:00
// call method from VehicleSittingState - he will switch state
2019-04-28 22:02:46 +00:00
if (m_isServer)
m_ped.GetStateOrLogError<VehicleSittingState> ().EnterVehicle(this.CurrentVehicle, seat);
2020-05-31 17:07:22 +00:00
}
}
}