2020-09-16 14:20:51 +02:00
|
|
|
package de.hems.trafficsim;
|
|
|
|
|
2020-10-12 18:26:52 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2020-09-16 14:20:51 +02:00
|
|
|
public class Vehicle {
|
2020-10-12 18:26:52 +02:00
|
|
|
protected int id;
|
2020-09-16 14:20:51 +02:00
|
|
|
protected float position;
|
|
|
|
protected float curVelocity;
|
|
|
|
protected float maxVelocity;
|
2020-10-12 18:26:52 +02:00
|
|
|
protected float brakeProp;
|
2020-10-29 11:53:35 +01:00
|
|
|
protected float trackLength;
|
2020-09-16 14:20:51 +02:00
|
|
|
|
|
|
|
|
2020-10-29 11:53:35 +01:00
|
|
|
public Vehicle(int id, int position, float maxVelocity, float brakeProp, float trackLength) {
|
2020-10-12 18:26:52 +02:00
|
|
|
this.id = id;
|
|
|
|
this.position = position;
|
|
|
|
this.maxVelocity = maxVelocity;
|
|
|
|
this.brakeProp = brakeProp;
|
|
|
|
this.curVelocity = 0;
|
2020-10-29 11:53:35 +01:00
|
|
|
this.trackLength = trackLength;
|
2020-10-12 18:26:52 +02:00
|
|
|
|
|
|
|
}
|
2020-09-16 14:20:51 +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;
|
|
|
|
}
|
|
|
|
|
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 < brakeProp && curVelocity > 0) {
|
|
|
|
curVelocity = getCurVelocity() - 1;
|
|
|
|
}
|
|
|
|
if (curVelocity > distanceForerunner) {
|
|
|
|
curVelocity = distanceForerunner;
|
|
|
|
}
|
2020-09-16 14:20:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-02 17:19:07 +01:00
|
|
|
public void timeElapse() {
|
2020-10-29 11:53:35 +01:00
|
|
|
position = (position + curVelocity) % this.trackLength;
|
2020-09-16 14:20:51 +02:00
|
|
|
}
|
2020-10-12 18:26:52 +02:00
|
|
|
|
2020-09-16 14:20:51 +02:00
|
|
|
}
|
2020-10-12 18:26:52 +02:00
|
|
|
|