ветки и кусты
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
import type { Inventory, LogDrop, TreeState, Vec2 } from './types';
|
||||
import type { Inventory, ResourceDrop, ResourceNode, Vec2 } from './types';
|
||||
import type { UpgradeId } from './upgrades';
|
||||
|
||||
const STORAGE_KEY = 'forest-save-v1';
|
||||
|
||||
export interface SaveData {
|
||||
version: 1;
|
||||
version: 2;
|
||||
inventory: Inventory;
|
||||
upgradeLevels: Record<UpgradeId, number>;
|
||||
/** World state (character/trees/logs) is optional so older saves without it still load fine —
|
||||
/** World state (character/nodes/drops) is optional so older saves without it still load fine —
|
||||
* progress is restored either way, just with a freshly spawned world. */
|
||||
character?: Vec2;
|
||||
trees?: TreeState[];
|
||||
logDrops?: LogDrop[];
|
||||
nodes?: ResourceNode[];
|
||||
resourceDrops?: ResourceDrop[];
|
||||
/** Absent on older saves, which are treated as level 1. */
|
||||
level?: number;
|
||||
}
|
||||
@@ -21,15 +21,17 @@ function isValidSave(value: unknown): value is SaveData {
|
||||
return false;
|
||||
const data = value as Partial<SaveData>;
|
||||
return (
|
||||
data.version === 1
|
||||
data.version === 2
|
||||
&& typeof data.inventory === 'object' && data.inventory !== null
|
||||
&& typeof data.inventory.logs === 'number'
|
||||
&& typeof data.inventory.twigs === 'number'
|
||||
&& typeof data.inventory.coins === 'number'
|
||||
&& typeof data.upgradeLevels === 'object' && data.upgradeLevels !== null
|
||||
);
|
||||
}
|
||||
|
||||
/** Reads the saved game from localStorage, or null if there's none / it's corrupted. */
|
||||
/** Reads the saved game from localStorage, or null if there's none / it's corrupted (including a
|
||||
* save from an older, incompatible schema version — that's treated the same as no save). */
|
||||
export function loadSave(): SaveData | null {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
|
||||
Reference in New Issue
Block a user