Shape2D.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /************************************************************************
  2. * Shape2D.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 Shape2D} class represents a GameObject that inherits from
  23. * Object2D and extends its functionality to automatically draw a Shape on the
  24. * buffer, according to the data passed by a Shape component.
  25. *
  26. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  27. * ! of another GameObject on the tree or as a root).
  28. *
  29. * @author Pedro Schneider
  30. *
  31. * @class
  32. */
  33. class Shape2D extends Object2D
  34. {
  35. /**
  36. * Initializes a Shape2D GameObject with the specified parameters.
  37. *
  38. * @param {String} name name for this Shape2D;
  39. * @param {SHAPES} shapeType type of the shape for this Shape2D.
  40. * @param {Shape} shape dta for the shape of this Shape2D.
  41. *
  42. * @constructor
  43. */
  44. constructor(name, shapeType = null, shape = null)
  45. {
  46. super(name);
  47. this.shapeType = shapeType; // This Shape2D's shape type.
  48. this.shape = shape; // This Shape2D's shape data.
  49. this.shapeMode = CORNER; // This Shape2D's shape mode.
  50. this.fillColor = color(255); // This Shape2D's fill color.
  51. this.noFill = false; // Should this Shape2D be filled with a color?
  52. this.strokeWeight = 1; // This Shape2D's stroke weight.
  53. this.strokeColor = color(0); // This Shape2D's stroke color.
  54. this.noStroke = false; // Should this Shape2D have an outline?
  55. }
  56. /**
  57. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  58. * and recursively calls the same callback on all of it's children. Also draws a shape
  59. * on the buffer based on the data passed to this GameObject.
  60. *
  61. * @param {number} delta number in seconds ellapsed since the last frame.
  62. * @param {p5.Graphics} db secondary buffer to draw to.
  63. *
  64. * @override
  65. */
  66. draw(delta, db)
  67. {
  68. db.push();
  69. db.translate(this.position.x, this.position.y);
  70. db.rotate(this.rotationDegrees);
  71. db.scale(this.scale.x, this.scale.y);
  72. db.fill(this.fillColor);
  73. db.strokeWeight(this.strokeWeight)
  74. db.stroke(this.strokeColor);
  75. if (this.noFill) db.noFill();
  76. if (this.noStroke) db.noStroke();
  77. switch (this.shapeType)
  78. {
  79. case SHAPES.RECT:
  80. db.rectMode(this.shapeMode);
  81. db.rect(0, 0, this.shape.w, this.shape.h);
  82. break;
  83. case SHAPES.ELLIPSE:
  84. db.ellipseMode(this.shapeMode);
  85. db.ellipse(0, 0, this.shape.rx, this.shape.ry);
  86. }
  87. this._draw(delta, db);
  88. for (let i = 0; i < this.children.length; i++)
  89. this.children[i].draw(delta, db);
  90. db.pop()
  91. }
  92. }