small optimization for finding closest vehicle/seat

This commit is contained in:
in0finite 2020-05-03 02:28:25 +02:00
parent 0e130999f4
commit 4d646c853d
3 changed files with 15 additions and 8 deletions

View file

@ -101,7 +101,7 @@ namespace SanAndreasUnity.Behaviours
if (null == vehicle)
return null;
var seat = vehicle.FindClosestSeat(this.transform.position);
var seat = vehicle.GetSeatAlignmentOfClosestSeat(this.transform.position);
this.EnterVehicle(vehicle, seat);

View file

@ -244,7 +244,7 @@ namespace SanAndreasUnity.Behaviours
// draw closest seat
var closestSeat = vehicle.FindClosestSeat(transform.position);
var closestSeat = vehicle.GetSeatAlignmentOfClosestSeat(transform.position);
if (closestSeat != Vehicle.SeatAlignment.None)
{

View file

@ -334,18 +334,25 @@ namespace SanAndreasUnity.Behaviours.Vehicles
m_rearRightLightOk = m_rearRightLight != null;
}
public SeatAlignment FindClosestSeat(Vector3 position)
public SeatAlignment GetSeatAlignmentOfClosestSeat(Vector3 position)
{
var seat = _seats.Select((s, i) => new { s, i })
.OrderBy(x => Vector3.Distance(position, x.s.Parent.position))
.FirstOrDefault();
var seat = FindClosestSeat(position);
return seat != null ? seat.Alignment : SeatAlignment.None;
}
return (seat == null ? SeatAlignment.None : seat.s.Alignment);
public Seat FindClosestSeat(Vector3 position)
{
if (this.Seats.Count < 1)
return null;
return this.Seats.Aggregate((a, b) =>
Vector3.Distance(position, a.Parent.position) < Vector3.Distance(position, b.Parent.position) ? a : b);
}
public Transform FindClosestSeatTransform(Vector3 position)
{
return GetSeatTransform(FindClosestSeat(position));
var seat = FindClosestSeat(position);
return seat != null ? seat.Parent : null;
}
public Seat GetSeat(SeatAlignment alignment)