-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameStorage.js
More file actions
86 lines (77 loc) · 2.01 KB
/
gameStorage.js
File metadata and controls
86 lines (77 loc) · 2.01 KB
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
class GameStorage {
constructor() {
this.storageKey = "algoAgentGameData";
this.lastSaveTime = 0;
}
// Save all game data to localStorage
saveGame(data) {
try {
// Get current game state
const gameData = {
version: 1,
timestamp: Date.now(),
pet: {
health: data.pet.health,
energy: data.pet.energy,
happiness: data.pet.happiness,
lastFed: data.pet.lastFed,
},
currency: {
balance: data.currency.balance,
},
inventory: {
items: data.inventory.items
},
backgrounds: {
currentTheme: data.backgrounds.currentTheme
},
shop: {
backgroundItems: data.shop.backgroundItems
}
};
// Save to localStorage
localStorage.setItem(this.storageKey, JSON.stringify(gameData));
this.lastSaveTime = Date.now();
console.log("Game saved successfully!");
return true;
} catch (error) {
console.error("Failed to save game:", error);
return false;
}
}
// Load game data from localStorage
loadGame() {
try {
const savedData = localStorage.getItem(this.storageKey);
if (!savedData) {
console.log("No saved game found.");
return null;
}
const gameData = JSON.parse(savedData);
console.log("Game data loaded successfully!");
return gameData;
} catch (error) {
console.error("Failed to load game:", error);
return null;
}
}
// Check if a saved game exists
hasSavedGame() {
return localStorage.getItem(this.storageKey) !== null;
}
// Delete saved game data
clearSavedGame() {
try {
localStorage.removeItem(this.storageKey);
console.log("Saved game cleared successfully!");
return true;
} catch (error) {
console.error("Failed to clear saved game:", error);
return false;
}
}
// Get last save timestamp
getLastSaveTime() {
return this.lastSaveTime;
}
}