summaryrefslogtreecommitdiff
path: root/main.js
blob: 107c580a699252565c8c9b046f4f4be064336a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { setupLevel, setupLevelUi } from './level.js';

window.onload = () => {

  const root = document.getElementById('root');

  const start = document.createElement('input');
  start.type = 'button';
  start.value = 'Start';
  start.onclick = () => {
    const audio = new AudioContext();
    root.removeChild(start);

    const rocketThrustAudio = document.getElementById('sfx-rocket-thrust');
    const rocketThrustSource = audio.createMediaElementSource(rocketThrustAudio);
    const rocketGain = audio.createGain();
    rocketThrustSource.connect(rocketGain).connect(audio.destination);
    
    const musicGain = audio.createGain();
    const addMusic = (music, id) => {
      const element = document.getElementById(id);
      const source = audio.createMediaElementSource(element);
      source.connect(musicGain).connect(audio.destination);
      return [ ...music, { element, source } ];
    }
    musicGain.gain.value = 0.5;
    const musicList = [
      'music-minute', 
      'music-starboard',
      'music-cribwhistling',
      'music-swish',
      'music-aeroplane',
    ].reduce(addMusic, []);
    musicList.forEach(
      ({ element }, i) => element.addEventListener(
        'ended', 
        () => musicList[(i+1) % musicList.length].element.play(),
      )
    )
    musicList[0].element.play();

    const level = setupLevel([[2, 2], [3,3]], [7, 7]);
    const ui = setupLevelUi(level, root, audio);
  };
  root.appendChild(start);
}