diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-08-26 19:00:03 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-08-26 19:00:03 +0200 |
commit | b7db43abcaf8c9d9ccb4bcaff07c5416cb21ff62 (patch) | |
tree | 264f0e61fc2493c584519af218f4d28f3a58c55e /resources/js/main.js | |
parent | 7ffb5e01dcd87a29e3cb6a3d0bcef1b87bc94d44 (diff) |
auto-save to local storage
Diffstat (limited to 'resources/js/main.js')
-rw-r--r-- | resources/js/main.js | 88 |
1 files changed, 53 insertions, 35 deletions
diff --git a/resources/js/main.js b/resources/js/main.js index aa86423..662dff6 100644 --- a/resources/js/main.js +++ b/resources/js/main.js @@ -1,39 +1,57 @@ UI.createEventListener(async function () { await initializeDB(); - // Start Game - const possibleStarterMonsters = ['budaye', 'dollfin', 'grintot', 'ignibus', 'memnomnom']; - - const monsterSelection = UI.openStarterMonsterSelection( - await Promise.all(possibleStarterMonsters.map(async (monsterSlug) => await fetchMonster(monsterSlug))) - ); - monsterSelection.addEventListener('starter:monster:selected', UI.createEventListener(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 = Memory.state.player.inventory[0]; // tuxeball - - possibleStarterMonsters.splice(possibleStarterMonsters.indexOf(event.detail.monster), 1); - Memory.state.rivalMonster = possibleStarterMonsters[Math.round(Math.random() * (possibleStarterMonsters.length - 1))]; - - await Game.goToArea('paper-town'); - - UI.drawActiveMonster(); - UI.drawActiveTechniques(); - - event.detail.popup.remove(); - })); + // 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.createEventListener(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 = Memory.state.player.inventory[0]; // 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(); + })); + } })(); |