Timer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  26. * ! of another GameObject on the tree or as a root).
  27. *
  28. * @author Pedro Schneider
  29. *
  30. * @class
  31. */
  32. class Timer extends GameObject
  33. {
  34. /**
  35. * Initializes a Timer GameObject with the given parameters.
  36. *
  37. * @param {String} name name for this Timer GameObject.
  38. * @param {number} duration duration in seconds of the timer.
  39. * Default is 1 second.
  40. * @param {boolean} autostart shuold the timer start automaticaly
  41. * when it enters the tree? Default is false.
  42. * @param {boolean} oneShot should the timer run only once?
  43. * Default is false.
  44. *
  45. * @constructor
  46. */
  47. constructor(name, duration = 1, autostart = false, oneShot = false)
  48. {
  49. super(name);
  50. this.duration = duration;
  51. this.timeLeft = this.duration;
  52. this.paused = !autostart;
  53. this.autostart = autostart;
  54. this.oneShot = oneShot;
  55. }
  56. /**
  57. * Starts counting the Timer if it is paused, and does nothing if
  58. * the Timer is already running.
  59. *
  60. * @param {number} timeSec duration in seconds to override Timer's duration.
  61. * Defaults to current Timer's duration.
  62. */
  63. start(timeSec = this.duration)
  64. {
  65. if (!this.paused) return;
  66. this.duration = timeSec;
  67. this.paused = false;
  68. this.timeLeft = this.duration;
  69. }
  70. /**
  71. * Pauses the Timer. Does nothing if already paused.
  72. */
  73. stop()
  74. {
  75. this.paused = true;
  76. }
  77. /**
  78. * Resumes the Timer. Does nothing if already running.
  79. */
  80. resume()
  81. {
  82. this.paused = false;
  83. }
  84. /**
  85. * Returns the paused state of the Timer.
  86. *
  87. * @returns {boolean} true if the timer is paused, false if not.
  88. */
  89. isStopped()
  90. {
  91. return this.paused;
  92. }
  93. /**
  94. * This function is called when the timer is done and serves
  95. * to change the data of the timer accordingly and emit the
  96. * timeout signal.
  97. */
  98. onFinish()
  99. {
  100. if (this.oneShot) this.paused = true
  101. this.timeLeft = this.duration;
  102. this._onFinish();
  103. this.emitSignal("timeout");
  104. }
  105. /**
  106. * Updates the Timer and calls the onFinish() function if the timer ended.
  107. * Also recursively calls the update() function for all of this GameObject's
  108. * children.
  109. *
  110. * @param {number} delta time in seconds ellapsed since the last frame.
  111. *
  112. * @override
  113. */
  114. update(delta)
  115. {
  116. if (!this.paused)
  117. {
  118. this.timeLeft -= delta;
  119. if (this.timeLeft <= 0) this.onFinish();
  120. }
  121. this.updateChildren(delta);
  122. }
  123. /**
  124. * Adds default signals for the Timer GameObject and serves as a caller
  125. * to the _initSignals() callback.
  126. *
  127. * @signal timeout emited once every time this timer is done.
  128. *
  129. * @override
  130. */
  131. initSignals()
  132. {
  133. this.addSignal("timeout");
  134. this._initSignals();
  135. }
  136. /**
  137. * ! This function should be overriden, it provides no default functionality.
  138. * Called once every time the Timer is done and can be used in
  139. * objects that inherit from Timer to add functinoality this event.
  140. *
  141. * @callback
  142. */
  143. _onFinish()
  144. {
  145. }
  146. }