From 11fc5c2dbf36385a23aa5f04f17f5c65a623c8b4 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Fri, 9 May 2025 23:32:15 +0530 Subject: [PATCH] Enhance StorageMechanics: improve material handling in spawn mode, update capacity change logic, and streamline material creation --- .../mechanics/storageMechanics.tsx | 74 ++++++++++++------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx index b21afb6..c67ef7c 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx @@ -21,6 +21,7 @@ function StorageMechanics() { setCurrentMaterials, clearCurrentMaterials, updateStorageUnitLoad, + getStorageUnitById, } = useStorageUnitStore(); const email = localStorage.getItem('email') @@ -76,12 +77,19 @@ function StorageMechanics() { } if (validOption === "spawn") { - clearCurrentMaterials(selectedEventData.data.modelUuid); - updateStorageUnitLoad(selectedEventData.data.modelUuid, selectedPointData.action.storageCapacity); - } else { - const materialType = selectedPointData.action.materialType || "default"; + const storageUnit = getStorageUnitById(selectedEventData.data.modelUuid); + if (storageUnit) { + const materialType = selectedPointData.action.materialType || "default"; + const materials = Array.from({ length: selectedPointData.action.storageCapacity }, () => + createNewMaterial(materialType) + ).filter(Boolean) as { materialType: string; materialId: string }[]; - updateStorageUnitLoad(selectedEventData.data.modelUuid, selectedPointData.action.storageCapacity); + setCurrentMaterials(selectedEventData.data.modelUuid, materials); + updateStorageUnitLoad(selectedEventData.data.modelUuid, selectedPointData.action.storageCapacity); + } + } else { + clearCurrentMaterials(selectedEventData.data.modelUuid); + updateStorageUnitLoad(selectedEventData.data.modelUuid, 0); } }; @@ -115,34 +123,41 @@ function StorageMechanics() { event ); - if (activeOption === "store") { - const materialType = selectedPointData.action.materialType || "default"; + if (activeOption === "spawn") { + const storageUnit = getStorageUnitById(selectedEventData.data.modelUuid); + if (storageUnit) { + const materialType = selectedPointData.action.materialType || "default"; + const currentCount = storageUnit.currentMaterials.length; - updateStorageUnitLoad(selectedEventData.data.modelUuid, newCapacity); + if (newCapacity > currentCount) { + const newMaterials = Array.from({ length: newCapacity - currentCount }, () => + createNewMaterial(materialType) + ).filter(Boolean) as { materialType: string; materialId: string }[]; + + setCurrentMaterials(selectedEventData.data.modelUuid, [ + ...storageUnit.currentMaterials, + ...newMaterials + ]); + } else if (newCapacity < currentCount) { + setCurrentMaterials( + selectedEventData.data.modelUuid, + storageUnit.currentMaterials.slice(0, newCapacity) + ); + } + updateStorageUnitLoad(selectedEventData.data.modelUuid, newCapacity); + } + } else { + updateStorageUnitLoad(selectedEventData.data.modelUuid, 0); } } }; - const createNewMaterial = (materialType: string): MaterialSchema | null => { + const createNewMaterial = (materialType: string): { materialType: string; materialId: string } | null => { if (!selectedEventData || !selectedPointData) return null; const materialId = THREE.MathUtils.generateUUID(); return { - materialId, - materialName: `${materialType}-${Date.now()}`, materialType, - isActive: false, - isVisible: true, - isPaused: false, - isRendered: true, - startTime: performance.now(), - current: { - modelUuid: selectedEventData.data.modelUuid || '', - pointUuid: selectedPointData?.uuid || '', - actionUuid: selectedPointData?.action.actionUuid || '' - }, - endTime: undefined, - previous: undefined, - next: null + materialId }; }; @@ -161,8 +176,15 @@ function StorageMechanics() { event ); - if (activeOption === "store") { - const capacity = selectedPointData.action.storageCapacity || 0; + if (activeOption === "spawn") { + const storageUnit = getStorageUnitById(selectedEventData.data.modelUuid); + if (storageUnit) { + const materials = Array.from({ length: storageUnit.currentMaterials.length }, () => + createNewMaterial(value) + ).filter(Boolean) as { materialType: string; materialId: string }[]; + + setCurrentMaterials(selectedEventData.data.modelUuid, materials); + } } } };