diff --git a/app/src/main/java/de/hems/trafficsim/MainActivity.java b/app/src/main/java/de/hems/trafficsim/MainActivity.java index 7a3cb63..caf7537 100644 --- a/app/src/main/java/de/hems/trafficsim/MainActivity.java +++ b/app/src/main/java/de/hems/trafficsim/MainActivity.java @@ -17,7 +17,7 @@ public class MainActivity extends AppCompatActivity implements Observer { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - this.track = new Track(); + this.track = new Track(20); this.track.addObserver(this); } @@ -26,14 +26,20 @@ public class MainActivity extends AppCompatActivity implements Observer { public void update(Observable observable, Object o) { String s = ""; for (Vehicle v: this.track.getVehicles()){ - s+="Pos = " + v.getPosition() +"\n"; + s+= "Vehicle " + v.id + " | " + + "Pos = " + v.getPosition() + " | " + + "Vel = " + v.getCurVelocity()+ " | " + + "\n"; + ; } TextView view = (TextView) findViewById(R.id.debugTextView); view.setText(s); view.invalidate(); } - public void onStepButtonClick (View view){ - this.track.timeElapse(50); + public void onStepButtonClick (View view) { + for (int j = 0; j < 3000; j++) { + this.track.timeElapse(50); + } } } \ No newline at end of file diff --git a/app/src/main/java/de/hems/trafficsim/Track.java b/app/src/main/java/de/hems/trafficsim/Track.java index 7671c8f..0255ebf 100644 --- a/app/src/main/java/de/hems/trafficsim/Track.java +++ b/app/src/main/java/de/hems/trafficsim/Track.java @@ -6,26 +6,46 @@ import java.util.Observable; public class Track extends Observable { protected List vehicles; + + public List getVehicles() { + return vehicles; } - - - public Track() { - this.vehicles = createVehiclesList(); + public Track(int numberVehicles) { + this.vehicles = createVehiclesList(numberVehicles); } - protected List createVehiclesList(){ + protected List createVehiclesList(int numberVehicles){ List result = new ArrayList<>(); - for(int i=0;i<10;i++){ - Vehicle vehicle = new Vehicle(i, 5, 0.2f); + for(int i=0;i= vehicles.size()) { + forerunnerIndex -= vehicles.size(); + } + Vehicle forerunner = vehicles.get(forerunnerIndex); + float distanceForerunner = forerunner.getPosition() - v.getPosition() - 1; + if(distanceForerunner < 0.0){ + distanceForerunner += 50; + } + v.updateVelocity(distanceForerunner); + } + + for(Vehicle v: vehicles){ + v.timeElapse(50); + } + this.setChanged(); this.notifyObservers(this); this.clearChanged(); diff --git a/app/src/main/java/de/hems/trafficsim/Vehicle.java b/app/src/main/java/de/hems/trafficsim/Vehicle.java index b316f42..5e2792d 100644 --- a/app/src/main/java/de/hems/trafficsim/Vehicle.java +++ b/app/src/main/java/de/hems/trafficsim/Vehicle.java @@ -1,15 +1,23 @@ package de.hems.trafficsim; +import java.util.Random; + public class Vehicle { + protected int id; protected float position; protected float curVelocity; protected float maxVelocity; + protected float brakeProp; - public void setForerunner(Vehicle forerunner) { - this.forerunner = forerunner; - } - protected Vehicle forerunner; + public Vehicle(int id, int position, float maxVelocity, float brakeProp) { + this.id = id; + this.position = position; + this.maxVelocity = maxVelocity; + this.brakeProp = brakeProp; + this.curVelocity = 0; + + } public float getPosition() { return position; @@ -19,17 +27,26 @@ public class Vehicle { return curVelocity; } - protected float brakeProp; + public void updateVelocity(float distanceForerunner) { + Random random = new Random(); + + float r = random.nextFloat(); + + if (curVelocity < maxVelocity) { + curVelocity = curVelocity + 1; + } + if (r < brakeProp && curVelocity > 0) { + curVelocity = getCurVelocity() - 1; + } + if (curVelocity > distanceForerunner) { + curVelocity = distanceForerunner; + } - public Vehicle(float position, float maxVelocity, float brakeProp) { - this.position = position; - this.maxVelocity = maxVelocity; - this.brakeProp = brakeProp; - this.curVelocity = 0; - this.forerunner = null; } public void timeElapse(float timeStep) { - + position = (position + curVelocity) % 50; } + } +