Timer.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. class Timer extends GameObject
  2. {
  3. constructor(name, duration = 1, autostart = false, oneShot = false)
  4. {
  5. super(name);
  6. this.duration = duration;
  7. this.timeLeft = this.duration;
  8. this.paused = !autostart;
  9. this.autostart = autostart;
  10. this.oneShot = oneShot;
  11. }
  12. start(timeSec = this.duration)
  13. {
  14. if (!this.paused) return;
  15. this.duration = timeSec;
  16. this.paused = false;
  17. this.timeLeft = this.duration;
  18. }
  19. stop()
  20. {
  21. this.paused = true;
  22. }
  23. resume()
  24. {
  25. this.paused = false;
  26. }
  27. isStopped()
  28. {
  29. return this.paused;
  30. }
  31. update(delta)
  32. {
  33. if (!this.paused)
  34. {
  35. this.timeLeft -= delta;
  36. if (this.timeLeft <= 0) this.onFinish();
  37. }
  38. this._update(delta);
  39. for (let i = 0; i < this.children.length; i++)
  40. this.children[i].update(delta);
  41. }
  42. onFinish()
  43. {
  44. if (this.oneShot) this.paused = true
  45. this.timeLeft = this.duration;
  46. this._onFinish();
  47. }
  48. _onFinish()
  49. {
  50. console.log("doneskis");
  51. }
  52. }