util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // Ticker
  28. var Ticker = {};
  29. (function(Ticker) {
  30. var listeners = [];
  31. Ticker.addListener = function(listener) {
  32. listeners.push(listener);
  33. }
  34. Ticker.removeListener = function(listener) {
  35. var index = listeners.indexOf(listener);
  36. listeners.splice(index, 1);
  37. }
  38. var nextFrame =
  39. window.requestAnimationFrame ||
  40. window.webkitRequestAnimationFrame ||
  41. window.mozRequestAnimationFrame ||
  42. function (callback) {
  43. setTimeout(function () {
  44. callback(Date.now())
  45. }, 10)
  46. };
  47. var then = Date.now();
  48. function tick(now) {
  49. var delta = (now - then) / 1000;
  50. listeners.forEach(function(listener){
  51. listener(delta);
  52. })
  53. then = now;
  54. nextFrame(tick);
  55. }
  56. Ticker.start = function() {
  57. nextFrame(tick);
  58. }
  59. })(Ticker);
  60. // Tweens
  61. function BaseTween() {}
  62. BaseTween.prototype.animate = function() {
  63. var self = this;
  64. function tick(dt) {
  65. self.update(dt);
  66. }
  67. Ticker.addListener(tick);
  68. self.promise.then(function() {
  69. Ticker.removeListener(tick);
  70. });
  71. self.update(0);
  72. return self;
  73. };
  74. function Tween(duration, start, end, easing) {
  75. this.duration = duration;
  76. this.start = start;
  77. this.end = end;
  78. this.easing = easing || easeLinear;
  79. this.deferred = Q.defer();
  80. this.timer = 0;
  81. this.promise = this.deferred.promise;
  82. };
  83. Tween.prototype = new BaseTween();
  84. Tween.prototype.update = function(dt) {
  85. this.timer += dt;
  86. if(this.timer >= this.duration) {
  87. this.timer = this.duration;
  88. this.deferred.resolve();
  89. }
  90. this.value = this.easing(this.timer, this.start, this.end, this.duration);
  91. };
  92. function PromiseTween(value, promise) {
  93. this.value = value;
  94. this.promise = promise;
  95. }
  96. PromiseTween.prototype.update = function() {};
  97. function ComposedTween(tweens) {
  98. this.tweens = tweens;
  99. this.promise = Q.all(this.tweens.map(function(t) { return t.promise }));
  100. this.switch(0);
  101. }
  102. ComposedTween.prototype = new BaseTween();
  103. ComposedTween.prototype.switch = function(index) {
  104. if(index >= this.tweens.length) return;
  105. var self = this;
  106. this.current = index;
  107. this.tweens[index].promise.then(function() {
  108. self.switch(index + 1);
  109. });
  110. };
  111. ComposedTween.prototype.update = function(dt) {
  112. if(this.current >= this.tweens.length) return;
  113. var active = this.tweens[this.current];
  114. active.update(dt);
  115. this.value = active.value;
  116. };
  117. function DeferredTween(start) {
  118. this.lastTarget = start;
  119. this.tweens = [];
  120. }
  121. DeferredTween.prototype = new BaseTween();
  122. DeferredTween.prototype.addTween = function(tween) {
  123. this.tweens.push(tween);
  124. this.tween = new ComposedTween(this.tweens);
  125. this.promise = this.tween.promise;
  126. return this;
  127. }
  128. DeferredTween.prototype.to = function(target, duration, easing) {
  129. easing = easing || easeLinear;
  130. this.addTween(new Tween(duration, this.lastTarget, target, easing));
  131. this.lastTarget = target;
  132. return this;
  133. };
  134. DeferredTween.prototype.wait = function(duration) {
  135. return this.to(this.lastTarget, duration);
  136. };
  137. DeferredTween.prototype.waitFor = function(promise) {
  138. return this.addTween(new PromiseTween(this.lastTarget, promise));
  139. };
  140. DeferredTween.prototype.update = function(dt) {
  141. this.tween.update(dt);
  142. this.value = this.tween.value;
  143. };
  144. Tween.from = function(start) {
  145. return new DeferredTween(start);
  146. };