util.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 sign(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
  14. function lerp(from, to, p) {
  15. return to * p + from * (1 - p);
  16. }
  17. function ease(v) { return v * v * (3 - 2 * v); }
  18. function easeLinear(t, b, c, d) {
  19. return lerp(b, c, t/d);
  20. }
  21. function easeOutExpo(t, b, c, d) {
  22. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  23. }
  24. function color(r,g,b,a) {
  25. return 'rgba('+r+','+g+','+b+','+a.toFixed(5)+')';
  26. }
  27. function makeObservable(o) {
  28. var listeners = [];
  29. o.hasTicker = true;
  30. o.addListener = function(listener) {
  31. listeners.push(listener);
  32. };
  33. o.removeListener = function(listener) {
  34. var index = listeners.indexOf(listener);
  35. listeners.splice(index, 1);
  36. };
  37. o.notify = function(dt) {
  38. listeners.forEach(function(listener) {
  39. listener(dt);
  40. })
  41. };
  42. }
  43. // Ticker
  44. var Ticker = {};
  45. (function(Ticker) {
  46. makeObservable(Ticker);
  47. var nextFrame =
  48. window.requestAnimationFrame ||
  49. window.webkitRequestAnimationFrame ||
  50. window.mozRequestAnimationFrame ||
  51. function (callback) {
  52. setTimeout(function () {
  53. callback(Date.now())
  54. }, 10)
  55. };
  56. var then = Date.now();
  57. function tick(now) {
  58. var delta = (now - then) / 1000;
  59. Ticker.notify(delta);
  60. then = now;
  61. nextFrame(tick);
  62. }
  63. Ticker.start = function() {
  64. nextFrame(tick);
  65. }
  66. })(Ticker);
  67. // Tweens
  68. function BaseTween() {}
  69. BaseTween.prototype.animate = function(scene) {
  70. var self = this;
  71. function tick(dt) {
  72. self.update(dt);
  73. }
  74. var ticker = Ticker;
  75. if(this.ticker) ticker = this.ticker;
  76. ticker.addListener(tick);
  77. self.promise.then(function() {
  78. ticker.removeListener(tick);
  79. });
  80. self.update(0);
  81. return self;
  82. };
  83. function SimpleTween(duration, start, end, easing) {
  84. this.duration = duration;
  85. this.start = start;
  86. this.end = end;
  87. this.easing = easing || easeLinear;
  88. this.deferred = Q.defer();
  89. this.timer = 0;
  90. this.promise = this.deferred.promise;
  91. };
  92. SimpleTween.prototype = new BaseTween();
  93. SimpleTween.prototype.update = function(dt) {
  94. this.timer += dt;
  95. if(this.timer >= this.duration) {
  96. this.timer = this.duration;
  97. this.deferred.resolve();
  98. }
  99. this.value = this.easing(this.timer, this.start, this.end, this.duration);
  100. };
  101. function PromiseTween(value, promise) {
  102. this.value = value;
  103. this.promise = promise;
  104. }
  105. PromiseTween.prototype.update = function() {};
  106. function ComposedTween(tweens) {
  107. this.tweens = tweens;
  108. this.promise = Q.all(this.tweens.map(function(t) { return t.promise }));
  109. this.switch(0);
  110. }
  111. ComposedTween.prototype = new BaseTween();
  112. ComposedTween.prototype.switch = function(index) {
  113. if(index >= this.tweens.length) return;
  114. var self = this;
  115. this.current = index;
  116. this.tweens[index].promise.then(function() {
  117. self.switch(index + 1);
  118. });
  119. };
  120. ComposedTween.prototype.update = function(dt) {
  121. if(this.current >= this.tweens.length) return;
  122. var active = this.tweens[this.current];
  123. active.update(dt);
  124. this.value = active.value;
  125. };
  126. function DeferredTween(start) {
  127. this.lastTarget = start;
  128. this.tweens = [];
  129. }
  130. DeferredTween.prototype = new BaseTween();
  131. DeferredTween.prototype.addTween = function(tween) {
  132. this.tweens.push(tween);
  133. this.tween = new ComposedTween(this.tweens);
  134. this.promise = this.tween.promise;
  135. return this;
  136. }
  137. DeferredTween.prototype.to = function(target, duration, easing) {
  138. easing = easing || easeLinear;
  139. this.addTween(new SimpleTween(duration, this.lastTarget, target, easing));
  140. this.lastTarget = target;
  141. return this;
  142. };
  143. DeferredTween.prototype.wait = function(duration) {
  144. return this.to(this.lastTarget, duration);
  145. };
  146. DeferredTween.prototype.waitFor = function(promise) {
  147. return this.addTween(new PromiseTween(this.lastTarget, promise));
  148. };
  149. DeferredTween.prototype.update = function(dt) {
  150. this.tween.update(dt);
  151. this.value = this.tween.value;
  152. };