Cars move on top of each other

This commit is contained in:
HeshamTB 2020-11-13 22:35:52 +03:00
parent fac5ed0089
commit 392993a008
Signed by: Hesham
GPG Key ID: 74876157D199B09E
8 changed files with 131 additions and 68 deletions

View File

@ -4,7 +4,7 @@ public class Bus extends CivilVehicle {
private String UID;
private static int numeberOfBuses;
private final int TIME_TO_FIX = 20; //in minutes
public final int MAX_FORWARD = 900; //Meter/Min
public static final int MAX_FORWARD = 900; //Meter/Min
public static final double STD_BUS_SIZE = 10;

View File

@ -13,10 +13,11 @@ public class Campaign {
private ArrayList<Vehicle> vehicles = new ArrayList<>();
//Will be of type PDate after extention
private Date timeToLeaveToDest;
private Date timeToLeaveToHousing;
private boolean startedMoving;
private static int numeberOfCampains;
public Campaign(District hotelDistrict, int numberofBusses) {
@ -89,6 +90,10 @@ public class Campaign {
this.vehicles = vehicles;
}
public void setStartedMoving(){
this.startedMoving = true;
}
private void generateBusses(int number){
for (int i = 1; i <= number; i++) {
vehicles.add(new Bus());

View File

@ -10,15 +10,15 @@ public class MakkahCity {
private static final PDate timeManager = new PDate(
new GregorianCalendar(2020, Calendar.JANUARY, 1, 4, 0, 0),
new GregorianCalendar(2020, Calendar.JANUARY, 1, 20, 0, 0)
new GregorianCalendar(2020, Calendar.JANUARY, 1, 17, 0, 0)
);
public static void main(String[] args) {
//Gen Camp
generateCamps(District.ALAZIZIYA, getRandom(70, 100));
generateCamps(District.ALMANSOOR, getRandom(110, 160));
generateCamps(District.ALHIJRA, getRandom(80, 110));
generateCamps(District.ALAZIZIYA, (int)getRandom(70, 100));
generateCamps(District.ALMANSOOR, (int)getRandom(110, 160));
generateCamps(District.ALHIJRA, (int)getRandom(80, 110));
fillBusesToList();
@ -31,19 +31,16 @@ public class MakkahCity {
//Set Routes for Campaigns
setRoutesForCampaigns();
//TODO: use Qeues or Wating area for each street?
while(!timeManager.isEnded()) {
timeManager.step(Calendar.MINUTE, 1);
//System.out.println(timeManager.getCurrentTime());
Vehicle v = listOfVehicles.get(0);
Vehicle v2 = listOfVehicles.get(1);
//Start of Every hour
if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == 0){
}
//Start of Every half-hour
if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == 30){
if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) % 30 == 0){
}
@ -51,23 +48,48 @@ public class MakkahCity {
&& timeManager.getCurrentCalendar().get(Calendar.SECOND) == getRandom(0,59)){
}
System.out.println("v1 "+ v.getCurrentLocation()+ " " + v.getCurrentStreet().getName().name());
System.out.println("v2 "+ v2.getCurrentLocation()+ " " + v.getCurrentStreet().getName().name());
//TODO: [2]add civil cars in loop iterations. (noise)
//noise based on time of day (From PDate)
//TODO: [3]Move busses and vehicles.
//TODO: [4]Update streets.
for (Street street : stdStreet) {
int lanes = street.getNumberOfLanes();
ArrayList<Vehicle> vehicles = street.getVehicles();
//Changes
//TODO: [5]Streets move forward.
for (int i = 1; i < lanes; i++) {
for (Campaign campaign : listOfCampaigns){
for (Vehicle vehicle : campaign.getVehicles()){
vehicle.moveForward(((Bus)vehicle).MAX_FORWARD);
}
}
//TODO: [6]update vehicles on street.
for (Vehicle vehicle : listOfVehicles) {
Street st = vehicle.getCurrentStreet();
Route route = vehicle.getRoute();
double currentLocation = vehicle.getCurrentLocation();
if (currentLocation >= st.getLength()){
//Move to next street
vehicle.setCurrentLocation(0);
int nxtIndex = route.indexOf(st) +1;
if (nxtIndex <= route.getStreets().length - 1)
vehicle.setCurrentStreet(route.getStreets()[nxtIndex]);
else vehicle.arrive();
}
if (!vehicle.isArrivedToDest()){
if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD - Bus.STD_BUS_SIZE - getRandom(10,15));
else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD);
else if (vehicle instanceof SUV) vehicle.move(SUV.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",
v.getCurrentStreet().getName(),
v.getCurrentLocation(),
v.getTotalDistanceTraveled());
//System.out.println(v.getTimeStartedMoving());
//TODO: [2]add civil cars in loop iterations. (noise)
//noise based on time of day (From PDate)
//TODO: [5]Streets move forward.
}
}
@ -90,13 +112,13 @@ public class MakkahCity {
}
}
private static int getRandom(int min, int max) {
return (int)(Math.random() * (max - min) + min);
private static double getRandom(int min, int max) {
return (Math.random() * (max - min) + min);
}
private static void generateCamps(District area, int count) {
for (int i = 0; i < count; i++){
Campaign camp = new Campaign(area, getRandom(10, 15));
Campaign camp = new Campaign(area, (int)getRandom(10, 15));
listOfCampaigns.add(camp);
}
}

View File

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

View File

@ -2,7 +2,7 @@
public class SUV extends CivilVehicle {
private final int TIME_TO_FIX = 15; //in minutes
public final int MAX_FORWARD = 1300;
public static final int MAX_FORWARD = 1300;
public SUV(double vehicleSize){
super(vehicleSize);

View File

@ -2,7 +2,7 @@
public class Sedan extends CivilVehicle {
private final int TIME_TO_FIX = 15; //in minutes
public final int MAX_FORWARD = 1500; // Meter/Min
public static final int MAX_FORWARD = 1500; // Meter/Min
public Sedan(double vehicleSize){
super(vehicleSize);

View File

@ -71,9 +71,9 @@ public class Street {
}
public void addVehicle(Vehicle vehicle) {
if(capcity() > vehicle.getVehicleSize() + 0.5) {
//if(capcity() > vehicle.getVehicleSize() + 0.5) {
//adds incoming vehicle in last.
vehicles.add(vehicle);
}
//}
}
}

View File

@ -6,10 +6,11 @@ public abstract class Vehicle {
private Route route;
private Street currentStreet;
private double currentLocation;
private int currentStreetIndex;
private double totalDistanceTraveled;
private boolean arrivedToDest;
private boolean moving;
private Date timeStartedMoving;
private Date timeOfArrival;
public Vehicle(double vehicleSize){
setVehicleSize(vehicleSize);
@ -22,11 +23,25 @@ public abstract class Vehicle {
return route;
}
public void arrive() {
setArrivedToDest(true);
setMoving(false);
setTimeOfArrival(MakkahCity.getTimeManager().getCurrentTime());
}
public void move(double distance) {
if (!isMoving()) {
setMoving(true);
setTimeStartedMoving(MakkahCity.getTimeManager().getCurrentTime());
}
setCurrentLocation(getCurrentLocation() + distance);
setTotalDistanceTraveled(getTotalDistanceTraveled() + distance);
}
public void setRoute(Route route) {
this.route = route;
this.currentStreetIndex = 0;
this.currentStreet = route.getStreets()[0];
this.route.getStreets()[0].addVehicle(this);//TODO: [9]street might not add if capacity is low
this.route.getStreets()[0].addVehicle(this);
}
private void setVehicleSize(double vehicleSize) {
@ -42,42 +57,6 @@ public abstract class Vehicle {
return currentLocation;
}
public void moveForward(double distance) {
if (!moving && !arrivedToDest) moving = true;
if (!arrivedToDest){
if (isAtEndOfCurrentStreet()) {
moveToNextStreet();
//this.currentLocation += distance;
}
else {
double dToNext = getDistanceToNextVehicle();
double trafficFactor = getCurrentStreet().getPercentRemainingCapacity() / 100.0;
if (dToNext == -1 ) {
this.currentLocation += distance * trafficFactor;
} else if (dToNext < distance){//TODO after I wake up
this.currentLocation += distance;
}
}
}
}
private boolean isAtEndOfCurrentStreet() {
//At last meter of current street
return (this.currentStreet.getLength() - this.currentLocation) < 1;
}
private void moveToNextStreet() {
this.currentStreetIndex++;
Street nextStreet;
try { nextStreet = this.getRoute().getStreets()[currentStreetIndex]; }
catch (IndexOutOfBoundsException e) { this.arrivedToDest = true; this.moving = false; return;}
if (nextStreet.canTakeVehicles(this)){
this.currentStreet = nextStreet;
this.currentLocation = 0;
}
}
/**
* @return Distance in meters to next car on same street. -1 if Can not calculate.
*/
@ -93,4 +72,54 @@ public abstract class Vehicle {
}
return -1;//open or unlimited
}
public void setCurrentStreet(Street currentStreet) {
this.currentStreet.getVehicles().remove(this);
this.currentStreet = currentStreet;
this.currentStreet.addVehicle(this);
}
public void setCurrentLocation(double currentLocation) {
this.currentLocation = currentLocation;
}
public double getTotalDistanceTraveled() {
return totalDistanceTraveled;
}
public void setTotalDistanceTraveled(double totalDistanceTraveled) {
this.totalDistanceTraveled = totalDistanceTraveled;
}
public boolean isArrivedToDest() {
return arrivedToDest;
}
public void setArrivedToDest(boolean arrivedToDest) {
this.arrivedToDest = arrivedToDest;
}
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public Date getTimeStartedMoving() {
return timeStartedMoving;
}
public void setTimeStartedMoving(Date timeStartedMoving) {
this.timeStartedMoving = timeStartedMoving;
}
public Date getTimeOfArrival() {
return timeOfArrival;
}
public void setTimeOfArrival(Date timeOfArrival) {
this.timeOfArrival = timeOfArrival;
}
}