fix runtime map loading lifecycle
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { MapNode, SceneData } from "@/types/editor";
|
||||
import type { SceneData } from "@/types/editor";
|
||||
import { parseMapNodes } from "@/utils/mapNodeValidation";
|
||||
|
||||
const MAP_JSON_PATH = "/map.json";
|
||||
|
||||
@@ -16,7 +17,7 @@ export async function createSceneDataFromFiles(
|
||||
throw new Error("Fichier map.json manquant à la racine du dossier");
|
||||
}
|
||||
|
||||
const mapNodes: MapNode[] = JSON.parse(await mapFile.text());
|
||||
const mapNodes = parseMapNodes(JSON.parse(await mapFile.text()));
|
||||
const models = new Map<string, string>();
|
||||
|
||||
for (const [path, file] of fileMap.entries()) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { MapNode, SceneData } from "@/types/editor";
|
||||
import { parseMapNodes } from "@/utils/mapNodeValidation";
|
||||
|
||||
const MAP_JSON_PATH = "/map.json";
|
||||
const MODEL_FILE_NAME = "model.gltf";
|
||||
@@ -11,7 +12,7 @@ export async function loadMapSceneData(): Promise<SceneData | null> {
|
||||
return null;
|
||||
}
|
||||
|
||||
const mapNodes: MapNode[] = await response.json();
|
||||
const mapNodes = parseMapNodes(await response.json());
|
||||
return createSceneData(mapNodes);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { MapNode } from "../types/editor";
|
||||
|
||||
function isVector3Tuple(value: unknown): value is [number, number, number] {
|
||||
return (
|
||||
Array.isArray(value) &&
|
||||
value.length === 3 &&
|
||||
value.every((item) => typeof item === "number" && Number.isFinite(item))
|
||||
);
|
||||
}
|
||||
|
||||
export function isMapNode(value: unknown): value is MapNode {
|
||||
if (typeof value !== "object" || value === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const node = value as Record<string, unknown>;
|
||||
return (
|
||||
typeof node.name === "string" &&
|
||||
typeof node.type === "string" &&
|
||||
isVector3Tuple(node.position) &&
|
||||
isVector3Tuple(node.rotation) &&
|
||||
isVector3Tuple(node.scale)
|
||||
);
|
||||
}
|
||||
|
||||
export function parseMapNodes(value: unknown): MapNode[] {
|
||||
if (!Array.isArray(value) || !value.every(isMapNode)) {
|
||||
throw new Error("Invalid map node data");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user