summaryrefslogtreecommitdiff
path: root/resources/js/story.js
blob: 1901829ae1768a12fc9b7f575540a8e6877d0313 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const Story = {
  async start () {
    const settingsPopup = UI.createPopup();
    settingsPopup.querySelector('[data-template-slot="content"]').append(UI.createSettingsMenu());
    UI.drawPopup(settingsPopup);

    await new Promise((resolve) => {
      settingsPopup.addEventListener('close', UI.wrapCallback(async () => {
        resolve();
      }));
    });

    await Story.immediateProgress('start', 'introduction');
  },

  async introduction () {
    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('omnichannel-ceo'), text: translate('spyder_intro00', true) });
    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('omnichannel-ceo'), text: translate('spyder_intro01', true) });
    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('tuxemart-keeper'), text: translate('spyder_intro_shopkeeper1', true) });

    const possibleStarterMonsters = await Promise.all(
      [
        'budaye',
        'dollfin',
        'grintot',
        'ignibus',
        'memnomnom',
      ].map(async (monsterSlug) => {
          const monster = await fetchMonster(monsterSlug);
          monster.level = 5;
          return monster;
      })
    );

    const monsterSelection = UI.openStarterMonsterSelection(possibleStarterMonsters, { title: translate('story:introduction:monster_selection:title', true) });
    await new Promise((resolve) => {
      monsterSelection.addEventListener('starter:monster:selected', UI.wrapCallback(async (event) => {
        if (!confirm(`Select ${event.detail.monster.name}?`)) {
          return;
        }

        event.detail.popup.remove();

        await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('tuxemart-keeper'), text: translate('spyder_intro_shopkeeper4', true) });

        // set rival monster
        Memory.state.rivalMonster = event.detail.monster.slug;

        // set initial values
        Memory.state.money = 250;

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

        resolve();
      }));
    });
  },

  async selectStarterMonster () {
    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('spyder_dante'), text: translate('spyder_papertown_myfirstmon_notmet', true) });
    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('spyder_dante'), text: translate('spyder_papertown_myfirstmon1', true) });
    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('spyder_dante'), text: translate('spyder_papertown_myfirstmon2', true) });

    const possibleStarterMonsters = await Promise.all(
      [
        'tweesher',
        'lambert',
        'nut',
        'agnite',
        'rockitten',
      ].map(async (monsterSlug) => {
          const monster = await fetchMonster(monsterSlug);
          monster.level = 5;
          return monster;
      })
    );

    const monsterSelection = UI.openStarterMonsterSelection(possibleStarterMonsters, { title: translate('story:select_starter_monster:monster_selection:title', true) });
    await new Promise((resolve) => {
      monsterSelection.addEventListener('starter:monster:selected', UI.wrapCallback(async (event) => {
        if (!confirm(`Select ${event.detail.monster.name}?`)) {
          return;
        }

        Memory.state.player.monsters.push(event.detail.monster);
        Game.setActivePlayerMonster(event.detail.monster);

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

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

        event.detail.popup.remove();

        resolve();
      }));
    });

    await Story.immediateProgress('selectStarterMonster', 'battleRivalOne');
  },

  async battleRivalOne () {
    const rivalMonster = await fetchMonster(Memory.state.rivalMonster);
    rivalMonster.level = 5;
    Memory.state.opponent = new Trainer({ monsters: [ rivalMonster ] });
    await Memory.state.opponent.initialize();

    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('spyder_billie'), text: translate('spyder_papertown_firstfight', true) });

    await Story.battle();

    if (Game.didWinStoryBattle) {
      await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('spyder_billie'), text: translate('spyder_papertown_firstfight_win', true) });
    } else {
      await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('spyder_billie'), text: translate('spyder_papertown_firstfight_lose', true) });
    }

    await UI.buildAndShowStoryPopup({ speaker: await fetchNpc('spyder_billie'), text: translate('spyder_papertown_firstfight_after', true) });
    Game.healParty();
  },


  // Helper

  /**
   * @param {string} slug
   */
  async progress (slug) {
    if (!Story[slug]) {
      return;
    }

    Memory.state.currentStory = slug;
    Memory.saveToLocalStorage();

    await Story[slug]();

    Memory.state.storyProgress[slug] = true;
    Memory.state.currentStory = null;
    Memory.saveToLocalStorage();
  },

  /**
   * @param {string} fromSlug
   * @param {string} toSlug
   */
  async immediateProgress (fromSlug, toSlug) {
    Memory.state.storyProgress[fromSlug] = true;
    Memory.saveToLocalStorage();

    await Story.progress(toSlug);
  },

  async battle () {
    const previousArea = Object.assign({}, Memory.state.currentArea);

    Game.isStoryBattle = true;
    Memory.state.Game.isInBattle = true;
    Memory.saveToLocalStorage();

    UI.drawBattle();
    UI.showBattle();

    await new Promise((resolve) => {
      const interval = setInterval(() => {
        if (!Game.isStoryBattle) {
          clearInterval(interval);
          resolve();
        }
      }, 100);
    });

    // reset incremented trainer progress
    if (Memory.state.areaProgress[previousArea.slug]) {
      Memory.state.areaProgress[previousArea.slug].trainerProgress = previousArea.trainerProgress;
    }
    if (previousArea.slug === Memory.state.currentArea.slug) {
      Memory.state.currentArea.trainerProgress = previousArea.trainerProgress;

      UI.drawStatus();
    }
  },
};