Area2D.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. switch (this.shapeType)
  52. {
  53. case SHAPES.RECT:
  54. this.shapeName = "Rect";
  55. break;
  56. case SHAPES.ELLIPSE:
  57. this.shapeName = "Ellipse";
  58. break;
  59. }
  60. this.shape = shape;
  61. this.listenToMouse = listenToMouse;
  62. this.drawDebug = drawDebug;
  63. this.mouseIn = false;
  64. }
  65. /**
  66. * Sets wether or not the Area2D's shape should be drawn to the screen.
  67. *
  68. * @param {boolean} val new value for drawDebug.
  69. */
  70. setDrawDebug(val)
  71. {
  72. this.drawDebug = val;
  73. }
  74. /**
  75. * Defines default signals for Area2Ds and serves as the caller to this Area2Ds's
  76. * _initSignals() callbacks.
  77. *
  78. * @signal mouseEntered emited once every time the mouse enters this Area2D's shape.
  79. * @signal mouseExited emited once every time the mouse exits this Area2D's shape.
  80. *
  81. * @override
  82. */
  83. initSignals()
  84. {
  85. this.addSignal("mouseEntered");
  86. this.addSignal("mouseExited");
  87. this._initSignals();
  88. }
  89. /**
  90. * Checks for the mouse position over this Area2D's shape and recursively calls the _update(delta)
  91. * callback for this GameObject and all of it's children.
  92. *
  93. * @param {number} delta number of ellapsed seconds since the last frame.
  94. *
  95. * @override
  96. */
  97. update(delta)
  98. {
  99. // Updates global transform
  100. if (!this.parented || !(this.parent instanceof Object2D))
  101. {
  102. this.globalPosition = this.position;
  103. this.globalRotationDegrees = this.rotationDegrees;
  104. this.globalScale = this.globalScale;
  105. }
  106. else
  107. {
  108. this.globalPosition.x = this.parent.globalPosition.x + this.position.x;
  109. this.globalPosition.y = this.parent.globalPosition.y + this.position.y;
  110. this.globalRotationDegrees = this.parent.globalRotationDegrees + this.rotationDegrees;
  111. this.globalScale.x = this.parent.globalScale.x * this.scale.x;
  112. this.globalScale.y = this.parent.globalScale.y * this.scale.y;
  113. }
  114. // Checks collision with mouse
  115. if (this.listenToMouse)
  116. {
  117. if (Collisions[this.shapeName].point(this.globalPosition.x, this.globalPosition.y, this.globalRotationDegrees, this.globalScale.x,
  118. this.globalScale.y, this.shape, GameHandler.mouseX, GameHandler.mouseY))
  119. {
  120. if (!this.mouseIn)
  121. this.emitSignal("mouseEntered");
  122. this.mouseIn = true;
  123. }
  124. else
  125. {
  126. if (this.mouseIn)
  127. this.emitSignal("mouseExited");
  128. this.mouseIn = false;
  129. }
  130. }
  131. // Callbacks
  132. this._update(delta);
  133. for (let i = 0; i < this.children.length; i++)
  134. this.children[i].update(delta);
  135. }
  136. /**
  137. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  138. * and recursively calls the same callback on all of it's children. Also draws this Area2D's
  139. * shape to the secondary buffer if requested.
  140. *
  141. * @param {number} delta number in seconds ellapsed since the last frame.
  142. * @param {p5.Graphics} db secondary buffer to draw to.
  143. *
  144. * @override
  145. */
  146. draw(delta, db)
  147. {
  148. if (!this.visible) return;
  149. db.push();
  150. db.translate(this.position.x, this.position.y);
  151. db.rotate(this.rotationDegrees / 180 * PI);
  152. db.scale(this.scale.x, this.scale.y);
  153. if (this.drawDebug)
  154. {
  155. db.push();
  156. db.noStroke();
  157. db.fill(0, 0, 100, 100);
  158. switch (this.shapeType)
  159. {
  160. case SHAPES.ELLIPSE:
  161. db.ellipseMode(CENTER);
  162. db.ellipse(0, 0, this.shape.rx * 2, this.shape.ry * 2);
  163. break;
  164. case SHAPES.RECT:
  165. db.rectMode(CENTER);
  166. db.rect(0, 0, this.shape.w, this.shape.h);
  167. break;
  168. }
  169. db.pop();
  170. }
  171. this._draw(delta, db);
  172. for (let i = 0; i < this.children.length; i++)
  173. this.children[i].draw(delta, db);
  174. db.pop()
  175. }
  176. }