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.

59 lines
1.3KB

  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. protected float trackLength;
  10. public Vehicle(int id, int position, float maxVelocity, float brakeProp, float trackLength) {
  11. this.id = id;
  12. this.position = position;
  13. this.maxVelocity = maxVelocity;
  14. this.brakeProp = brakeProp;
  15. this.curVelocity = 0;
  16. this.trackLength = trackLength;
  17. }
  18. public float getPosition() {
  19. return position;
  20. }
  21. public float getCurVelocity() {
  22. return curVelocity;
  23. }
  24. public float getMaxVelocity() {
  25. return maxVelocity;
  26. }
  27. public void updateVelocity(float distanceForerunner) {
  28. Random random = new Random();
  29. float r = random.nextFloat();
  30. if (curVelocity < maxVelocity) {
  31. curVelocity = curVelocity + 1;
  32. }
  33. if (r < brakeProp && curVelocity > 0) {
  34. curVelocity = getCurVelocity() - 1;
  35. }
  36. if (curVelocity > distanceForerunner) {
  37. curVelocity = distanceForerunner;
  38. }
  39. }
  40. public void timeElapse(float timeStep) {
  41. position = (position + curVelocity) % this.trackLength;
  42. }
  43. }