Object2D.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * Translates this Object2D by (x, y).
  116. *
  117. * @param {number} x X-axis translation.
  118. * @param {number} y Y-axis translation.
  119. */
  120. translate(x, y)
  121. {
  122. this.position.x += x;
  123. this.position.y += y;
  124. }
  125. /**
  126. * Rotates this Object2D by a degrees.
  127. *
  128. * @param {number} a rotation in degrees.
  129. */
  130. rotate(a)
  131. {
  132. this.rotationDegrees += a;
  133. }
  134. /**
  135. * Scales this Object2D by (x, y).
  136. *
  137. * @param {number} x X-axis scale.
  138. * @param {number} y Y-axis scale.
  139. */
  140. addScale(x, y)
  141. {
  142. this.scale.x *= x;
  143. this.scale.y *= y;
  144. }
  145. /**
  146. * Updates this Object2D's global transform and recursively calls the _update(delta)
  147. * callback for this GameObject and all of it's children.
  148. *
  149. * @param {number} delta number of ellapsed seconds since the last frame.
  150. *
  151. * @override
  152. */
  153. update(delta)
  154. {
  155. // Update global transform
  156. if (!this.parented || !(this.parent instanceof Object2D))
  157. {
  158. this.globalPosition = this.position;
  159. this.globalRotationDegrees = this.rotationDegrees;
  160. this.globalScale = this.scale;
  161. }
  162. else
  163. {
  164. this.globalPosition.x = this.parent.globalPosition.x + this.position.x;
  165. this.globalPosition.y = this.parent.globalPosition.y + this.position.y;
  166. this.globalRotationDegrees = this.parent.globalRotationDegrees + this.rotationDegrees;
  167. this.globalScale.x = this.parent.globalScale.x * this.scale.x;
  168. this.globalScale.y = this.parent.globalScale.y * this.scale.y;
  169. }
  170. // Callbacks
  171. this._update(delta);
  172. for (let i = 0; i < this.children.length; i++)
  173. {
  174. this.children[i].update(delta);
  175. }
  176. }
  177. /**
  178. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  179. * and recursively calls the same callback on all of it's children. This results in the
  180. * appearence of relative position.
  181. *
  182. * @param {number} delta number of seconds ellapsed since the last frame.
  183. * @param {p5.Graphics} db secondary buffer to draw to.
  184. *
  185. * @override
  186. */
  187. draw(delta, db)
  188. {
  189. if (!this.visible) return;
  190. db.push();
  191. db.translate(this.position.x, this.position.y);
  192. db.rotate(this.rotationDegrees / 180 * PI);
  193. db.scale(this.scale.x, this.scale.y);
  194. this._draw(delta, db);
  195. for (let i = 0; i < this.children.length; i++)
  196. this.children[i].draw(delta, db);
  197. db.pop()
  198. }
  199. }