Object2D.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * @author Pedro Schneider
  27. *
  28. * @class
  29. */
  30. class Object2D extends GameObject
  31. {
  32. /**
  33. * @constructor
  34. * Initializes an empty Object2D GameObject.
  35. *
  36. * @param {String} name name of the Object2D GameObject.
  37. */
  38. constructor(name)
  39. {
  40. super(name);
  41. this.position = Vector2.ZERO(); // This Object2D's position on the secondary buffer.
  42. this.rotationDegrees = 0; // This Object2D's rotation degrees on the secondary buffer.
  43. this.scale = Vector2.ONE(); // This Object2D's scale on the secondary buffer.
  44. this.visible = true; // Is this Object2D visible at the moment?
  45. }
  46. /**
  47. * Sets the visibility flag of this Object2D and all of its children
  48. * that have a visibility flag to true.
  49. */
  50. show()
  51. {
  52. this.visible = true;
  53. for (let i = 0; i < this.children.length; i++)
  54. {
  55. if (!this.children[i].show) continue;
  56. this.children[i].show();
  57. }
  58. }
  59. /**
  60. * Sets the visibility flag of this Object2D and all of its children
  61. * that have a visibility flag to false.
  62. */
  63. hide()
  64. {
  65. this.visible = false;
  66. for (let i = 0; i < this.children.length; i++)
  67. {
  68. if (!this.children[i].hide) continue;
  69. this.children[i].hide();
  70. }
  71. }
  72. /**
  73. * Sets the visibility flag of this Object2D and all of its children that have a visibility
  74. * flag to the provided value.
  75. *
  76. * @param {boolean} val value to set the flag to.
  77. */
  78. setVisibility(val)
  79. {
  80. this.visible = val;
  81. for (let i = 0; i < this.children.length; i++)
  82. {
  83. if (!this.children[i].setVisibility) continue;
  84. this.children[i].setVisibility(val);
  85. }
  86. }
  87. /**
  88. * Returns this Object2D's visibility flag.
  89. *
  90. * @returns {boolean} true if this Object2D is visible, false if not.
  91. */
  92. getVisibility()
  93. {
  94. return this.visible;
  95. }
  96. /**
  97. * @override
  98. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  99. * and recursively calls the same callback on all of it's children. This results in the
  100. * appearence of relative position.
  101. *
  102. * @param {number} delta number of seconds ellapsed since the last frame.
  103. * @param {p5.Graphics} db secondary buffer to draw to.
  104. */
  105. draw(delta, db)
  106. {
  107. if (!this.visible) return;
  108. db.push();
  109. db.translate(this.position.x, this.position.y);
  110. db.rotate(this.rotationDegrees);
  111. db.scale(this.scale.x, this.scale.y);
  112. this._draw(delta, db);
  113. for (let i = 0; i < this.children.length; i++)
  114. this.children[i].draw(delta, db);
  115. db.pop()
  116. }
  117. }