- Complete game architecture with modular JS structure - 12 buildings across 4 tiers with scaling costs - 15+ upgrades with requirements and bonuses - 20 achievements to unlock - Auto-save system with offline progress (50% efficiency) - Export/Import save functionality - Responsive dark-themed UI with cosmic aesthetic - Floating text animations and notification system
105 lines
2.6 KiB
JavaScript
105 lines
2.6 KiB
JavaScript
/**
|
|
* Cosmic Garden - Save System
|
|
* Handles localStorage persistence and export/import
|
|
*/
|
|
|
|
const SaveSystem = {
|
|
SAVE_KEY: 'cosmicGardenSave',
|
|
|
|
/**
|
|
* Save game to localStorage
|
|
*/
|
|
save() {
|
|
const data = JSON.stringify(Game.state);
|
|
localStorage.setItem(this.SAVE_KEY, data);
|
|
console.log('💾 Game saved');
|
|
},
|
|
|
|
/**
|
|
* Load game from localStorage
|
|
*/
|
|
load() {
|
|
const data = localStorage.getItem(this.SAVE_KEY);
|
|
if (data) {
|
|
try {
|
|
return JSON.parse(data);
|
|
} catch (e) {
|
|
console.error('Failed to parse save:', e);
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
|
|
/**
|
|
* Export save as base64 string
|
|
*/
|
|
exportSave() {
|
|
const data = btoa(JSON.stringify(Game.state));
|
|
navigator.clipboard.writeText(data).then(() => {
|
|
UI.showNotification('Save copied to clipboard!', 'success');
|
|
}).catch(() => {
|
|
// Fallback for older browsers
|
|
const textarea = document.createElement('textarea');
|
|
textarea.value = data;
|
|
document.body.appendChild(textarea);
|
|
textarea.select();
|
|
document.execCommand('copy');
|
|
document.body.removeChild(textarea);
|
|
UI.showNotification('Save copied to clipboard!', 'success');
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Import save from base64 string
|
|
*/
|
|
importSave(data) {
|
|
try {
|
|
const parsed = JSON.parse(atob(data));
|
|
|
|
// Validate save data
|
|
if (!parsed.stardust || !parsed.buildings) {
|
|
throw new Error('Invalid save format');
|
|
}
|
|
|
|
// Merge with existing state
|
|
Game.state = { ...Game.state, ...parsed };
|
|
Game.state.lastSaveTime = Date.now();
|
|
|
|
UI.update();
|
|
return true;
|
|
} catch (e) {
|
|
console.error('Failed to import save:', e);
|
|
UI.showNotification('Invalid save data!', 'warning');
|
|
return false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Clear save data
|
|
*/
|
|
clear() {
|
|
localStorage.removeItem(this.SAVE_KEY);
|
|
},
|
|
|
|
/**
|
|
* Get last save time
|
|
*/
|
|
getLastSaveTime() {
|
|
const data = this.load();
|
|
if (data && data.lastSaveTime) {
|
|
return new Date(data.lastSaveTime);
|
|
}
|
|
return null;
|
|
},
|
|
|
|
/**
|
|
* Check if auto-save is available
|
|
*/
|
|
hasAutoSave() {
|
|
return !!localStorage.getItem(this.SAVE_KEY);
|
|
}
|
|
};
|
|
|
|
window.SaveSystem = SaveSystem;
|