Sprite2D.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /************************************************************************
  2. * Sprite2D.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 Sprite2D} class represents a GameObject that inherits from
  23. * Object2D and extends its functionality to automatically draw a Sprite on the
  24. * buffer.
  25. *
  26. * @author Pedro Schneider
  27. *
  28. * @class
  29. */
  30. class Sprite2D extends Object2D
  31. {
  32. /**
  33. * @constructor
  34. * Initializes a Sprite2D GameObject with the specified parameters.
  35. *
  36. * @param {String} name name for this Sprite2D.
  37. * @param {p5.Image} p5Image p5.Image to be drawn on the buffer.
  38. */
  39. constructor(name, p5Image)
  40. {
  41. super(name);
  42. this.P5Image = p5Image; // This Sprite2D p5.Image.
  43. }
  44. /**
  45. * @override
  46. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  47. * and recursively calls the same callback on all of it's children. Also draws an image
  48. * on the buffer based on the data passed to this GameObject.
  49. *
  50. * @param {number} delta number in seconds ellapsed since the last frame.
  51. * @param {p5.Graphics} db secondary buffer to draw to.
  52. */
  53. draw(delta, db)
  54. {
  55. db.push();
  56. db.translate(this.position.x, this.position.y);
  57. db.rotate(this.rotationDegrees);
  58. db.scale(this.scale.x, this.scale.y);
  59. db.image(this.P5Image, 0, 0, this.P5Image.width, this.P5Image.height);
  60. this._draw(delta, db);
  61. for (let i = 0; i < this.children.length; i++)
  62. this.children[i].draw(delta, db);
  63. db.pop();
  64. }
  65. }