safari.js 13 KB

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