Better movement.

- Move Vehicles from list
   in Makkah.
This commit is contained in:
HeshamTB 2020-11-14 01:30:20 +03:00
parent 392993a008
commit fe781f43b6
Signed by: Hesham
GPG Key ID: 74876157D199B09E
4 changed files with 53 additions and 33 deletions

View File

@ -31,7 +31,7 @@ public class MakkahCity {
//Set Routes for Campaigns //Set Routes for Campaigns
setRoutesForCampaigns(); setRoutesForCampaigns();
//TODO: use Qeues or Wating area for each street? //TODO: use Queues or Wating area for each street?
while(!timeManager.isEnded()) { while(!timeManager.isEnded()) {
timeManager.step(Calendar.MINUTE, 1); timeManager.step(Calendar.MINUTE, 1);
@ -49,41 +49,42 @@ public class MakkahCity {
} }
for (Street street : stdStreet) {
int lanes = street.getNumberOfLanes();
ArrayList<Vehicle> vehicles = street.getVehicles();
//Changes
for (int i = 1; i < lanes; i++) {
}
}
for (Vehicle vehicle : listOfVehicles) { for (Vehicle vehicle : listOfVehicles) {
Street st = vehicle.getCurrentStreet();
Route route = vehicle.getRoute(); Route route = vehicle.getRoute();
double currentLocation = vehicle.getCurrentLocation(); double currentLocation = vehicle.getCurrentLocation();
if (currentLocation >= st.getLength()){ if (vehicle.getCurrentStreet() == null &&
route.getStreets()[0].capcityPoint(0,1500) < 1) {
vehicle.setCurrentStreet(route.getStreets()[0]);
}
else if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation+1500,
currentLocation+1500*2) < 1 ) { //May test diff values.
if (currentLocation >= vehicle.getCurrentStreet().getLength()) {
//Move to next street //Move to next street
vehicle.setCurrentLocation(0); vehicle.setCurrentLocation(0);
int nxtIndex = route.indexOf(st) +1; int nxtIndex = route.indexOf(vehicle.getCurrentStreet()) + 1;
if (nxtIndex <= route.getStreets().length - 1) if (nxtIndex <= route.getStreets().length - 1)
vehicle.setCurrentStreet(route.getStreets()[nxtIndex]); vehicle.setCurrentStreet(route.getStreets()[nxtIndex]);
else vehicle.arrive(); else vehicle.arrive();
} }
if (!vehicle.isArrivedToDest()){ if (!vehicle.isArrivedToDest()) {
if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD - Bus.STD_BUS_SIZE - getRandom(10,15)); if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD);
else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD); else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD);
else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD); else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD);
else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD); else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD);
} }
} }
Vehicle v = listOfVehicles.get(0); }
System.out.printf("St: %s distance: %f total: %f\n", Vehicle v = listOfVehicles.get(320);
if (v.getCurrentStreet() != null) {
System.out.printf("St: %s distance: %f total: %f %s\n",
v.getCurrentStreet().getName(), v.getCurrentStreet().getName(),
v.getCurrentLocation(), v.getCurrentLocation(),
v.getTotalDistanceTraveled()); v.getTotalDistanceTraveled(),
timeManager.getCurrentTime());
}
//System.out.println(v.getTimeStartedMoving()); //System.out.println(v.getTimeStartedMoving());
//TODO: [2]add civil cars in loop iterations. (noise) //TODO: [2]add civil cars in loop iterations. (noise)
//noise based on time of day (From PDate) //noise based on time of day (From PDate)

View File

@ -10,6 +10,11 @@ public class Route {
setMashier(mashier); setMashier(mashier);
} }
public Route(Street street){
this.streets = new Street[1];
this.streets[0] = street;
}
public int indexOf(Street street){ public int indexOf(Street street){
for (int i = 0; i < streets.length; i++) { for (int i = 0; i < streets.length; i++) {
if (street == streets[i]) return i; if (street == streets[i]) return i;

View File

@ -76,4 +76,15 @@ public class Street {
vehicles.add(vehicle); vehicles.add(vehicle);
//} //}
} }
public double capcityPoint(double min, double max) {
double totalLength = (max - min) * numberOfLanes;
double totalLenthofCar=0;
for(int i=0;i<vehicles.size();i++) {
if (vehicles.get(i).getCurrentLocation() >= min &&
vehicles.get(i).getCurrentLocation() <= max)
totalLenthofCar+=vehicles.get(i).getVehicleSize();
}
return totalLenthofCar / totalLength;
}
} }

View File

@ -15,6 +15,7 @@ public abstract class Vehicle {
public Vehicle(double vehicleSize){ public Vehicle(double vehicleSize){
setVehicleSize(vehicleSize); setVehicleSize(vehicleSize);
} }
public double getVehicleSize() { public double getVehicleSize() {
return vehicleSize; return vehicleSize;
} }
@ -40,8 +41,8 @@ public abstract class Vehicle {
public void setRoute(Route route) { public void setRoute(Route route) {
this.route = route; this.route = route;
this.currentStreet = route.getStreets()[0]; //this.currentStreet = route.getStreets()[0];
this.route.getStreets()[0].addVehicle(this); //this.route.getStreets()[0].addVehicle(this);
} }
private void setVehicleSize(double vehicleSize) { private void setVehicleSize(double vehicleSize) {
@ -74,7 +75,9 @@ public abstract class Vehicle {
} }
public void setCurrentStreet(Street currentStreet) { public void setCurrentStreet(Street currentStreet) {
if (this.currentStreet != null) {
this.currentStreet.getVehicles().remove(this); this.currentStreet.getVehicles().remove(this);
}
this.currentStreet = currentStreet; this.currentStreet = currentStreet;
this.currentStreet.addVehicle(this); this.currentStreet.addVehicle(this);
} }