util.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. function createSound(name, url) {
  2. var deferred = Q.defer();
  3. soundManager.createSound({
  4. id: name,
  5. url: url,
  6. autoLoad: true,
  7. onload: function() {
  8. deferred.resolve(this);
  9. }
  10. });
  11. return deferred.promise;
  12. }
  13. function drawTriangle(x, y, h, w) {
  14. var ctx = Game.context;
  15. ctx.beginPath();
  16. ctx.moveTo(x, y);
  17. ctx.lineTo(x - w, y - h/2);
  18. ctx.lineTo(x - w, y + h/2);
  19. ctx.closePath();
  20. ctx.fill();
  21. }
  22. function drawCircle(point, r, w, color) {
  23. var ctx = Game.context;
  24. ctx.beginPath();
  25. ctx.arc(point.x, point.y, r, 0, 2*Math.PI, false);
  26. ctx.closePath();
  27. ctx.lineWidth = w;
  28. ctx.strokeStyle = color;
  29. ctx.stroke();
  30. }
  31. function drawBG() {
  32. var ctx = Game.context;
  33. ctx.fillStyle = 'black';
  34. ctx.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
  35. }
  36. function clamp(n, low, high) {
  37. return Math.max(Math.min(n, high), low);
  38. }
  39. function sign(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
  40. function lerp(from, to, p) {
  41. return to * p + from * (1 - p);
  42. }
  43. function ease(v) { return v * v * (3 - 2 * v); }
  44. function easeLinear(t, b, c, d) {
  45. return lerp(b, c, t/d);
  46. }
  47. function easeOutExpo(t, b, c, d) {
  48. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  49. }
  50. function color(r,g,b,a) {
  51. return 'rgba('+r+','+g+','+b+','+a.toFixed(5)+')';
  52. }
  53. function font(size) {
  54. return size+'px Quicksand, Futura, Arial, sans-serif';
  55. }
  56. function makeObservable(o) {
  57. var listeners = [];
  58. o.hasTicker = true;
  59. o.addListener = function(listener) {
  60. listeners.push(listener);
  61. };
  62. o.removeListener = function(listener) {
  63. var index = listeners.indexOf(listener);
  64. listeners.splice(index, 1);
  65. };
  66. o.notify = function(dt) {
  67. listeners.forEach(function(listener) {
  68. listener(dt);
  69. })
  70. };
  71. }
  72. // Ticker
  73. var Ticker = {};
  74. (function(Ticker) {
  75. makeObservable(Ticker);
  76. var nextFrame =
  77. window.requestAnimationFrame ||
  78. window.webkitRequestAnimationFrame ||
  79. window.mozRequestAnimationFrame ||
  80. function (callback) {
  81. setTimeout(function () {
  82. callback(Date.now())
  83. }, 10)
  84. };
  85. var then = null;
  86. function tick(now) {
  87. var delta = (now - then) / 1000;
  88. Ticker.notify(delta);
  89. then = now;
  90. nextFrame(tick);
  91. }
  92. function init(now) {
  93. then = now;
  94. nextFrame(tick);
  95. }
  96. Ticker.start = function() {
  97. nextFrame(init);
  98. }
  99. })(Ticker);
  100. // Tweens
  101. function BaseTween() {}
  102. BaseTween.prototype.animate = function(scene) {
  103. var self = this;
  104. function tick(dt) {
  105. self.update(dt);
  106. }
  107. var ticker = Ticker;
  108. if(this.ticker) ticker = this.ticker;
  109. ticker.addListener(tick);
  110. self.promise.then(function() {
  111. ticker.removeListener(tick);
  112. });
  113. self.update(0);
  114. return self;
  115. };
  116. function SimpleTween(duration, start, end, easing) {
  117. this.duration = duration;
  118. this.start = start;
  119. this.end = end;
  120. this.easing = easing || easeLinear;
  121. this.deferred = Q.defer();
  122. this.timer = 0;
  123. this.promise = this.deferred.promise;
  124. if(this.duration == 0) {
  125. this.deferred.resolve();
  126. }
  127. };
  128. SimpleTween.prototype = new BaseTween();
  129. SimpleTween.prototype.update = function(dt) {
  130. if(this.duration > 0) {
  131. this.timer += dt;
  132. if(this.timer >= this.duration) {
  133. this.timer = this.duration;
  134. this.deferred.resolve();
  135. }
  136. this.value = this.easing(this.timer, this.start, this.end, this.duration);
  137. }
  138. else {
  139. this.value = this.end;
  140. }
  141. };
  142. function PromiseTween(value, promise) {
  143. this.value = value;
  144. this.promise = promise;
  145. }
  146. PromiseTween.prototype.update = function() {};
  147. function ComposedTween(tweens) {
  148. this.tweens = tweens;
  149. this.promise = Q.all(this.tweens.map(function(t) { return t.promise }));
  150. this.switch(0);
  151. }
  152. ComposedTween.prototype = new BaseTween();
  153. ComposedTween.prototype.switch = function(index) {
  154. if(index >= this.tweens.length) return;
  155. var self = this;
  156. this.current = index;
  157. this.tweens[index].promise.then(function() {
  158. self.switch(index + 1);
  159. });
  160. };
  161. ComposedTween.prototype.update = function(dt) {
  162. if(this.current >= this.tweens.length) return;
  163. var active = this.tweens[this.current];
  164. active.update(dt);
  165. this.value = active.value;
  166. };
  167. function DeferredTween(start) {
  168. this.lastTarget = start;
  169. this.tweens = [];
  170. }
  171. DeferredTween.prototype = new BaseTween();
  172. DeferredTween.prototype.addTween = function(tween) {
  173. this.tweens.push(tween);
  174. this.tween = new ComposedTween(this.tweens);
  175. this.promise = this.tween.promise;
  176. return this;
  177. }
  178. DeferredTween.prototype.to = function(target, duration, easing) {
  179. easing = easing || easeLinear;
  180. this.addTween(new SimpleTween(duration, this.lastTarget, target, easing));
  181. this.lastTarget = target;
  182. return this;
  183. };
  184. DeferredTween.prototype.wait = function(duration) {
  185. return this.to(this.lastTarget, duration);
  186. };
  187. DeferredTween.prototype.waitFor = function(promise) {
  188. return this.addTween(new PromiseTween(this.lastTarget, promise));
  189. };
  190. DeferredTween.prototype.update = function(dt) {
  191. this.tween.update(dt);
  192. this.value = this.tween.value;
  193. };
  194. function tweenFrom(start) {
  195. return new DeferredTween(start);
  196. };
  197. // Entities
  198. function Beater() {};
  199. (function() {
  200. var RADIUS = 8;
  201. var RIPPLE_SIZE = 10;
  202. var RIPPLE_SPEED = 10;
  203. Beater.prototype.init = function(x, y, color) {
  204. this.baseColor = color;
  205. this.rippleCounter = 0;
  206. this.position = {x: x, y: y};
  207. };
  208. Beater.prototype.update = function(dt) {
  209. this.rippleCounter += RIPPLE_SPEED * dt;
  210. if(this.rippleCounter > RIPPLE_SIZE) {
  211. this.rippleCounter = 0;
  212. }
  213. };
  214. Beater.prototype.draw = function() {
  215. var ctx = Game.context;
  216. ctx.fillStyle = this.baseColor;
  217. drawCircle(this.position, RADIUS, 3, this.baseColor);
  218. drawCircle(this.position, 2, 3, this.baseColor);
  219. // ripples
  220. var p = lerp(1, 0, ease(this.rippleCounter/RIPPLE_SIZE));
  221. ctx.globalAlpha = p;
  222. drawCircle(this.position, RADIUS+7+this.rippleCounter, 1, this.baseColor);
  223. ctx.globalAlpha = p*0.8;
  224. drawCircle(this.position, RADIUS+4+this.rippleCounter, 1, this.baseColor);
  225. ctx.globalAlpha = p*0.5;
  226. drawCircle(this.position, RADIUS+1+this.rippleCounter, 1, this.baseColor);
  227. ctx.globalAlpha = 1;
  228. };
  229. })();