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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. console.log(this);
  30. }.bind(this);
  31. this.img.src = "assets/"+src+ext;
  32. }
  33. ImageSource.prototype.draw = function(ctx, step) {
  34. if (!step) step = 0;
  35. ctx.drawImage(this.img,
  36. 0, this.frameh * step,
  37. this.width, this.frameh,
  38. 0, 0,
  39. this.width, this.frameh);
  40. }
  41. function Animation(imgSrc, type, loop, fps) {
  42. this.type = type || "forward";
  43. this.loop = loop || false;
  44. this.imgSrc = imgSrc;
  45. this.fps = fps || 12;
  46. this.waitTime = 1000 / this.fps;
  47. this.doStep = false;
  48. if (type === "forward" || type === "bounce") {
  49. this.step = 0;
  50. this.direction = 1;
  51. } else if (type === "reverse" || type === "bounce-reverse") {
  52. this.step = -1;
  53. this.direction = -1;
  54. }
  55. // We don't care about the direction after setting this.direction
  56. if (this.type === "bounce-reverse")
  57. this.type = "bounce";
  58. else if (this.type === "reverse")
  59. this.type = "forward";
  60. }
  61. Animation.prototype.nextFrame = function() {
  62. if (!this.imgSrc.ready) {
  63. return;
  64. } else if (this.step === -1) {
  65. this.step = this.imgSrc.steps - 1;
  66. return;
  67. } else if (this.imgSrc.steps === 1) {
  68. return;
  69. }
  70. var next = this.step + this.direction;
  71. if (next < 0 || next > this.imgSrc.steps - 1) {
  72. if (this.loop) {
  73. if (this.type === "bounce") {
  74. this.direction = -this.direction;
  75. next = this.step + this.direction;
  76. } else if (this.type === "forward") {
  77. if (next < 0)
  78. next = this.imgSrc.steps - 1;
  79. else
  80. next = 0;
  81. }
  82. } else {
  83. this.doStep = false;
  84. return;
  85. }
  86. }
  87. this.step = next;
  88. }
  89. Animation.prototype.draw = function(ctx) {
  90. if (!this.imgSrc.ready)
  91. return;
  92. this.imgSrc.draw(ctx, this.step);
  93. if (this.doStep) {
  94. this.nextFrame();
  95. this.doStep = false;
  96. setTimeout(function() {
  97. this.doStep = true;
  98. }.bind(this), this.waitTime);
  99. }
  100. }
  101. Animation.prototype.play = function() {
  102. if (this.step !== -1)
  103. this.step = 0;
  104. this.doStep = true;
  105. }
  106. Animation.prototype.setStep = function(step) {
  107. if (step > this.imgSrc.steps - 1)
  108. this.step = this.imgSrc.steps - 1;
  109. else if (step < 0)
  110. this.step = 0;
  111. else
  112. this.step = step;
  113. }