Shape2D.js 3.5 KB

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