util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.lineWidth = w;
  27. ctx.strokeStyle = color;
  28. ctx.stroke();
  29. }
  30. function sign(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
  31. function lerp(from, to, p) {
  32. return to * p + from * (1 - p);
  33. }
  34. function ease(v) { return v * v * (3 - 2 * v); }
  35. function easeLinear(t, b, c, d) {
  36. return lerp(b, c, t/d);
  37. }
  38. function easeOutExpo(t, b, c, d) {
  39. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  40. }
  41. function color(r,g,b,a) {
  42. return 'rgba('+r+','+g+','+b+','+a.toFixed(5)+')';
  43. }
  44. function font(size) {
  45. return size+'px Quicksand, Futura, Arial, sans-serif';
  46. }
  47. function makeObservable(o) {
  48. var listeners = [];
  49. o.hasTicker = true;
  50. o.addListener = function(listener) {
  51. listeners.push(listener);
  52. };
  53. o.removeListener = function(listener) {
  54. var index = listeners.indexOf(listener);
  55. listeners.splice(index, 1);
  56. };
  57. o.notify = function(dt) {
  58. listeners.forEach(function(listener) {
  59. listener(dt);
  60. })
  61. };
  62. }
  63. // Ticker
  64. var Ticker = {};
  65. (function(Ticker) {
  66. makeObservable(Ticker);
  67. var nextFrame =
  68. window.requestAnimationFrame ||
  69. window.webkitRequestAnimationFrame ||
  70. window.mozRequestAnimationFrame ||
  71. function (callback) {
  72. setTimeout(function () {
  73. callback(Date.now())
  74. }, 10)
  75. };
  76. var then = null;
  77. function tick(now) {
  78. var delta = (now - then) / 1000;
  79. Ticker.notify(delta);
  80. then = now;
  81. nextFrame(tick);
  82. }
  83. function init(now) {
  84. then = now;
  85. nextFrame(tick);
  86. }
  87. Ticker.start = function() {
  88. nextFrame(init);
  89. }
  90. })(Ticker);
  91. // Tweens
  92. function BaseTween() {}
  93. BaseTween.prototype.animate = function(scene) {
  94. var self = this;
  95. function tick(dt) {
  96. self.update(dt);
  97. }
  98. var ticker = Ticker;
  99. if(this.ticker) ticker = this.ticker;
  100. ticker.addListener(tick);
  101. self.promise.then(function() {
  102. ticker.removeListener(tick);
  103. });
  104. self.update(0);
  105. return self;
  106. };
  107. function SimpleTween(duration, start, end, easing) {
  108. this.duration = duration;
  109. this.start = start;
  110. this.end = end;
  111. this.easing = easing || easeLinear;
  112. this.deferred = Q.defer();
  113. this.timer = 0;
  114. this.promise = this.deferred.promise;
  115. if(this.duration == 0) {
  116. this.deferred.resolve();
  117. }
  118. };
  119. SimpleTween.prototype = new BaseTween();
  120. SimpleTween.prototype.update = function(dt) {
  121. if(this.duration > 0) {
  122. this.timer += dt;
  123. if(this.timer >= this.duration) {
  124. this.timer = this.duration;
  125. this.deferred.resolve();
  126. }
  127. this.value = this.easing(this.timer, this.start, this.end, this.duration);
  128. }
  129. else {
  130. this.value = this.end;
  131. }
  132. };
  133. function PromiseTween(value, promise) {
  134. this.value = value;
  135. this.promise = promise;
  136. }
  137. PromiseTween.prototype.update = function() {};
  138. function ComposedTween(tweens) {
  139. this.tweens = tweens;
  140. this.promise = Q.all(this.tweens.map(function(t) { return t.promise }));
  141. this.switch(0);
  142. }
  143. ComposedTween.prototype = new BaseTween();
  144. ComposedTween.prototype.switch = function(index) {
  145. if(index >= this.tweens.length) return;
  146. var self = this;
  147. this.current = index;
  148. this.tweens[index].promise.then(function() {
  149. self.switch(index + 1);
  150. });
  151. };
  152. ComposedTween.prototype.update = function(dt) {
  153. if(this.current >= this.tweens.length) return;
  154. var active = this.tweens[this.current];
  155. active.update(dt);
  156. this.value = active.value;
  157. };
  158. function DeferredTween(start) {
  159. this.lastTarget = start;
  160. this.tweens = [];
  161. }
  162. DeferredTween.prototype = new BaseTween();
  163. DeferredTween.prototype.addTween = function(tween) {
  164. this.tweens.push(tween);
  165. this.tween = new ComposedTween(this.tweens);
  166. this.promise = this.tween.promise;
  167. return this;
  168. }
  169. DeferredTween.prototype.to = function(target, duration, easing) {
  170. easing = easing || easeLinear;
  171. this.addTween(new SimpleTween(duration, this.lastTarget, target, easing));
  172. this.lastTarget = target;
  173. return this;
  174. };
  175. DeferredTween.prototype.wait = function(duration) {
  176. return this.to(this.lastTarget, duration);
  177. };
  178. DeferredTween.prototype.waitFor = function(promise) {
  179. return this.addTween(new PromiseTween(this.lastTarget, promise));
  180. };
  181. DeferredTween.prototype.update = function(dt) {
  182. this.tween.update(dt);
  183. this.value = this.tween.value;
  184. };