/Code/src/com/game/Regulator.java

https://bitbucket.org/DeveloperUX/behaviortree · Java · 48 lines · 21 code · 5 blank · 22 comment · 1 complexity · d15c351cdc2dc1d5f96d6c005fa1dcf8 MD5 · raw file

  1. package com.game;
  2. /**
  3. * Time step class, for regulating update speed of updates.
  4. * @author Ying
  5. *
  6. */
  7. public class Regulator
  8. {
  9. /**
  10. * Last time the regulator was ready
  11. */
  12. private long lastUpdateTime;
  13. /**
  14. * Speed at which the regulator updates
  15. */
  16. private long updateSpeed;
  17. /**
  18. * Millisecond constant, to avoid magic numbers
  19. */
  20. private static final int MILSECS = 1000;
  21. /**
  22. * Creates a new instance of the Regulator class
  23. * @param framesPerSecond is the update speed for this regulator.
  24. */
  25. public Regulator(float framesPerSecond)
  26. {
  27. this.updateSpeed = (long) (MILSECS / framesPerSecond);
  28. this.lastUpdateTime = System.currentTimeMillis();
  29. }
  30. /**
  31. * Returns true if the regulator is ready for a update, false otherwise
  32. * @return
  33. */
  34. public boolean IsReady()
  35. {
  36. if(System.currentTimeMillis() - this.lastUpdateTime > updateSpeed)
  37. {
  38. this.lastUpdateTime = System.currentTimeMillis();
  39. return true;
  40. }
  41. return false;
  42. }
  43. }