Vehicle: calc distance to next

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-11-10 20:16:36 +03:00
parent 92661695b1
commit b491eaf4ab

View File

@ -39,4 +39,16 @@ public abstract class Vehicle {
//TODO: Check if at end of street move to next street in Route.
this.currentLocation += distance;
}
/**
* @return Distance in meters to next car on same street. -1 if Can not calculate.
*/
public double getDistanceToNextVehicle() {
if (getCurrentStreet() != null) {
int indexOfNxt = getCurrentStreet().getVehicles().indexOf(this) + 1; //next
Vehicle nextVehicle = getCurrentStreet().getVehicles().get(indexOfNxt);
return nextVehicle.getCurrentLocation() - this.getCurrentLocation();
}
return -1;//open or unlimited
}
}