TrafficSim/app/src/main/java/de/hems/trafficsim/Vehicle.java
Loch Christian (uib05376) 6abefa9f19 Implement ActionListeners for SeekBars
Adjust SeekBar styles
Implement live updating
2020-11-11 16:10:35 +01:00

63 lines
1.5 KiB
Java

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 brakeProb;
protected float trackLength;
public Vehicle(int id, int position, float maxVelocity, float brakeProb, float trackLength) {
this.id = id;
this.position = position;
this.maxVelocity = maxVelocity;
this.brakeProb = brakeProb;
this.curVelocity = 0;
this.trackLength = trackLength;
}
public float getPosition() {
return position;
}
public float getCurVelocity() {
return curVelocity;
}
public float getMaxVelocity() {
return maxVelocity;
}
public void setBrakeProb(float brakeProb) { this.brakeProb = brakeProb; }
public void setMaxVelocity(float maxVelocity) { this.maxVelocity = maxVelocity; }
public void updateVelocity(float distanceForerunner) {
Random random = new Random();
float r = random.nextFloat();
if (curVelocity < maxVelocity) {
curVelocity = curVelocity + 1;
}
if (r < brakeProb && curVelocity > 0) {
System.out.println("vehicle: "+brakeProb);
curVelocity = curVelocity - 1;
}
if (curVelocity > distanceForerunner) {
curVelocity = distanceForerunner;
}
}
public void timeElapse() {
position = (position + curVelocity) % this.trackLength;
}
}