ветки и кусты

This commit is contained in:
azykov@mail.ru
2026-09-04 17:18:31 +03:00
parent e872867c5e
commit 03a7552b9a
12 changed files with 482 additions and 224 deletions
+9 -7
View File
@@ -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);