TrafficSim/app/src/main/java/de/hems/trafficsim/Vehicle.java
Loch Christian (uib05376) ca1a82a9ee Implement TrackView
2020-10-26 21:17:40 +01:00

57 lines
1.2 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 brakeProp;
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;
}
public float getCurVelocity() {
return curVelocity;
}
public float getMaxVelocity() {
return maxVelocity;
}
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 void timeElapse(float timeStep) {
position = (position + curVelocity) % 50;
}
}