safari.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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.spectrum = [];
  220. this.alpha = 0;
  221. if(points < 4) {
  222. this.alpha = ease(1-points/4);
  223. }
  224. this.init(0, 0, color(0, 255, 0, this.alpha));
  225. this.reset();
  226. }
  227. Target.prototype = new Beater();
  228. Target.prototype.reset = function() {
  229. this.y = -20;
  230. this.x = Math.random()*LEVEL_SIZE;
  231. };
  232. Target.prototype.draw = function() {
  233. this.position = translatePoints(this);
  234. Beater.prototype.draw.call(this);
  235. };
  236. Target.prototype.onUpdate = function(dt) {
  237. this.update(dt);
  238. if(colliding(player, this)) {
  239. state.point();
  240. this.beat.sound.setPan(0);
  241. this.beat.sound.setVolume(100);
  242. this.shouldDelete = true;
  243. return;
  244. }
  245. if(this.y > canvas.height + 20) {
  246. this.reset();
  247. }
  248. var xDist = distX(this, player);
  249. var s = sign(xDist);
  250. var xDistAbs = easeOutExpo(Math.abs(xDist), 0, 100, LEVEL_SIZE/2);
  251. var yDist = this.y - player.y;
  252. var angle = 0;
  253. if(yDist != 0) {
  254. angle = easeOutExpo(Math.abs(Math.atan2(Math.abs(yDist), xDist) - Math.PI/2), 0, 100, Math.PI);
  255. }
  256. else {
  257. angle = s * 100;
  258. }
  259. this.beat.sound.setPan(Math.max(xDistAbs, angle) * s);
  260. var rDist = xDist*xDist + yDist*yDist;
  261. if(rDist >= 250000) {
  262. rDist = 250000;
  263. }
  264. rDist = 250000 - rDist;
  265. rDist /= 2500;
  266. this.beat.sound.setVolume(rDist);
  267. this.shimmerFactor = easeOutExpo(Math.abs(xDist), 1, -1, LEVEL_SIZE/2) * rDist / 100;
  268. if(isNaN(this.shimmerFactor) || this.shimmerFactor < 0) {
  269. this.shimmerFactor = 0;
  270. }
  271. var pos = gameTime % this.beat.info.duration;
  272. if(pos < this.beat.sound.duration) this.spectrum = this.beat.sound.getSpectrum(pos);
  273. };
  274. }
  275. var player = {
  276. x: 0,
  277. y: canvas.height - 20,
  278. dx: PLAYER_SPEED
  279. };
  280. var meter = new Meter();
  281. var points;
  282. var entities;
  283. var activeTarget;
  284. function switchBeat(index) {
  285. var currentBeat = beats[index];
  286. currentBeats.push(currentBeat);
  287. if(currentBeats.length > 4) {
  288. currentBeats.splice(0, 1);
  289. }
  290. activeTarget = new Target(currentBeat);
  291. entities.push(activeTarget);
  292. }
  293. function reset() {
  294. points = 0;
  295. entities = [];
  296. for(var j = 0; j < canvas.height / DOT_DISTANCE + 4; ++j) {
  297. for(var i = 0; i < LEVEL_SIZE / DOT_DISTANCE; ++i) {
  298. var x = i * DOT_DISTANCE;
  299. var y = (j-4) * DOT_DISTANCE;
  300. if(j % 2 == 0 && i % 2 == 0) {
  301. entities.push(new Dot(x, y));
  302. }
  303. if(j % 4 == 1) {
  304. if(i % 4 == 0) {
  305. entities.push(new Star(x, y));
  306. }
  307. else {
  308. entities.push(new Dot(x, y));
  309. }
  310. }
  311. if(j % 4 == 3) {
  312. if(i % 4 == 2) {
  313. entities.push(new Star(x, y));
  314. }
  315. else {
  316. entities.push(new Dot(x, y));
  317. }
  318. }
  319. }
  320. }
  321. switchBeat(0);
  322. }
  323. // Update
  324. this.update = function(dt) {
  325. if(state.current == 'loading') return;
  326. var delta = dt * 1000;
  327. currentBeats.map(function(beat) {
  328. if(gameTime + delta > nextPlayTime(beat.info.duration)) {
  329. if(beat == activeTarget.beat && state.current == 'waiting') {
  330. state.play();
  331. }
  332. beat.sound.play();
  333. }
  334. });
  335. if(37 in Game.keysDown) {
  336. player.x -= player.dx * dt;
  337. }
  338. if(39 in Game.keysDown) {
  339. player.x += player.dx * dt;
  340. }
  341. player.x = (player.x + LEVEL_SIZE) % LEVEL_SIZE;
  342. for(var i = entities.length - 1; i >= 0; --i) {
  343. var entity = entities[i];
  344. entity.y += STAGE_SPEED * dt;
  345. entity.onUpdate(dt);
  346. if(entity.shouldDelete) {
  347. entities.splice(i, 1);
  348. }
  349. }
  350. meter.onUpdate(dt);
  351. gameTime += delta;
  352. };
  353. this.draw = function() {
  354. if(state.current != 'loading') {
  355. ctx.fillStyle = "black";
  356. ctx.fillRect(0, 0, canvas.width, canvas.height);
  357. entities.map(function(elem) {
  358. elem.draw();
  359. });
  360. drawPlayer(translatePoints(player));
  361. meter.draw();
  362. ctx.fillStyle = "white";
  363. ctx.fillText(points+"", 5, 15);
  364. if(debug) {
  365. drawSpectrum();
  366. }
  367. }
  368. if(this.coverAlphaTween.promise.isPending()) {
  369. ctx.globalAlpha = this.coverAlphaTween.value;
  370. ctx.fillStyle = "black";
  371. ctx.fillRect(0, 0, canvas.width, canvas.height);
  372. ctx.globalAlpha = 1;
  373. }
  374. if(state.current == 'loading') {
  375. ctx.globalAlpha = this.textAlphaTween.value;
  376. ctx.fillStyle = 'white';
  377. ctx.textAlign = 'center';
  378. ctx.font = font(48);
  379. ctx.fillText("sound safari", canvas.width / 2, canvas.height / 2);
  380. ctx.font = font(12);
  381. ctx.textAlign = 'start';
  382. ctx.globalAlpha = 1;
  383. }
  384. };
  385. var onKeyUp = function(e) {
  386. switch(e.charCode) {
  387. case 80: // P
  388. case 112: // p
  389. case 32: // space
  390. Game.pause();
  391. soundManager.play('pause');
  392. break;
  393. case 68: // D
  394. debug = !debug;
  395. break;
  396. }
  397. };
  398. this.load = function() {
  399. addEventListener('keypress', onKeyUp);
  400. beats = [];
  401. var soundPromises = beatInfo.map(function(elem, index) {
  402. var beat = { info: elem, rounds: 0 };
  403. var promise = createSound('beat'+index, elem.url).then(function(sound) {
  404. beat.sound = sound;
  405. });
  406. beats.push(beat);
  407. return promise;
  408. });
  409. soundPromises.push(createSound('get', 'sound/get.mp3'));
  410. this.textAlphaTween = this.tween(0)
  411. .wait(0.5)
  412. .to(1, 1.5)
  413. .wait(1)
  414. .waitFor(Q.all(soundPromises))
  415. .to(0, 1.5)
  416. .animate();
  417. this.coverAlphaTween = this.tween(1)
  418. .waitFor(this.textAlphaTween.promise)
  419. .to(0, 1)
  420. .animate();
  421. this.textAlphaTween.promise.done(function() {
  422. state.ready();
  423. });
  424. };
  425. this.unload = function() {
  426. removeEventListener('keypress', onKeyUp);
  427. soundManager.destroySound('get');
  428. beats.forEach(function(beat) {beat.sound.destruct()});
  429. };
  430. this.pause = function() {
  431. removeEventListener('keypress', onKeyUp);
  432. currentBeats.map(function(beat) {beat.sound.pause()});
  433. };
  434. this.resume = function() {
  435. addEventListener('keypress', onKeyUp);
  436. currentBeats.map(function(beat) {beat.sound.resume()});
  437. };
  438. }
  439. SoundSafari.prototype = new Scene();