Game
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

assets.js 3.7KB

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