You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1KB

  1. package de.hems.trafficsim;
  2. import java.util.Random;
  3. public class Vehicle {
  4. protected int id;
  5. protected float position;
  6. protected float curVelocity;
  7. protected float maxVelocity;
  8. protected float brakeProp;
  9. public Vehicle(int id, int position, float maxVelocity, float brakeProp) {
  10. this.id = id;
  11. this.position = position;
  12. this.maxVelocity = maxVelocity;
  13. this.brakeProp = brakeProp;
  14. this.curVelocity = 0;
  15. }
  16. public float getPosition() {
  17. return position;
  18. }
  19. public float getCurVelocity() {
  20. return curVelocity;
  21. }
  22. public void updateVelocity(float distanceForerunner) {
  23. Random random = new Random();
  24. float r = random.nextFloat();
  25. if (curVelocity < maxVelocity) {
  26. curVelocity = curVelocity + 1;
  27. }
  28. if (r < brakeProp && curVelocity > 0) {
  29. curVelocity = getCurVelocity() - 1;
  30. }
  31. if (curVelocity > distanceForerunner) {
  32. curVelocity = distanceForerunner;
  33. }
  34. }
  35. public void timeElapse(float timeStep) {
  36. position = (position + curVelocity) % 50;
  37. }
  38. }