SpriteFrames.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /************************************************************************
  2. * SpriteFrames.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 SpriteAnimation
  22. {
  23. constructor(name = "default", p5Image, rows, cols, indices, fps)
  24. {
  25. this.name = name;
  26. this.fullP5Image = p5Image;
  27. this.rows = rows;
  28. this.columns = cols;
  29. this.indices = indices;
  30. this.numFrames = this.indices.length;
  31. this.frameTime = 1 / fps;
  32. this.frames = [];
  33. for (let i = 0; i < this.numFrames; i++)
  34. {
  35. this.frames.push(this.makeFrame(i));
  36. }
  37. }
  38. setFrameTime(time)
  39. {
  40. this.frameTime = time;
  41. }
  42. setFPS(fps)
  43. {
  44. this.frameTime = 1 / fps;
  45. }
  46. getFrame(idx)
  47. {
  48. if (idx < this.numFrames)
  49. return this.frames[idx];
  50. return null;
  51. }
  52. getFrameTime()
  53. {
  54. return this.frameTime;
  55. }
  56. getNumFrames()
  57. {
  58. return this.numFrames;
  59. }
  60. makeFrame(idx)
  61. {
  62. let r = int(this.indices[idx] / this.columns);
  63. let c = this.indices[idx] % this.columns;
  64. let w = this.fullP5Image.width / this.columns;
  65. let h = this.fullP5Image.height / this.rows;
  66. let x = c * w;
  67. let y = r * h;
  68. return this.fullP5Image.get(x, y, w, h);
  69. }
  70. }
  71. class SpriteFrames
  72. {
  73. constructor()
  74. {
  75. this.animations = [];
  76. this.numAnimations = 0;
  77. }
  78. getAnimationByIndex(idx)
  79. {
  80. if (idx < this.numAnimations) return this.animations[idx];
  81. return null;
  82. }
  83. getAnimationByName(name)
  84. {
  85. for (let i = 0; this.numAnimations; i++)
  86. {
  87. if (this.animations[i].name == name)
  88. {
  89. return this.animations[i];
  90. }
  91. }
  92. return null;
  93. }
  94. getAnimationIndexByName(name)
  95. {
  96. for (let i = 0; this.numAnimations; i++)
  97. {
  98. if (this.animations[i].name == name)
  99. {
  100. return i;
  101. }
  102. }
  103. return null;
  104. }
  105. addAnimation(name, p5Image, rows, cols, indices, fps = 24)
  106. {
  107. this.animations.push(new SpriteAnimation(name, p5Image, rows, cols, indices, fps));
  108. this.numAnimations++;
  109. }
  110. }