2020-05-31 17:07:22 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using SanAndreasUnity.Behaviours.Vehicles;
|
|
|
|
|
using SanAndreasUnity.Behaviours.World;
|
|
|
|
|
using SanAndreasUnity.Importing.Animation;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using SanAndreasUnity.Behaviours.Peds.States;
|
2019-04-25 19:58:21 +00:00
|
|
|
|
using SanAndreasUnity.Net;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours
|
|
|
|
|
{
|
|
|
|
|
|
2019-04-23 23:25:49 +00:00
|
|
|
|
public partial class Ped {
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
[SerializeField] private float m_enterVehicleRadius = 2.0f;
|
2019-06-23 00:52:48 +00:00
|
|
|
|
public float EnterVehicleRadius { get { return m_enterVehicleRadius; } set { m_enterVehicleRadius = value; } }
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
public Vehicle CurrentVehicle {
|
|
|
|
|
get {
|
|
|
|
|
if (this.CurrentState != null && this.CurrentState is IVehicleState)
|
|
|
|
|
{
|
|
|
|
|
return ((IVehicleState)this.CurrentState).CurrentVehicle;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vehicle.Seat CurrentVehicleSeat {
|
|
|
|
|
get {
|
|
|
|
|
if (this.CurrentState != null && this.CurrentState is IVehicleState)
|
|
|
|
|
{
|
|
|
|
|
return ((IVehicleState)this.CurrentState).CurrentVehicleSeat;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 21:13:57 +00:00
|
|
|
|
public bool IsInVehicle { get { return this.CurrentVehicle != null; } }
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
public bool IsInVehicleSeat { get { return this.CurrentState != null && this.CurrentState.RepresentsState (typeof(VehicleSittingState)); } }
|
|
|
|
|
|
2019-07-25 01:46:30 +00:00
|
|
|
|
public bool IsDrivingVehicle { get { var seat = this.CurrentVehicleSeat; return seat != null && seat.IsDriver; } }
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2019-07-11 21:13:57 +00:00
|
|
|
|
public Vehicle.SeatAlignment CurrentVehicleSeatAlignment { get { return this.CurrentVehicleSeat.Alignment; } }
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void EnterVehicle(Vehicle vehicle, Vehicle.SeatAlignment seatAlignment, bool immediate = false)
|
|
|
|
|
{
|
2019-04-25 19:58:21 +00:00
|
|
|
|
NetStatus.ThrowIfNotOnServer();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
// find state script, and call it's method
|
|
|
|
|
this.GetStateOrLogError<VehicleEnteringState>().TryEnterVehicle( vehicle, seatAlignment, immediate );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ExitVehicle(bool immediate = false)
|
|
|
|
|
{
|
2019-04-25 19:58:21 +00:00
|
|
|
|
NetStatus.ThrowIfNotOnServer();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
this.GetStateOrLogError<VehicleExitingState> ().ExitVehicle (immediate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Vehicle.SeatAlignment> GetFreeSeats( Vehicle vehicle )
|
|
|
|
|
{
|
|
|
|
|
return vehicle.Seats.Where (s => !s.IsTaken).Select (s => s.Alignment).ToList ();
|
|
|
|
|
|
|
|
|
|
// var freeSeats = new List<Vehicle.SeatAlignment> (vehicle.Seats.Select (s => s.Alignment));
|
|
|
|
|
//
|
|
|
|
|
// var players = FindObjectsOfType<Player> ();
|
|
|
|
|
//
|
|
|
|
|
// foreach (var p in players) {
|
|
|
|
|
// if (p.IsInVehicle && p.CurrentVehicle == vehicle) {
|
|
|
|
|
// freeSeats.Remove (p.CurrentVehicleSeatAlignment);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return freeSeats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateWheelTurning()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Vehicle FindVehicleInRange ()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// find any vehicles that have a seat inside the checking radius and sort by closest seat
|
2020-05-02 23:33:37 +00:00
|
|
|
|
return Vehicle.AllVehicles
|
|
|
|
|
.Where(x => x.Seats.Count > 0)
|
2020-05-31 17:07:22 +00:00
|
|
|
|
.Where(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position) < EnterVehicleRadius)
|
|
|
|
|
.OrderBy(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position))
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 02:08:19 +00:00
|
|
|
|
public (Vehicle vehicle, Vehicle.SeatAlignment seatAlignment) GetVehicleThatPedWouldEnterOnAttempt()
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
2021-09-12 02:08:19 +00:00
|
|
|
|
var vehicle = this.FindVehicleInRange();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
if (null == vehicle)
|
2021-09-12 02:08:19 +00:00
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
var seatAlignment = vehicle.GetSeatAlignmentOfClosestSeat(this.transform.position);
|
|
|
|
|
if (seatAlignment == Vehicle.SeatAlignment.None)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
return (vehicle, seatAlignment);
|
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-09-12 02:08:19 +00:00
|
|
|
|
public Vehicle TryEnterVehicleInRange ()
|
|
|
|
|
{
|
|
|
|
|
var vehicleAndSeatAlignment = this.GetVehicleThatPedWouldEnterOnAttempt();
|
|
|
|
|
if (null == vehicleAndSeatAlignment.vehicle)
|
|
|
|
|
return null;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-09-12 02:08:19 +00:00
|
|
|
|
this.EnterVehicle(vehicleAndSeatAlignment.vehicle, vehicleAndSeatAlignment.seatAlignment);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-09-12 02:08:19 +00:00
|
|
|
|
return vehicleAndSeatAlignment.vehicle;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|