safari.js 13 KB

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