util.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. };
  116. SimpleTween.prototype = new BaseTween();
  117. SimpleTween.prototype.update = function(dt) {
  118. this.timer += dt;
  119. if(this.timer >= this.duration) {
  120. this.timer = this.duration;
  121. this.deferred.resolve();
  122. }
  123. this.value = this.easing(this.timer, this.start, this.end, this.duration);
  124. };
  125. function PromiseTween(value, promise) {
  126. this.value = value;
  127. this.promise = promise;
  128. }
  129. PromiseTween.prototype.update = function() {};
  130. function ComposedTween(tweens) {
  131. this.tweens = tweens;
  132. this.promise = Q.all(this.tweens.map(function(t) { return t.promise }));
  133. this.switch(0);
  134. }
  135. ComposedTween.prototype = new BaseTween();
  136. ComposedTween.prototype.switch = function(index) {
  137. if(index >= this.tweens.length) return;
  138. var self = this;
  139. this.current = index;
  140. this.tweens[index].promise.then(function() {
  141. self.switch(index + 1);
  142. });
  143. };
  144. ComposedTween.prototype.update = function(dt) {
  145. if(this.current >= this.tweens.length) return;
  146. var active = this.tweens[this.current];
  147. active.update(dt);
  148. this.value = active.value;
  149. };
  150. function DeferredTween(start) {
  151. this.lastTarget = start;
  152. this.tweens = [];
  153. }
  154. DeferredTween.prototype = new BaseTween();
  155. DeferredTween.prototype.addTween = function(tween) {
  156. this.tweens.push(tween);
  157. this.tween = new ComposedTween(this.tweens);
  158. this.promise = this.tween.promise;
  159. return this;
  160. }
  161. DeferredTween.prototype.to = function(target, duration, easing) {
  162. easing = easing || easeLinear;
  163. this.addTween(new SimpleTween(duration, this.lastTarget, target, easing));
  164. this.lastTarget = target;
  165. return this;
  166. };
  167. DeferredTween.prototype.wait = function(duration) {
  168. return this.to(this.lastTarget, duration);
  169. };
  170. DeferredTween.prototype.waitFor = function(promise) {
  171. return this.addTween(new PromiseTween(this.lastTarget, promise));
  172. };
  173. DeferredTween.prototype.update = function(dt) {
  174. this.tween.update(dt);
  175. this.value = this.tween.value;
  176. };