summaryrefslogtreecommitdiff
path: root/levelSelect.js
blob: b1f60730cee2ba369622e835d147d37039add7c3 (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
import{ setupLevel, setupLevelUi } from './level.js';

const levels = {
  'Initial Jump': [ [], setupLevel([], [8, 0]) ],
  'Monopole Mining': [ ['Initial Jump'], setupLevel([[3, 3]], [8,8]) ],
};

export function setupLevelSelectUi(root, audio) {
  const buttonEnterAudio = document.getElementById('sfx-buttonenter');
  const buttonClickAudio = document.getElementById('sfx-buttonclick');
  audio.createMediaElementSource(buttonEnterAudio).connect(audio.destination);
  audio.createMediaElementSource(buttonClickAudio).connect(audio.destination);

  const levelList = document.createElement('ol');
  const levelPicker = name => {
    const [ dependencies, level ] = levels[name];
    const allDependenciesSatisfied = dependencies.map(x => levels[x][1].completed).reduce((acc, x) => acc && x, true);
    if (allDependenciesSatisfied) {
      const button = document.createElement('input');
      button.type = 'button';
      button.value = name;
      button.onmouseenter = () => {
        buttonEnterAudio.load();
        buttonEnterAudio.play();
      }
      button.onclick = () => {
        buttonClickAudio.load();
        buttonClickAudio.play();
        root.innerText = '';
        setupLevelUi(level, root, audio);
      }
      const li = document.createElement('li');
      li.appendChild(button);
      levelList.appendChild(li);
    }
  }

  [...Object.keys(levels)].forEach(levelPicker);
  root.appendChild(levelList);
}