From 4d646c853df60a70d54adf05b7f96be18ca97dec Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 3 May 2020 02:28:25 +0200 Subject: [PATCH] small optimization for finding closest vehicle/seat --- Assets/Scripts/Behaviours/Ped/Ped_Vehicle.cs | 2 +- .../Behaviours/Ped/PlayerController.cs | 2 +- Assets/Scripts/Behaviours/Vehicles/Vehicle.cs | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Behaviours/Ped/Ped_Vehicle.cs b/Assets/Scripts/Behaviours/Ped/Ped_Vehicle.cs index 260e82e3..2c7e6154 100644 --- a/Assets/Scripts/Behaviours/Ped/Ped_Vehicle.cs +++ b/Assets/Scripts/Behaviours/Ped/Ped_Vehicle.cs @@ -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); diff --git a/Assets/Scripts/Behaviours/Ped/PlayerController.cs b/Assets/Scripts/Behaviours/Ped/PlayerController.cs index 6d3ed547..905dc822 100644 --- a/Assets/Scripts/Behaviours/Ped/PlayerController.cs +++ b/Assets/Scripts/Behaviours/Ped/PlayerController.cs @@ -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) { diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs index 0481ac25..624e680c 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs @@ -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)