Object2D.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. }
  50. /**
  51. * Sets this Object2D's position.
  52. *
  53. * @param {number} x new position on the x axis for this Object2D.
  54. * @param {number} y new positoin on the y axis for this Object2D.
  55. */
  56. setPosition(x, y)
  57. {
  58. this.position.x = x;
  59. this.position.y = y;
  60. }
  61. /**
  62. * Sets the visibility flag of this Object2D and all of its children
  63. * that have a visibility flag to true.
  64. */
  65. show()
  66. {
  67. this.visible = true;
  68. for (let i = 0; i < this.children.length; i++)
  69. {
  70. if (!this.children[i].show) continue;
  71. this.children[i].show();
  72. }
  73. }
  74. /**
  75. * Sets the visibility flag of this Object2D and all of its children
  76. * that have a visibility flag to false.
  77. */
  78. hide()
  79. {
  80. this.visible = false;
  81. for (let i = 0; i < this.children.length; i++)
  82. {
  83. if (!this.children[i].hide) continue;
  84. this.children[i].hide();
  85. }
  86. }
  87. /**
  88. * Sets the visibility flag of this Object2D and all of its children that have a visibility
  89. * flag to the provided value.
  90. *
  91. * @param {boolean} val value to set the flag to.
  92. */
  93. setVisibility(val)
  94. {
  95. this.visible = val;
  96. for (let i = 0; i < this.children.length; i++)
  97. {
  98. if (!this.children[i].setVisibility) continue;
  99. this.children[i].setVisibility(val);
  100. }
  101. }
  102. /**
  103. * Returns this Object2D's visibility flag.
  104. *
  105. * @returns {boolean} true if this Object2D is visible, false if not.
  106. */
  107. getVisibility()
  108. {
  109. return this.visible;
  110. }
  111. /**
  112. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  113. * and recursively calls the same callback on all of it's children. This results in the
  114. * appearence of relative position.
  115. *
  116. * @param {number} delta number of seconds ellapsed since the last frame.
  117. * @param {p5.Graphics} db secondary buffer to draw to.
  118. *
  119. * @override
  120. */
  121. draw(delta, db)
  122. {
  123. if (!this.visible) return;
  124. db.push();
  125. db.translate(this.position.x, this.position.y);
  126. db.rotate(this.rotationDegrees);
  127. db.scale(this.scale.x, this.scale.y);
  128. this._draw(delta, db);
  129. for (let i = 0; i < this.children.length; i++)
  130. this.children[i].draw(delta, db);
  131. db.pop()
  132. }
  133. }