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.

62 lines
1.5KB

  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 brakeProb;
  9. protected float trackLength;
  10. public Vehicle(int id, int position, float maxVelocity, float brakeProb, float trackLength) {
  11. this.id = id;
  12. this.position = position;
  13. this.maxVelocity = maxVelocity;
  14. this.brakeProb = brakeProb;
  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 setBrakeProb(float brakeProb) { this.brakeProb = brakeProb; }
  28. public void setMaxVelocity(float maxVelocity) { this.maxVelocity = maxVelocity; }
  29. public void updateVelocity(float distanceForerunner) {
  30. Random random = new Random();
  31. float r = random.nextFloat();
  32. if (curVelocity < maxVelocity) {
  33. curVelocity = curVelocity + 1;
  34. }
  35. if (r < brakeProb && curVelocity > 0) {
  36. curVelocity = curVelocity - 1;
  37. }
  38. if (curVelocity > distanceForerunner) {
  39. curVelocity = distanceForerunner;
  40. }
  41. }
  42. public void timeElapse() {
  43. position = (position + curVelocity) % this.trackLength;
  44. }
  45. }