Area2D.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /************************************************************************
  2. * Area2D.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 Area2D} class represents a GameObject that has a shape and can
  23. * detect the mouse on the secondary buffer.
  24. *
  25. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  26. * ! of another GameObject on the tree or as a root).
  27. *
  28. * @author Pedro Schneider
  29. *
  30. * @class
  31. */
  32. class Area2D extends Object2D
  33. {
  34. /**
  35. * Creates an empty Area2D with the specified parameters.
  36. *
  37. * @param {String} name name for the Area2D GameObject.
  38. * @param {SHAPES} shapeType type of the shape on the Area2D.
  39. * @param {Shape} shape data for the shape of the Area2D.
  40. * @param {boolean} listenToMouse should this Area2D interact with the mouse?
  41. * Default is true.
  42. * @param {boolean} drawDebug should the Area2D's shape be drawn to the
  43. * secondary buffer? Default is false.
  44. *
  45. * @constructor
  46. */
  47. constructor(name, shapeType, shape, listenToMouse = true, drawDebug = false)
  48. {
  49. super(name);
  50. this.shapeType = shapeType;
  51. this.shape = shape;
  52. this.listenToMouse = listenToMouse;
  53. this.drawDebug = drawDebug;
  54. this.mouseIn = false;
  55. }
  56. /**
  57. * Sets wether or not the Area2D's shape should be drawn to the screen.
  58. *
  59. * @param {boolean} val new value for drawDebug.
  60. */
  61. setDrawDebug(val)
  62. {
  63. this.drawDebug = val;
  64. }
  65. /**
  66. * Defines default signals for Area2Ds and serves as the caller to this Area2Ds's
  67. * _initSignals() callbacks.
  68. *
  69. * @signal mouseEntered emited once every time the mouse enters this Area2D's shape.
  70. * @signal mouseExited emited once every time the mouse exits this Area2D's shape.
  71. *
  72. * @override
  73. */
  74. initSignals()
  75. {
  76. this.addSignal("mouseEntered");
  77. this.addSignal("mouseExited");
  78. this._initSignals();
  79. }
  80. /**
  81. * Checks for the mouse position over this Area2D's shape and recursively calls the _update(delta)
  82. * callback for this GameObject and all of it's children.
  83. *
  84. * @param {number} delta number of ellapsed seconds since the last frame.
  85. *
  86. * @override
  87. */
  88. update(delta)
  89. {
  90. if (this.listenToMouse)
  91. {
  92. if (this.shape.isIn(GameHandler.mouseX - this.position.x, GameHandler.mouseY - this.position.y))
  93. {
  94. if (!this.mouseIn)
  95. this.emitSignal("mouseEntered");
  96. this.mouseIn = true;
  97. }
  98. else
  99. {
  100. if (this.mouseIn)
  101. this.emitSignal("mouseExited");
  102. this.mouseIn = false;
  103. }
  104. }
  105. this._update(delta);
  106. for (let i = 0; i < this.children.length; i++)
  107. this.children[i].update(delta);
  108. }
  109. /**
  110. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  111. * and recursively calls the same callback on all of it's children. Also draws this Area2D's
  112. * shape to the secondary buffer if requested.
  113. *
  114. * @param {number} delta number in seconds ellapsed since the last frame.
  115. * @param {p5.Graphics} db secondary buffer to draw to.
  116. *
  117. * @override
  118. */
  119. draw(delta, db)
  120. {
  121. if (!this.visible) return;
  122. db.push();
  123. db.translate(this.position.x, this.position.y);
  124. db.rotate(this.rotationDegrees);
  125. db.scale(this.scale.x, this.scale.y);
  126. if (this.drawDebug)
  127. {
  128. db.push();
  129. db.noStroke();
  130. db.fill(0, 0, 100, 100);
  131. switch (this.shapeType)
  132. {
  133. case SHAPES.ELLIPSE:
  134. db.ellipseMode(CENTER);
  135. db.ellipse(0, 0, this.shape.rx * 2, this.shape.ry * 2);
  136. break;
  137. case SHAPES.RECT:
  138. db.rectMode(CENTER);
  139. db.rect(0, 0, this.shape.w, this.shape.h);
  140. break;
  141. }
  142. db.pop();
  143. }
  144. this._draw(delta, db);
  145. for (let i = 0; i < this.children.length; i++)
  146. this.children[i].draw(delta, db);
  147. db.pop()
  148. }
  149. }