23 lines
663 B
TypeScript
Raw Normal View History

2022-12-07 10:47:46 +01:00
import { sys } from "cc";
import { UserData } from "../Game/Data/UserData";
export class SaveSystem {
2022-12-19 13:17:32 +01:00
private userDataIdentifier = "user-d";
2022-12-07 10:47:46 +01: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 09:17:11 +01:00
// TODO: the data can be corrupted if we introduce a new field in UserData
2022-12-07 10:47:46 +01:00
return <UserData>JSON.parse(data);
} catch (error) {
return new UserData();
}
}
}