util.js 5.8 KB

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