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

116 lines
2.9 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-29 14:25:50 +00:00
using System.Linq;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.Behaviours.Peds.States
{
public class VehicleExitingState : BaseVehicleState
{
2019-04-29 00:28:40 +00:00
Coroutine m_coroutine;
2019-04-29 14:25:50 +00:00
bool m_isExitingImmediately = false;
2020-05-31 17:07:22 +00:00
2019-04-29 15:05:08 +00:00
public override void OnBecameActive()
{
base.OnBecameActive();
if (m_isServer) // clients will do this when vehicle gets assigned
this.ExitVehicleInternal();
2019-04-29 15:05:08 +00:00
}
2019-04-29 00:28:40 +00:00
public override void OnBecameInactive()
{
this.Cleanup();
2020-05-31 17:07:22 +00:00
2019-04-29 14:25:50 +00:00
m_isExitingImmediately = false;
2019-04-29 00:28:40 +00:00
if (m_coroutine != null)
StopCoroutine(m_coroutine);
m_coroutine = null;
2020-05-31 17:07:22 +00:00
2019-04-29 00:28:40 +00:00
base.OnBecameInactive();
2020-05-31 17:07:22 +00:00
}
protected override void OnVehicleAssigned()
{
this.ExitVehicleInternal();
}
2020-05-31 17:07:22 +00:00
public void ExitVehicle(bool immediate = false)
{
if (!m_ped.IsInVehicle || !m_ped.IsInVehicleSeat)
return;
// obtain current vehicle from Ped
this.CurrentVehicle = m_ped.CurrentVehicle;
2019-07-11 20:54:48 +00:00
this.CurrentVehicleSeatAlignment = m_ped.CurrentVehicleSeatAlignment;
2019-04-29 14:39:07 +00:00
2019-04-29 14:25:50 +00:00
m_isExitingImmediately = immediate;
2020-05-31 17:07:22 +00:00
// after obtaining parameters, switch to this state
m_ped.SwitchState<VehicleExitingState> ();
2019-04-29 14:25:50 +00:00
// we'll do the rest of the work when our state gets activated
2020-05-31 17:07:22 +00:00
2019-04-29 14:25:50 +00:00
}
void ExitVehicleInternal()
{
2019-07-11 23:04:41 +00:00
BaseVehicleState.PreparePedForVehicle(m_ped, this.CurrentVehicle, this.CurrentVehicleSeat);
2019-04-29 00:26:44 +00:00
2019-04-29 14:25:50 +00:00
// TODO: no need for this, vehicle should know when there is no driver
// TODO: but, right now, this should be included in cleanup
2019-04-28 23:49:21 +00:00
if (this.CurrentVehicleSeat.IsDriver)
this.CurrentVehicle.StopControlling();
2019-04-28 23:51:12 +00:00
if (m_isServer && this.CurrentVehicleSeat.IsDriver)
{
// remove authority
2019-05-22 16:59:16 +00:00
// Net.NetManager.RemoveAuthority(this.CurrentVehicle.gameObject);
}
2019-04-29 14:25:50 +00:00
m_coroutine = StartCoroutine (ExitVehicleAnimation (m_isExitingImmediately));
2020-05-31 17:07:22 +00:00
}
private System.Collections.IEnumerator ExitVehicleAnimation(bool immediate)
{
var seat = this.CurrentVehicleSeat;
var animIndex = seat.IsLeftHand ? AnimIndex.GetOutLeft : AnimIndex.GetOutRight;
2019-04-29 00:04:16 +00:00
m_model.VehicleParentOffset = Vector3.Scale(m_model.GetAnim(AnimGroup.Car, animIndex).RootStart, new Vector3(-1, -1, -1));
2020-05-31 17:07:22 +00:00
if (!immediate)
{
2019-04-29 00:04:16 +00:00
var animState = m_model.PlayAnim(AnimGroup.Car, animIndex, PlayMode.StopAll);
2020-05-31 17:07:22 +00:00
animState.wrapMode = WrapMode.Once;
// wait until anim finishes or stops
2019-07-11 21:26:45 +00:00
while (animState != null && animState.enabled)
2020-05-31 17:07:22 +00:00
yield return new WaitForEndOfFrame();
}
2019-04-29 00:04:16 +00:00
// ped now completely exited the vehicle
2020-05-31 17:07:22 +00:00
2019-04-29 00:04:16 +00:00
m_ped.transform.localPosition = m_model.VehicleParentOffset;
2020-05-31 17:07:22 +00:00
m_ped.transform.localRotation = Quaternion.identity;
2019-04-29 00:04:16 +00:00
m_model.VehicleParentOffset = Vector3.zero;
2019-04-29 14:39:07 +00:00
// now switch to other state
// when our state gets deactivated, it will cleanup everything
if (m_isServer)
m_ped.SwitchState<StandState> ();
2020-05-31 17:07:22 +00:00
}
}
}