Skip to content

Commit

Permalink
Saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
colecrouter committed Oct 28, 2024
1 parent 389e72d commit ef52c84
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/lib/SaveFile.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Player, Save } from "$types/save/1.6";

class SaveClass {
private filename = $state<string>();
private saveData = $state<Save>();

public players = $state<Player[]>([]);

constructor() {
// Update players array when save data changes
$effect(() => {
// Clear the players array when the save data changes
if (!this.saveData) {
this.players = [];
return;
}

const farmers =
this.saveData.farmhands.Farmer === undefined
? []
: Array.isArray(this.saveData.farmhands.Farmer)
? this.saveData.farmhands.Farmer
: [this.saveData.farmhands.Farmer];
const mainPlayer = this.saveData.player;

this.players = [mainPlayer, ...farmers];
});

// Update save data when the players array changes
$effect(() => {
if (!this.saveData) return;

this.saveData.player = this.players[0];
this.saveData.farmhands.Farmer = this.players.slice(1);
});
}
}

0 comments on commit ef52c84

Please sign in to comment.