Area2D.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Updates global transform
  91. if (!this.parented || !(this.parent instanceof Object2D))
  92. {
  93. this.globalPosition = this.position;
  94. this.globalRotationDegrees = this.rotationDegrees;
  95. this.globalScale = this.globalScale;
  96. }
  97. else
  98. {
  99. this.globalPosition.x = this.parent.globalPosition.x + this.position.x;
  100. this.globalPosition.y = this.parent.globalPosition.y + this.position.y;
  101. this.globalRotationDegrees = this.parent.globalRotationDegrees + this.rotationDegrees;
  102. this.globalScale.x = this.parent.globalScale.x + this.scale.x;
  103. this.globalScale.y = this.parent.globalScale.y + this.scale.y;
  104. }
  105. // Checks collision with mouse
  106. if (this.listenToMouse)
  107. {
  108. if (this.shape.isIn(GameHandler.mouseX - this.globalPosition.x, GameHandler.mouseY - this.globalPosition.y))
  109. {
  110. if (!this.mouseIn)
  111. this.emitSignal("mouseEntered");
  112. this.mouseIn = true;
  113. }
  114. else
  115. {
  116. if (this.mouseIn)
  117. this.emitSignal("mouseExited");
  118. this.mouseIn = false;
  119. }
  120. }
  121. // Callbacks
  122. this._update(delta);
  123. for (let i = 0; i < this.children.length; i++)
  124. this.children[i].update(delta);
  125. }
  126. /**
  127. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  128. * and recursively calls the same callback on all of it's children. Also draws this Area2D's
  129. * shape to the secondary buffer if requested.
  130. *
  131. * @param {number} delta number in seconds ellapsed since the last frame.
  132. * @param {p5.Graphics} db secondary buffer to draw to.
  133. *
  134. * @override
  135. */
  136. draw(delta, db)
  137. {
  138. if (!this.visible) return;
  139. db.push();
  140. db.translate(this.position.x, this.position.y);
  141. db.rotate(this.rotationDegrees / 180 * PI);
  142. db.scale(this.scale.x, this.scale.y);
  143. if (this.drawDebug)
  144. {
  145. db.push();
  146. db.noStroke();
  147. db.fill(0, 0, 100, 100);
  148. switch (this.shapeType)
  149. {
  150. case SHAPES.ELLIPSE:
  151. db.ellipseMode(CENTER);
  152. db.ellipse(0, 0, this.shape.rx * 2, this.shape.ry * 2);
  153. break;
  154. case SHAPES.RECT:
  155. db.rectMode(CENTER);
  156. db.rect(0, 0, this.shape.w, this.shape.h);
  157. break;
  158. }
  159. db.pop();
  160. }
  161. this._draw(delta, db);
  162. for (let i = 0; i < this.children.length; i++)
  163. this.children[i].draw(delta, db);
  164. db.pop()
  165. }
  166. }