Area2D.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. this.udpateGlobalTransform();
  100. // Checks collision with mouse
  101. if (this.listenToMouse)
  102. {
  103. if (Collisions[this.shapeName].point(this.globalPosition.x, this.globalPosition.y, this.globalRotationDegrees, this.globalScale.x,
  104. this.globalScale.y, this.shape, GameHandler.mouseX, GameHandler.mouseY))
  105. {
  106. if (!this.mouseIn)
  107. this.emitSignal("mouseEntered");
  108. this.mouseIn = true;
  109. }
  110. else
  111. {
  112. if (this.mouseIn)
  113. this.emitSignal("mouseExited");
  114. this.mouseIn = false;
  115. }
  116. }
  117. this.updateChildren(delta);
  118. }
  119. /**
  120. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  121. * and recursively calls the same callback on all of it's children. Also draws this Area2D's
  122. * shape to the secondary buffer if requested.
  123. *
  124. * @param {number} delta number in seconds ellapsed since the last frame.
  125. * @param {p5.Graphics} db secondary buffer to draw to.
  126. *
  127. * @override
  128. */
  129. draw(delta, db)
  130. {
  131. if (!this.visible) return;
  132. db.push();
  133. this.applyTransform(db);
  134. // Draw the shape for debug purposes.
  135. if (this.drawDebug)
  136. {
  137. db.push();
  138. db.noStroke();
  139. db.fill(0, 0, 100, 100);
  140. switch (this.shapeType)
  141. {
  142. case SHAPES.ELLIPSE:
  143. db.ellipseMode(CENTER);
  144. db.ellipse(0, 0, this.shape.rx * 2, this.shape.ry * 2);
  145. break;
  146. case SHAPES.RECT:
  147. db.rectMode(CENTER);
  148. db.rect(0, 0, this.shape.w, this.shape.h);
  149. break;
  150. }
  151. db.pop();
  152. }
  153. this.drawChildren(delta, db);
  154. db.pop()
  155. }
  156. }