Object2D.js 4.1 KB

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