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

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