Skip to content

Commit

Permalink
game: move game save to game custom store
Browse files Browse the repository at this point in the history
  • Loading branch information
jnikula committed Mar 1, 2024
1 parent 794942f commit da6b159
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,11 @@
return `piste-on-piste-save-${slot}`;
}
function save_game(): void {
localStorage.setItem(save_game_name($game.save_game_slot), JSON.stringify($game.undo_stack));
}
function undo_stack_push(s: State, autosave: boolean = true): State {
$game.undo_stack.splice(++$game.undo_index, $game.undo_stack.length, s);
if (autosave)
save_game();
game.save();
return s;
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class Game {
constructor() {
}

save_game_name(): string {
return `piste-on-piste-save-${this.save_game_slot}`;
}

_save(): void {
localStorage.setItem(this.save_game_name(), JSON.stringify(this.undo_stack));
}

get can_undo(): boolean {
return this.undo_index > 0;
}
Expand Down Expand Up @@ -43,6 +51,7 @@ function create_game(_game: Game) {
subscribe,
undo: () => update((val) => { val._undo(); return val; }),
redo: () => update((val) => { val._redo(); return val; }),
save: () => update((val) => { val._save(); return val; }),
};
}

Expand Down

0 comments on commit da6b159

Please sign in to comment.