Object2D.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /************************************************************************
  2. * Object2D.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 Object2D} class represents a GameObject that has a transform,
  23. * comprised of a position, a rotation in degrees and a scale. All parameters
  24. * of this transform are relative to the parent of this GameObject, if it has one.
  25. *
  26. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  27. * ! of another GameObject on the tree or as a root).
  28. *
  29. * @author Pedro Schneider
  30. *
  31. * @class
  32. */
  33. class Object2D extends GameObject
  34. {
  35. /**
  36. * Initializes an empty Object2D GameObject.
  37. *
  38. * @param {String} name name of the Object2D GameObject.
  39. *
  40. * @constructor
  41. */
  42. constructor(name)
  43. {
  44. super(name);
  45. this.position = Vector2.ZERO(); // This Object2D's position on the secondary buffer.
  46. this.rotationDegrees = 0; // This Object2D's rotation degrees on the secondary buffer.
  47. this.scale = Vector2.ONE(); // This Object2D's scale on the secondary buffer.
  48. this.visible = true; // Is this Object2D visible at the moment?
  49. this.globalPosition = Vector2.ZERO(); // This Object2D's global position on the secondary buffer.
  50. this.globalRotationDegrees = 0; // This Object2D's global rotation degrees on the secondary buffer.
  51. this.globalScale = Vector2.ONE(); // This Object2D's global scale on the secondary buffer.
  52. }
  53. /**
  54. * Sets this Object2D's position.
  55. *
  56. * @param {number} x new position on the x axis for this Object2D.
  57. * @param {number} y new positoin on the y axis for this Object2D.
  58. */
  59. setPosition(x, y)
  60. {
  61. this.position.x = x;
  62. this.position.y = y;
  63. }
  64. /**
  65. * Sets the visibility flag of this Object2D and all of its children
  66. * that have a visibility flag to true.
  67. */
  68. show()
  69. {
  70. this.visible = true;
  71. for (let i = 0; i < this.children.length; i++)
  72. {
  73. if (!this.children[i].show) continue;
  74. this.children[i].show();
  75. }
  76. }
  77. /**
  78. * Sets the visibility flag of this Object2D and all of its children
  79. * that have a visibility flag to false.
  80. */
  81. hide()
  82. {
  83. this.visible = false;
  84. for (let i = 0; i < this.children.length; i++)
  85. {
  86. if (!this.children[i].hide) continue;
  87. this.children[i].hide();
  88. }
  89. }
  90. /**
  91. * Sets the visibility flag of this Object2D and all of its children that have a visibility
  92. * flag to the provided value.
  93. *
  94. * @param {boolean} val value to set the flag to.
  95. */
  96. setVisibility(val)
  97. {
  98. this.visible = val;
  99. for (let i = 0; i < this.children.length; i++)
  100. {
  101. if (!this.children[i].setVisibility) continue;
  102. this.children[i].setVisibility(val);
  103. }
  104. }
  105. /**
  106. * Returns this Object2D's visibility flag.
  107. *
  108. * @returns {boolean} true if this Object2D is visible, false if not.
  109. */
  110. getVisibility()
  111. {
  112. return this.visible;
  113. }
  114. /**
  115. * Updates this Object2D's global transform and recursively calls the _update(delta)
  116. * callback for this GameObject and all of it's children.
  117. *
  118. * @param {number} delta number of ellapsed seconds since the last frame.
  119. *
  120. * @override
  121. */
  122. update(delta)
  123. {
  124. // Update global transform
  125. if (!this.parented || !(this.parent instanceof Object2D))
  126. {
  127. this.globalPosition = this.position;
  128. this.globalRotationDegrees = this.rotationDegrees;
  129. this.globalScale = this.scale;
  130. }
  131. else
  132. {
  133. this.globalPosition.x = this.parent.globalPosition.x + this.position.x;
  134. this.globalPosition.y = this.parent.globalPosition.y + this.position.y;
  135. this.globalRotationDegrees = this.parent.globalRotationDegrees + this.rotationDegrees;
  136. this.globalScale.x = this.parent.globalScale.x * this.scale.x;
  137. this.globalScale.y = this.parent.globalScale.y * this.scale.y;
  138. }
  139. // Callbacks
  140. this._update(delta);
  141. for (let i = 0; i < this.children.length; i++)
  142. {
  143. this.children[i].update(delta);
  144. }
  145. }
  146. /**
  147. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  148. * and recursively calls the same callback on all of it's children. This results in the
  149. * appearence of relative position.
  150. *
  151. * @param {number} delta number of seconds ellapsed since the last frame.
  152. * @param {p5.Graphics} db secondary buffer to draw to.
  153. *
  154. * @override
  155. */
  156. draw(delta, db)
  157. {
  158. if (!this.visible) return;
  159. db.push();
  160. db.translate(this.position.x, this.position.y);
  161. db.rotate(this.rotationDegrees / 180 * PI);
  162. db.scale(this.scale.x, this.scale.y);
  163. this._draw(delta, db);
  164. for (let i = 0; i < this.children.length; i++)
  165. this.children[i].draw(delta, db);
  166. db.pop()
  167. }
  168. }