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

128 lines
3.1 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;
2020-05-31 17:07:22 +00:00
2019-04-28 20:57:04 +00:00
public override void OnBecameInactive()
{
// restore everything
2019-04-28 21:17:50 +00:00
if (!m_ped.IsInVehicle)
{
m_ped.characterController.enabled = true;
// restore seat's occupying ped ? - no
m_ped.transform.SetParent(null, true);
m_model.IsInVehicle = false;
}
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
}
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
{
2020-05-31 17:07:22 +00:00
Vehicle.Seat seat = vehicle.GetSeat (seatAlignment);
// switch state here
m_ped.SwitchState<VehicleEnteringState>();
this.CurrentVehicle = vehicle;
this.CurrentVehicleSeat = seat;
seat.OccupyingPed = m_ped;
m_ped.characterController.enabled = false;
m_ped.transform.SetParent(seat.Parent);
m_ped.transform.localPosition = Vector3.zero;
m_ped.transform.localRotation = Quaternion.identity;
2019-04-28 20:57:04 +00:00
if (seat.IsDriver)
2020-05-31 17:07:22 +00:00
{
2019-04-28 20:57:04 +00:00
// TODO: this should be done when ped enters the car
2020-05-31 17:07:22 +00:00
vehicle.StartControlling();
}
2019-04-28 20:26:12 +00:00
m_model.IsInVehicle = true;
2020-05-31 17:07:22 +00:00
if (!vehicle.IsNightToggled && WorldController.IsNight)
vehicle.IsNightToggled = true;
else if (vehicle.IsNightToggled && !WorldController.IsNight)
vehicle.IsNightToggled = false;
2019-04-28 21:17:50 +00:00
// send message to clients
2019-04-28 22:02:46 +00:00
if (m_isServer)
{
if (!immediate)
{
foreach(var pedSync in Net.Player.AllPlayers.Select(p => p.GetComponent<Net.PedSync>()))
pedSync.PedStartedEnteringVehicle(m_ped);
}
}
2019-04-28 21:17:50 +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;
// TODO: also check if this state is still active state
while (animState.enabled)
{
yield return new WaitForEndOfFrame();
}
}
// TODO: check if this state is still active, and if vehicle is alive
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
}
}
}