TrafficSim/app/src/main/java/de/hems/trafficsim/Vehicle.java

62 lines
1.5 KiB
Java
Raw Normal View History

package de.hems.trafficsim;
2020-10-12 18:26:52 +02:00
import java.util.Random;
public class Vehicle {
2020-10-12 18:26:52 +02:00
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) {
2020-10-12 18:26:52 +02:00
this.id = id;
this.position = position;
this.maxVelocity = maxVelocity;
this.brakeProb = brakeProb;
2020-10-12 18:26:52 +02:00
this.curVelocity = 0;
this.trackLength = trackLength;
2020-10-12 18:26:52 +02:00
}
public float getPosition() {
return position;
}
public float getCurVelocity() {
return curVelocity;
}
2020-10-26 21:17:40 +01:00
public float getMaxVelocity() {
return maxVelocity;
}
public void setBrakeProb(float brakeProb) { this.brakeProb = brakeProb; }
public void setMaxVelocity(float maxVelocity) { this.maxVelocity = maxVelocity; }
2020-10-12 18:26:52 +02:00
public void updateVelocity(float distanceForerunner) {
Random random = new Random();
float r = random.nextFloat();
if (curVelocity < maxVelocity) {
curVelocity = curVelocity + 1;
}
if (r < brakeProb && curVelocity > 0) {
curVelocity = curVelocity - 1;
2020-10-12 18:26:52 +02:00
}
if (curVelocity > distanceForerunner) {
curVelocity = distanceForerunner;
}
}
public void timeElapse() {
position = (position + curVelocity) % this.trackLength;
}
2020-10-12 18:26:52 +02:00
}
2020-10-12 18:26:52 +02:00