safari.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. function SoundSafari(beatInfo) {
  2. var canvas = Game.canvas;
  3. var ctx = Game.context;
  4. // Constants
  5. var RADIUS = 8;
  6. var LEAF_SIZE = 8;
  7. var LEVEL_SIZE = 800;
  8. var ALLOWANCE = (LEVEL_SIZE - canvas.width)/2;
  9. var METER_WIDTH = 120;
  10. var METER_HEIGHT = 40;
  11. var METER_SPEED = 10;
  12. var PLAYER_SPEED = 50;
  13. var STAGE_SPEED = 25;
  14. var RIPPLE_SIZE = 10;
  15. var RIPPLE_SPEED = 10;
  16. var DOT_SIZE = 3;
  17. var DOT_DISTANCE = 4 * 5;
  18. var starColors = [
  19. 'maroon',
  20. 'red',
  21. 'orange',
  22. 'yellow',
  23. 'olive',
  24. 'green',
  25. 'teal',
  26. 'blue',
  27. 'navy',
  28. 'purple'
  29. ];
  30. var debug = false;
  31. var state = StateMachine.create({
  32. initial: 'loading',
  33. events: [
  34. {name: 'ready', from: 'loading', to: 'waiting'},
  35. {name: 'play', from: 'waiting', to: 'playing'},
  36. {name: 'point', from: 'playing', to: 'waiting'}
  37. ],
  38. callbacks: {
  39. onready: function() {
  40. reset();
  41. },
  42. onpoint: function() {
  43. ++points;
  44. soundManager.play('get');
  45. switchBeat(points % beats.length);
  46. }
  47. }
  48. });
  49. var gameTime = 0;
  50. var beats = [];
  51. var currentBeats = [];
  52. // Drawing functions
  53. {
  54. function drawLeafV(point, h, w) {
  55. ctx.beginPath();
  56. ctx.moveTo(point.x, point.y);
  57. ctx.quadraticCurveTo(point.x + w, point.y + h/2, point.x, point.y + h);
  58. ctx.quadraticCurveTo(point.x - w, point.y + h/2, point.x, point.y);
  59. ctx.fill();
  60. }
  61. function drawLeafH(point, w, h) {
  62. ctx.beginPath();
  63. ctx.moveTo(point.x, point.y);
  64. ctx.quadraticCurveTo(point.x + w/2, point.y + h, point.x + w, point.y);
  65. ctx.quadraticCurveTo(point.x + w/2, point.y - h, point.x, point.y);
  66. ctx.fill();
  67. }
  68. function drawPlayer(point) {
  69. drawCircle(point, RADIUS, 5, "#FFFFFF");
  70. }
  71. function drawSpectrum() {
  72. var barHeight = 30;
  73. var barWidth = 6;
  74. var barSpacing = 2;
  75. var margin = 5;
  76. ctx.strokeStyle = 'white';
  77. ctx.lineWidth = 2;
  78. ctx.beginPath();
  79. ctx.rect(
  80. margin - ctx.lineWidth,
  81. canvas.height - barHeight - ctx.lineWidth - margin,
  82. activeTarget.spectrum.length * (barWidth+barSpacing) + 2 * ctx.lineWidth - barSpacing,
  83. barHeight + 2 * ctx.lineWidth
  84. );
  85. ctx.stroke();
  86. for(var i = 0; i < activeTarget.spectrum.length; ++i) {
  87. var height = activeTarget.spectrum[i]*20;
  88. ctx.fillStyle = starColors[i];
  89. ctx.fillRect(i * (barWidth + barSpacing) + margin, canvas.height - height - margin, barWidth, height);
  90. }
  91. }
  92. }
  93. // Helpers
  94. {
  95. function distX(a, b) {
  96. var dx = a.x - b.x;
  97. if(dx > LEVEL_SIZE / 2) dx -= LEVEL_SIZE;
  98. if(dx < -LEVEL_SIZE / 2) dx += LEVEL_SIZE;
  99. return dx;
  100. }
  101. function distSq(a, b) {
  102. var c = distX(a, b);
  103. var d = a.y - b.y;
  104. return c * c + d * d;
  105. }
  106. function colliding(a, b) {
  107. return distSq(a, b) <= 4 * RADIUS * RADIUS;
  108. }
  109. function translatePoints(point) {
  110. var x = point.x - player.x + canvas.width/2;
  111. if(x < -ALLOWANCE) x += LEVEL_SIZE;
  112. if(x > LEVEL_SIZE - ALLOWANCE) x -= LEVEL_SIZE;
  113. return {x:x, y:point.y};
  114. }
  115. function nextPlayTime(duration) {
  116. return Math.ceil(gameTime / duration) * duration;
  117. }
  118. }
  119. // "Entities"
  120. {
  121. function Meter() {
  122. this.offset = 10;
  123. this.amp = 0;
  124. this.percentage = 0;
  125. this.direction = METER_SPEED;
  126. }
  127. Meter.prototype.draw = function() {
  128. var left = (canvas.width - METER_WIDTH) / 2;
  129. var vmid = this.offset + METER_HEIGHT / 2;
  130. var third = METER_WIDTH / 3;
  131. var peak = METER_HEIGHT / 2;
  132. ctx.fillStyle = 'black';
  133. ctx.rect(left, this.offset, METER_WIDTH, METER_HEIGHT);
  134. ctx.fill();
  135. var cpUp = lerp(vmid, vmid + peak * this.amp, this.percentage);
  136. var cpDown = lerp(vmid, vmid - peak * this.amp, this.percentage);
  137. ctx.lineWidth = 2;
  138. ctx.strokeStyle = 'red';
  139. ctx.beginPath();
  140. var pos = left;
  141. ctx.moveTo(left, vmid);
  142. ctx.quadraticCurveTo(pos+third/2, cpUp, pos+third, vmid);
  143. pos += third;
  144. ctx.quadraticCurveTo(pos+third/2, cpDown, pos+third, vmid);
  145. pos += third;
  146. ctx.quadraticCurveTo(pos+third/2, cpUp, pos+third, vmid);
  147. ctx.stroke();
  148. ctx.lineWidth = 3;
  149. ctx.strokeStyle = 'white';
  150. ctx.beginPath();
  151. ctx.rect(left, this.offset, METER_WIDTH, METER_HEIGHT);
  152. ctx.stroke();
  153. };
  154. Meter.prototype.onUpdate = function(dt) {
  155. if(this.percentage > 1) {
  156. this.direction = -METER_SPEED;
  157. }
  158. if(this.percentage < -1) {
  159. this.direction = METER_SPEED;
  160. }
  161. this.percentage += this.direction * dt;
  162. this.amp = 0.5 + lerp(0, 1, activeTarget.shimmerFactor);
  163. };
  164. function Star(x, y) {
  165. this.x = x;
  166. this.y = y;
  167. this.alpha = 0;
  168. this.randomize();
  169. }
  170. Star.prototype.randomize = function() {
  171. this.type = Math.floor(Math.random()*starColors.length);
  172. this.color = starColors[this.type];
  173. };
  174. Star.prototype.draw = function() {
  175. ctx.fillStyle = this.color;
  176. ctx.globalAlpha = this.alpha;
  177. var adjPosition = translatePoints(this);
  178. ctx.fillRect(adjPosition.x, adjPosition.y, DOT_SIZE, DOT_SIZE);
  179. var top = {x: adjPosition.x + DOT_SIZE/2, y: adjPosition.y - DOT_SIZE/2 };
  180. var bottom = {x: adjPosition.x + DOT_SIZE/2, y: adjPosition.y + DOT_SIZE/2*3};
  181. var left = {x: adjPosition.x - DOT_SIZE/2, y: adjPosition.y + DOT_SIZE/2 };
  182. var right = {x: adjPosition.x + DOT_SIZE/2*3, y: adjPosition.y + DOT_SIZE/2 };
  183. drawLeafH(left, -LEAF_SIZE, LEAF_SIZE/2);
  184. drawLeafH(right, LEAF_SIZE, LEAF_SIZE/2);
  185. drawLeafV(top, -LEAF_SIZE, LEAF_SIZE/2);
  186. drawLeafV(bottom, LEAF_SIZE, LEAF_SIZE/2);
  187. ctx.globalAlpha = 1;
  188. };
  189. Star.prototype.onUpdate = function() {
  190. if(this.y > canvas.height + DOT_DISTANCE * 2) {
  191. this.y -= canvas.height + DOT_DISTANCE * 4;
  192. this.randomize();
  193. }
  194. if(state.current == 'playing') {
  195. var newAlpha = 1.5 * activeTarget.spectrum[this.type];
  196. var a = 0.9;
  197. this.alpha = Math.min(1, activeTarget.shimmerFactor * (this.alpha * (1-a) + newAlpha * a));
  198. }
  199. else if(state.current == 'waiting') {
  200. this.alpha = this.alpha * 0.95;
  201. }
  202. };
  203. function Dot(x, y) {
  204. this.x = x;
  205. this.y = y;
  206. }
  207. Dot.prototype.draw = function() {
  208. ctx.fillStyle = 'gray';
  209. var adjPosition = translatePoints(this);
  210. ctx.fillRect(adjPosition.x, adjPosition.y, DOT_SIZE, DOT_SIZE);
  211. };
  212. Dot.prototype.onUpdate = function() {
  213. if(this.y > canvas.height + DOT_DISTANCE * 2) {
  214. this.y -= canvas.height + DOT_DISTANCE * 4;
  215. }
  216. };
  217. function Target(beat) {
  218. this.beat = beat;
  219. this.alpha = 0;
  220. this.spectrum = [];
  221. if(points < 4) {
  222. this.alpha = ease(1-points/4);
  223. }
  224. this.rippleCounter = 0;
  225. this.reset();
  226. }
  227. Target.prototype.reset = function() {
  228. this.y = -20;
  229. this.x = Math.random()*LEVEL_SIZE;
  230. };
  231. Target.prototype.color = function(p) {
  232. return color(0, 255, 0, p*this.alpha);
  233. };
  234. Target.prototype.draw = function() {
  235. var adjPosition = translatePoints(this);
  236. drawCircle(adjPosition, RADIUS, 3, this.color(1));
  237. drawCircle(adjPosition, 2, 3, this.color(1));
  238. // ripples
  239. var p = lerp(1, 0, ease(this.rippleCounter/RIPPLE_SIZE));
  240. drawCircle(adjPosition, RADIUS+7+this.rippleCounter, 1, this.color(p));
  241. drawCircle(adjPosition, RADIUS+4+this.rippleCounter, 1, this.color(p*0.8));
  242. drawCircle(adjPosition, RADIUS+1+this.rippleCounter, 1, this.color(p*0.5));
  243. };
  244. Target.prototype.onUpdate = function(dt) {
  245. this.rippleCounter += RIPPLE_SPEED * dt;
  246. if(this.rippleCounter > RIPPLE_SIZE) {
  247. this.rippleCounter = 0;
  248. }
  249. if(colliding(player, this)) {
  250. state.point();
  251. this.beat.sound.setPan(0);
  252. this.beat.sound.setVolume(100);
  253. this.shouldDelete = true;
  254. return;
  255. }
  256. if(this.y > canvas.height + 20) {
  257. this.reset();
  258. }
  259. var xDist = distX(this, player);
  260. var s = sign(xDist);
  261. var xDistAbs = easeOutExpo(Math.abs(xDist), 0, 100, LEVEL_SIZE/2);
  262. var yDist = this.y - player.y;
  263. var angle = 0;
  264. if(yDist != 0) {
  265. angle = easeOutExpo(Math.abs(Math.atan2(Math.abs(yDist), xDist) - Math.PI/2), 0, 100, Math.PI);
  266. }
  267. else {
  268. angle = s * 100;
  269. }
  270. this.beat.sound.setPan(Math.max(xDistAbs, angle) * s);
  271. var rDist = xDist*xDist + yDist*yDist;
  272. if(rDist >= 250000) {
  273. rDist = 250000;
  274. }
  275. rDist = 250000 - rDist;
  276. rDist /= 2500;
  277. this.beat.sound.setVolume(rDist);
  278. this.shimmerFactor = easeOutExpo(Math.abs(xDist), 1, -1, LEVEL_SIZE/2) * rDist / 100;
  279. if(isNaN(this.shimmerFactor) || this.shimmerFactor < 0) {
  280. this.shimmerFactor = 0;
  281. }
  282. var pos = gameTime % this.beat.info.duration;
  283. if(pos < this.beat.sound.duration) this.spectrum = this.beat.sound.getSpectrum(pos);
  284. };
  285. }
  286. var player = {
  287. x: 0,
  288. y: canvas.height - 20,
  289. dx: PLAYER_SPEED
  290. };
  291. var meter = new Meter();
  292. var points;
  293. var entities;
  294. var activeTarget;
  295. function switchBeat(index) {
  296. var currentBeat = beats[index];
  297. currentBeats.push(currentBeat);
  298. if(currentBeats.length > 4) {
  299. currentBeats.splice(0, 1);
  300. }
  301. activeTarget = new Target(currentBeat);
  302. entities.push(activeTarget);
  303. }
  304. function reset() {
  305. points = 0;
  306. entities = [];
  307. for(var j = 0; j < canvas.height / DOT_DISTANCE + 4; ++j) {
  308. for(var i = 0; i < LEVEL_SIZE / DOT_DISTANCE; ++i) {
  309. var x = i * DOT_DISTANCE;
  310. var y = (j-4) * DOT_DISTANCE;
  311. if(j % 2 == 0 && i % 2 == 0) {
  312. entities.push(new Dot(x, y));
  313. }
  314. if(j % 4 == 1) {
  315. if(i % 4 == 0) {
  316. entities.push(new Star(x, y));
  317. }
  318. else {
  319. entities.push(new Dot(x, y));
  320. }
  321. }
  322. if(j % 4 == 3) {
  323. if(i % 4 == 2) {
  324. entities.push(new Star(x, y));
  325. }
  326. else {
  327. entities.push(new Dot(x, y));
  328. }
  329. }
  330. }
  331. }
  332. switchBeat(0);
  333. }
  334. // Update
  335. this.update = function(dt) {
  336. if(state.current == 'loading') return;
  337. var delta = dt * 1000;
  338. currentBeats.map(function(beat) {
  339. if(gameTime + delta > nextPlayTime(beat.info.duration)) {
  340. if(beat == activeTarget.beat && state.current == 'waiting') {
  341. state.play();
  342. }
  343. beat.sound.play();
  344. }
  345. });
  346. if(37 in Game.keysDown) {
  347. player.x -= player.dx * dt;
  348. }
  349. if(39 in Game.keysDown) {
  350. player.x += player.dx * dt;
  351. }
  352. player.x = (player.x + LEVEL_SIZE) % LEVEL_SIZE;
  353. for(var i = entities.length - 1; i >= 0; --i) {
  354. var entity = entities[i];
  355. entity.y += STAGE_SPEED * dt;
  356. entity.onUpdate(dt);
  357. if(entity.shouldDelete) {
  358. entities.splice(i, 1);
  359. }
  360. }
  361. meter.onUpdate(dt);
  362. gameTime += delta;
  363. };
  364. this.draw = function() {
  365. if(state.current != 'loading') {
  366. ctx.fillStyle = "black";
  367. ctx.fillRect(0, 0, canvas.width, canvas.height);
  368. entities.map(function(elem) {
  369. elem.draw();
  370. });
  371. drawPlayer(translatePoints(player));
  372. meter.draw();
  373. ctx.fillStyle = "white";
  374. ctx.fillText(points+"", 5, 15);
  375. if(debug) {
  376. drawSpectrum();
  377. }
  378. }
  379. if(this.coverAlphaTween.promise.isPending()) {
  380. ctx.globalAlpha = this.coverAlphaTween.value;
  381. ctx.fillStyle = "black";
  382. ctx.fillRect(0, 0, canvas.width, canvas.height);
  383. ctx.globalAlpha = 1;
  384. }
  385. if(state.current == 'loading') {
  386. ctx.globalAlpha = this.textAlphaTween.value;
  387. ctx.fillStyle = 'white';
  388. ctx.textAlign = 'center';
  389. ctx.font = font(48);
  390. ctx.fillText("sound safari", canvas.width / 2, canvas.height / 2);
  391. ctx.font = font(12);
  392. ctx.textAlign = 'start';
  393. ctx.globalAlpha = 1;
  394. }
  395. };
  396. var onKeyUp = function(e) {
  397. switch(e.charCode) {
  398. case 80: // P
  399. case 112: // p
  400. case 32: // space
  401. Game.pause();
  402. soundManager.play('pause');
  403. break;
  404. case 68: // D
  405. debug = !debug;
  406. break;
  407. }
  408. };
  409. this.load = function() {
  410. addEventListener('keypress', onKeyUp);
  411. beats = [];
  412. var soundPromises = beatInfo.map(function(elem, index) {
  413. var beat = { info: elem, rounds: 0 };
  414. var promise = createSound('beat'+index, elem.url).then(function(sound) {
  415. beat.sound = sound;
  416. });
  417. beats.push(beat);
  418. return promise;
  419. });
  420. soundPromises.push(createSound('get', 'sound/get.mp3'));
  421. this.textAlphaTween = this.tween(0)
  422. .to(1, 1.5)
  423. .wait(1)
  424. .waitFor(Q.all(soundPromises))
  425. .to(0, 1.5)
  426. .animate();
  427. this.coverAlphaTween = this.tween(1)
  428. .waitFor(this.textAlphaTween.promise)
  429. .to(0, 1)
  430. .animate();
  431. this.textAlphaTween.promise.done(function() {
  432. state.ready();
  433. });
  434. };
  435. this.unload = function() {
  436. removeEventListener('keypress', onKeyUp);
  437. soundManager.destroySound('get');
  438. beats.forEach(function(beat) {beat.sound.destruct()});
  439. };
  440. this.pause = function() {
  441. removeEventListener('keypress', onKeyUp);
  442. currentBeats.map(function(beat) {beat.sound.pause()});
  443. };
  444. this.resume = function() {
  445. addEventListener('keypress', onKeyUp);
  446. currentBeats.map(function(beat) {beat.sound.resume()});
  447. };
  448. }
  449. SoundSafari.prototype = new Scene();