summaryrefslogtreecommitdiff
path: root/main.js
blob: e41a89afd7abac01b75c489b9b5ffded7bc888cd (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { setupLevelSelectUi } from './levelSelect.js';
import { setupLevel, setupLevelUi } from './level.js';

window.onload = () => setTimeout(() => {
  
  const root = document.getElementById('root');
  root.classList.add('center');

  const title = document.createElement('h1');
  title.innerText = 'GENERAL PRODUCTS';
  root.appendChild(title);

  const logo = document.createElement('h1');
  logo.classList.add('logo');
  logo.innerText = '※';
  root.appendChild(logo);

  const title2 = document.createElement('h2');
  title2.innerText = 'AUTOMATED MONOPOLE HARVESTER (AMH) HYPERSPACE NAVIGATIONAL SERVICES';
  root.appendChild(title2);

  const paragraph = document.createElement('p');
  paragraph.innerText = 'greetings, navigator! thank you for helping us to program the hyperspace engines of our automated monopole harvester (AMH) platforms. by doing so, you help ensure stable antimatter production throughout the eight worlds.';
  root.appendChild(paragraph);

  const start = document.createElement('input');
  start.type = 'button';
  start.value = 'connect to AMH';
  start.onclick = () => {
    root.classList.remove('center');
    root.innerText = '';
    const audio = new AudioContext();

    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-aeroplane',
      'music-minute', 
      'music-starboard',
      'music-swish',
      'music-cribwhistling',
    ].reduce(addMusic, []);
    musicList.forEach(
      ({ element }, i) => element.addEventListener(
        'ended', 
        () => musicList[(i+1) % musicList.length].element.play(),
      )
    )
    musicList[0].element.play();

    const sfx = {};
    sfx.buttonEnterAudio = document.getElementById('sfx-buttonenter');
    sfx.buttonClickAudio = document.getElementById('sfx-buttonclick');
    sfx.listAppearAudio = document.getElementById('sfx-listappear');
    audio.createMediaElementSource(sfx.buttonEnterAudio).connect(audio.destination);
    audio.createMediaElementSource(sfx.buttonClickAudio).connect(audio.destination);
    audio.createMediaElementSource(sfx.listAppearAudio).connect(audio.destination);

    sfx.resourceAudio = document.getElementById('sfx-resource');
    audio.createMediaElementSource(sfx.resourceAudio).connect(audio.destination);
  
    sfx.doneAudio = document.getElementById('sfx-done');
    audio.createMediaElementSource(sfx.doneAudio).connect(audio.destination);
  
    sfx.wrongAudio = document.getElementById('sfx-wrong');
    audio.createMediaElementSource(sfx.wrongAudio).connect(audio.destination);

    // const level = setupLevel([[2, 2], [3,3]], [7, 7]);
    // const ui = setupLevelUi(level, root, audio);

    Promise.all([...Object.values(sfx)].map(x => x.load())).then(
      () => setupLevelSelectUi(root, sfx)
    );
  };
  root.appendChild(start);
}, 200);