summaryrefslogtreecommitdiff
path: root/resources/js/main.js
blob: f472a14bec70aaba29b209a398c5c37e3fa9d90a (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
UI.wrapCallback(async function () {
  await initializeDB();

  // Load existing state
  if (localStorage.getItem('state')) {
    Memory.loadFromLocalStorage();
  }

  // Start New Game
  else {
    const possibleStarterMonsters = await Promise.all(
      [
        'budaye',
        'dollfin',
        'grintot',
        'ignibus',
        'memnomnom',
      ].map(async (monsterSlug) => await fetchMonster(monsterSlug))
    );

    const monsterSelection = UI.openStarterMonsterSelection(possibleStarterMonsters);
    monsterSelection.addEventListener('starter:monster:selected', UI.wrapCallback(async (event) => {
      if (!confirm(`Select ${event.detail.monster.name}?`)) {
        return;
      }

      Memory.state.player = new Trainer({
        monsters: [
          event.detail.monster,
        ],
        inventory: [
          new InventoryItem(await fetchItem('tuxeball'), 5),
          new InventoryItem(await fetchItem('potion')),
        ]
      });
      await Memory.state.player.initialize();

      Game.setActivePlayerMonster(Memory.state.player.monsters[0]);
      Memory.state.activeBall = 'tuxeball';

      // set rival monster
      possibleStarterMonsters.splice(possibleStarterMonsters.indexOf(event.detail.monster), 1);
      const rivalMonster = possibleStarterMonsters[Math.round(Math.random() * (possibleStarterMonsters.length - 1))];
      Memory.state.opponent = new Trainer({ monsters: [ rivalMonster ] });
      await Memory.state.opponent.initialize();
      Memory.state.rivalMonster = rivalMonster.slug

      // go to starting area
      await Game.goToArea('paper-town');

      UI.drawActiveMonster();
      UI.drawActiveTechniques();

      event.detail.popup.remove();
    }));
  }
})();