Slash-The-Hordes/assets/Scripts/AppRoot/SaveSystem.ts

23 lines
665 B
TypeScript
Raw Normal View History

2022-12-07 09:47:46 +00:00
import { sys } from "cc";
import { UserData } from "../Game/Data/UserData";
export class SaveSystem {
2022-12-22 13:05:31 +00:00
private userDataIdentifier = "user-dse";
2022-12-07 09:47:46 +00:00
public save(userData: UserData): void {
sys.localStorage.setItem(this.userDataIdentifier, JSON.stringify(userData));
}
public load(): UserData {
const data: string = sys.localStorage.getItem(this.userDataIdentifier);
if (!data) return new UserData();
try {
2022-12-14 08:17:11 +00:00
// TODO: the data can be corrupted if we introduce a new field in UserData
2022-12-07 09:47:46 +00:00
return <UserData>JSON.parse(data);
} catch (error) {
return new UserData();
}
}
}