Sprite2D.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. * ! 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 Sprite2D extends Object2D
  34. {
  35. /**
  36. * Initializes a Sprite2D GameObject with the specified parameters.
  37. *
  38. * @param {String} name name for this Sprite2D.
  39. * @param {p5.Image} p5Image p5.Image to be drawn on the buffer.
  40. *
  41. * @constructor
  42. */
  43. constructor(name, p5Image)
  44. {
  45. super(name);
  46. this.P5Image = p5Image; // This Sprite2D p5.Image.
  47. }
  48. /**
  49. * Applies this Object2D's transform before calling this GameObject's _draw() callback
  50. * and recursively calls the same callback on all of it's children. Also draws an image
  51. * on the buffer based on the data passed to this GameObject.
  52. *
  53. * @param {number} delta number in seconds ellapsed since the last frame.
  54. * @param {p5.Graphics} db secondary buffer to draw to.
  55. *
  56. * @override
  57. */
  58. draw(delta, db)
  59. {
  60. db.push();
  61. this.applyTransform(db);
  62. db.image(this.P5Image, 0, 0, this.P5Image.width, this.P5Image.height);
  63. this.drawChildren(delta, db);
  64. db.pop();
  65. }
  66. }