AnimatedSprite2D.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /************************************************************************
  2. * AnimatedSprite2D.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. class AnimatedSprite2D extends Sprite2D
  22. {
  23. constructor(name, p5Image, spriteFrames)
  24. {
  25. super(name, p5Image);
  26. this.spriteFrames = spriteFrames;
  27. this.playing = false;
  28. this.frame = 0;
  29. this.currentAnimation = 0;
  30. this.timeSinceLastFrame = 0;
  31. }
  32. // Setters
  33. setCurrentAnimationByIndex(idx)
  34. {
  35. if (idx < this.spriteFrames.numAnimations)
  36. this.currentAnimation = idx;
  37. else this.currentAnimation = null;
  38. this.frame = 0;
  39. this.timeSinceLastFrame = 0;
  40. }
  41. setCurrentAnimationByName(name)
  42. {
  43. this.currentAnimation = this.spriteFrames.getAnimationIndexByName(name);
  44. this.frame = 0;
  45. this.timeSinceLastFrame = 0;
  46. }
  47. setCurrentFrameTime(time)
  48. {
  49. this.getCurrentAnimation().setFrameTime(time);
  50. }
  51. setCurrentFPS(fps)
  52. {
  53. this.getCurrentAnimation().setFPS(fps);
  54. }
  55. // Getters
  56. getAnimationByIndex(idx)
  57. {
  58. return this.spriteFrames.getAnimationByIndex(idx);
  59. }
  60. getCurrentAnimation()
  61. {
  62. return this.spriteFrames.getAnimationByIndex(this.currentAnimation);
  63. }
  64. getCurrentFrame()
  65. {
  66. return this.getCurrentAnimation().getFrame(this.frame);
  67. }
  68. getCurrentFrameTime()
  69. {
  70. return this.getCurrentAnimation().getFrameTime();
  71. }
  72. getCurrentNumFrames()
  73. {
  74. return this.getCurrentAnimation().getNumFrames();
  75. }
  76. // Methods
  77. play()
  78. {
  79. this.playing = true;
  80. }
  81. stop()
  82. {
  83. this.playing = false;
  84. }
  85. isPlaying()
  86. {
  87. return this.playing;
  88. }
  89. update(delta)
  90. {
  91. if (this.playing)
  92. {
  93. this.timeSinceLastFrame += delta;
  94. if (this.timeSinceLastFrame >= this.getCurrentFrameTime())
  95. {
  96. this.frame = (this.frame + 1) % this.getCurrentNumFrames();
  97. this.timeSinceLastFrame = 0;
  98. }
  99. }
  100. this.P5Image = this.getCurrentFrame();
  101. this._update(delta);
  102. for (let i = 0; i < this.children.length; i++)
  103. this.children[i].update(delta);
  104. }
  105. }