Timer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /************************************************************************
  2. * Timer.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /**
  22. * The {@code Timer} class represents a Timer GameObject with the functionality
  23. * of emiting a signal after some amount of time passed.
  24. *
  25. * @author Pedro Schneider
  26. *
  27. * @class
  28. */
  29. class Timer extends GameObject
  30. {
  31. /**
  32. * @constructor
  33. * Initializes a Timer GameObject with the given parameters.
  34. *
  35. * @param {String} name name for this Timer GameObject.
  36. * @param {number} duration duration in seconds of the timer.
  37. * Default is 1 second.
  38. * @param {boolean} autostart shuold the timer start automaticaly
  39. * when it enters the tree? Default is false.
  40. * @param {boolean} oneShot should the timer run only once?
  41. * Default is false.
  42. */
  43. constructor(name, duration = 1, autostart = false, oneShot = false)
  44. {
  45. super(name);
  46. this.duration = duration;
  47. this.timeLeft = this.duration;
  48. this.paused = !autostart;
  49. this.autostart = autostart;
  50. this.oneShot = oneShot;
  51. }
  52. /**
  53. * Starts counting the Timer if it is paused, and does nothing if
  54. * the Timer is already running.
  55. *
  56. * @param {number} timeSec duration in seconds to override Timer's duration.
  57. * Defaults to current Timer's duration.
  58. */
  59. start(timeSec = this.duration)
  60. {
  61. if (!this.paused) return;
  62. this.duration = timeSec;
  63. this.paused = false;
  64. this.timeLeft = this.duration;
  65. }
  66. /**
  67. * Pauses the Timer. Does nothing if already paused.
  68. */
  69. stop()
  70. {
  71. this.paused = true;
  72. }
  73. /**
  74. * Resumes the Timer. Does nothing if already running.
  75. */
  76. resume()
  77. {
  78. this.paused = false;
  79. }
  80. /**
  81. * Returns the paused state of the Timer.
  82. *
  83. * @returns {boolean} true if the timer is paused, false if not.
  84. */
  85. isStopped()
  86. {
  87. return this.paused;
  88. }
  89. /**
  90. * This function is called when the timer is done and serves
  91. * to change the data of the timer accordingly and emit the
  92. * timeout signal.
  93. */
  94. onFinish()
  95. {
  96. if (this.oneShot) this.paused = true
  97. this.timeLeft = this.duration;
  98. this._onFinish();
  99. this.emitSignal("timeout");
  100. }
  101. /**
  102. * @override
  103. * Updates the Timer and calls the onFinish() function if the timer ended.
  104. * Also recursively calls the update() function for all of this GameObject's
  105. * children.
  106. *
  107. * @param {number} delta time in seconds ellapsed since the last frame.
  108. */
  109. update(delta)
  110. {
  111. if (!this.paused)
  112. {
  113. this.timeLeft -= delta;
  114. if (this.timeLeft <= 0) this.onFinish();
  115. }
  116. this._update(delta);
  117. for (let i = 0; i < this.children.length; i++)
  118. this.children[i].update(delta);
  119. }
  120. /**
  121. * @override
  122. * Adds default signals for the Timer GameObject and serves as a caller
  123. * to the _initSignals() callback.
  124. *
  125. * @signal timeout emited once every time this timer is done.
  126. */
  127. initSignals()
  128. {
  129. this.addSignal("timeout");
  130. this._initSignals();
  131. }
  132. /**
  133. * @callback
  134. * ! This function should be overriden, it provides no default functionality.
  135. * Called once every time the Timer is done and can be used in
  136. * objects that inherit from Timer to add functinoality this event.
  137. */
  138. _onFinish()
  139. {
  140. }
  141. }