Game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

assets.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var assets = {
  2. };
  3. /*
  4. * Image source.
  5. * If animation, the images must be stacked on top of each other.
  6. */
  7. function ImageSource(src, frameh) {
  8. var ext = ".png";
  9. this.img = document.createElement("img");
  10. this.ready = false;
  11. this.width = 0;
  12. this.height = 0;
  13. this.frameh = frameh == null ? -1 : frameh;
  14. this.steps = 1;
  15. this.img.onload = function() {
  16. this.ready = true;
  17. this.width = this.img.width;
  18. this.height = this.img.height;
  19. if (this.frameh === -1)
  20. this.frameh = this.height;
  21. this.steps = this.height / this.frameh;
  22. if (this.steps !== Math.round(this.steps)) {
  23. this.steps = Math.round(this.steps);
  24. console.log(
  25. "Warning: '"+src+ext+"': "+
  26. "Height isn't evenly divisible by frame height. "+
  27. "Height: "+this.height+", frame height: "+this.frameh);
  28. }
  29. }.bind(this);
  30. this.img.src = "assets/"+src+ext;
  31. }
  32. ImageSource.prototype.draw = function(ctx, step) {
  33. if (!step) step = 0;
  34. ctx.drawImage(this.img, 0, 0,
  35. this.width, this.frameh,
  36. 0, this.frameh * step,
  37. this.width, this.frameh);
  38. }
  39. function Animation(imgSrc, type, loop, fps) {
  40. this.type = type || "forward";
  41. this.loop = loop || false;
  42. this.imgSrc = imgSrc;
  43. this.fps = fps || 12;
  44. this.waitTime = 1000 / this.fps;
  45. this.done = false;
  46. this.doStep = true;
  47. if (type === "forward" || type === "bounce") {
  48. this.step = 0;
  49. this.direction = 1;
  50. } else if (type === "reverse" || type === "bounce-reverse") {
  51. this.step = -1;
  52. this.direction = -1;
  53. }
  54. // We don't care about the direction after setting this.direction
  55. if (this.type === "bounce-reverse")
  56. this.type = "bounce";
  57. else if (this.type === "reverse")
  58. this.type = "forward";
  59. }
  60. Animation.prototype.nextFrame = function() {
  61. if (!this.imgSrc.ready) {
  62. return;
  63. } else if (this.step === -1) {
  64. this.step = this.imgSrc.steps - 1;
  65. return;
  66. } else if (this.imgSrc.steps === 1) {
  67. return;
  68. }
  69. var next = this.step + this.direction;
  70. if (next < 0 || next > this.imgSrc.steps - 1) {
  71. if (this.loop) {
  72. this.direction = -this.direction;
  73. next = this.step + this.direction;
  74. } else {
  75. this.done = true;
  76. return;
  77. }
  78. }
  79. this.step = next;
  80. }
  81. Animation.prototype.draw = function(ctx) {
  82. if (!this.imgSrc.ready)
  83. return;
  84. this.imgSrc.draw(ctx, this.step);
  85. if (this.doStep) {
  86. this.nextFrame();
  87. this.doStep = false;
  88. this.setTimeout(function() {
  89. this.doStep = true;
  90. }.bind(this), this.waitTime);
  91. }
  92. }