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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var assetsPath = "assets/"+conf.assets;
  2. var commonPath = "assets/common";
  3. var assets = {
  4. imgs: {
  5. player: new ImageSource("theme:player"),
  6. player_flipped: new ImageSource("theme:player_flipped"),
  7. background: new ImageSource("background", null, ".jpg"),
  8. wall: new ImageSource("wall"),
  9. wall_overlap: new ImageSource("wall-overlap"),
  10. },
  11. audio: {
  12. hurt: new AudioSource("audio/hurt"),
  13. loss: new AudioSource("audio/loss"),
  14. flap: new AudioSource("audio/flap"),
  15. chomp: new AudioSource("audio/chomp"),
  16. },
  17. soundtracks: {
  18. soundtrack1: new AudioSource("soundtracks/track1"),
  19. soundtrack2: new AudioSource("soundtracks/track2"),
  20. soundtrack3: new AudioSource("soundtracks/track3"),
  21. },
  22. };
  23. /*
  24. * Audio source.
  25. */
  26. function AudioSource(src, ext) {
  27. makeEventListener(this);
  28. var ext = ext || ".mp3";
  29. this.elems = [];
  30. for (var i = 0; i < 10; ++i)
  31. this.elems[i] = new Audio(commonPath+"/"+src+ext);
  32. this.ready = false;
  33. this.index = 0;
  34. this.elems[0].addEventListener("canplaythrough", function() {
  35. if (this.ready)
  36. return;
  37. this.ready = true;
  38. this.emit("load");
  39. }.bind(this));
  40. this.elems[0].addEventListener("ended", function() {
  41. this.emit("ended");
  42. }.bind(this));
  43. this.elems[0].onerror = function() {
  44. console.error("Error with", src);
  45. }
  46. }
  47. AudioSource.prototype.play = function() {
  48. this.elems[this.index++].play();
  49. this.index %= this.elems.length;
  50. }
  51. AudioSource.prototype.pause = function() {
  52. for (var i in this.elems)
  53. this.elmes[i].pause();
  54. }
  55. /*
  56. * Image source.
  57. */
  58. function ImageSource(src, frameh, ext) {
  59. var ext = ext || ".png";
  60. if (src.indexOf("theme:") == 0) {
  61. src = assetsPath+"/images/"+src.split(":")[1];
  62. } else {
  63. src = commonPath+"/images/"+src;
  64. }
  65. this.img = document.createElement("img");
  66. this.ready = false;
  67. this.width = 0;
  68. this.height = 0;
  69. this.frameh = frameh == null ? -1 : frameh;
  70. this.steps = 1;
  71. this.img.onload = function() {
  72. this.ready = true;
  73. this.width = this.img.width;
  74. this.height = this.img.height;
  75. if (this.frameh === -1)
  76. this.frameh = this.height;
  77. this.steps = this.height / this.frameh;
  78. if (this.steps !== Math.round(this.steps)) {
  79. this.steps = Math.round(this.steps);
  80. console.log(
  81. "Warning: '"+src+ext+"': "+
  82. "Height isn't evenly divisible by frame height. "+
  83. "Height: "+this.height+", frame height: "+this.frameh);
  84. }
  85. }.bind(this);
  86. this.img.src = src+ext
  87. this.img.onerror = function() {
  88. console.error("error with", src);
  89. }
  90. }
  91. ImageSource.prototype.draw = function(ctx, step) {
  92. if (!step) step = 0;
  93. if (!this.ready)
  94. return;
  95. ctx.drawImage(this.img,
  96. 0, this.frameh * step,
  97. this.width, this.frameh,
  98. 0, 0,
  99. this.width, this.frameh);
  100. }
  101. function Animation(imgSrc, type, loop, fps) {
  102. this.type = type || "forward";
  103. this.loop = loop || false;
  104. this.imgSrc = imgSrc;
  105. this.fps = fps || 12;
  106. this.waitTime = 1000 / this.fps;
  107. this.doStep = false;
  108. if (type === "forward" || type === "bounce") {
  109. this.step = 0;
  110. this.direction = 1;
  111. } else if (type === "reverse" || type === "bounce-reverse") {
  112. this.step = -1;
  113. this.direction = -1;
  114. }
  115. // We don't care about the direction after setting this.direction
  116. if (this.type === "bounce-reverse")
  117. this.type = "bounce";
  118. else if (this.type === "reverse")
  119. this.type = "forward";
  120. }
  121. Animation.prototype.nextFrame = function() {
  122. if (!this.imgSrc.ready) {
  123. return;
  124. } else if (this.step === -1) {
  125. this.step = this.imgSrc.steps - 1;
  126. return;
  127. } else if (this.imgSrc.steps === 1) {
  128. return;
  129. }
  130. var next = this.step + this.direction;
  131. if (next < 0 || next > this.imgSrc.steps - 1) {
  132. if (this.loop) {
  133. if (this.type === "bounce") {
  134. this.direction = -this.direction;
  135. next = this.step + this.direction;
  136. } else if (this.type === "forward") {
  137. if (next < 0)
  138. next = this.imgSrc.steps - 1;
  139. else
  140. next = 0;
  141. }
  142. } else {
  143. this.doStep = false;
  144. return;
  145. }
  146. }
  147. this.step = next;
  148. }
  149. Animation.prototype.draw = function(ctx) {
  150. if (!this.imgSrc.ready)
  151. return;
  152. this.imgSrc.draw(ctx, this.step);
  153. if (this.doStep) {
  154. this.nextFrame();
  155. this.doStep = false;
  156. setTimeout(function() {
  157. this.doStep = true;
  158. }.bind(this), this.waitTime);
  159. }
  160. }
  161. Animation.prototype.play = function() {
  162. if (this.step !== -1)
  163. this.step = 0;
  164. this.doStep = true;
  165. }
  166. Animation.prototype.setStep = function(step) {
  167. if (step > this.imgSrc.steps - 1)
  168. this.step = this.imgSrc.steps - 1;
  169. else if (step < 0)
  170. this.step = 0;
  171. else
  172. this.step = step;
  173. }