import { useMemo, useState } from 'react'; import { useSelectedActionSphere, useToggleView, useSimulationPaths, useSelectedPath, useStartSimulation } from '../../store/store'; import * as THREE from 'three'; import useModuleStore from '../../store/useModuleStore'; function SimulationUI() { const { ToggleView } = useToggleView(); const { activeModule } = useModuleStore(); const { startSimulation, setStartSimulation } = useStartSimulation(); const { selectedActionSphere } = useSelectedActionSphere(); const { selectedPath, setSelectedPath } = useSelectedPath(); const { simulationPaths, setSimulationPaths } = useSimulationPaths(); const handleAddAction = () => { if (!selectedActionSphere) return; const newAction = { uuid: THREE.MathUtils.generateUUID(), type: 'Inherit', material: 'Inherit', delay: 'Inherit', spawnInterval: 'Inherit', isUsed: false }; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, actions: [...point.actions, newAction] } : point ), })); setSimulationPaths(updatedPaths); }; const handleDeleteAction = (uuid: string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, actions: point.actions.filter(action => action.uuid !== uuid) } : point ), })); setSimulationPaths(updatedPaths); }; const handleActionSelect = (uuid: string, actionType: string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, actions: point.actions.map((action) => action.uuid === uuid ? { ...action, type: actionType } : action ), } : point ), })); setSimulationPaths(updatedPaths); }; const handleMaterialSelect = (uuid: string, material: string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, actions: point.actions.map((action) => action.uuid === uuid ? { ...action, material } : action ), } : point ), })); setSimulationPaths(updatedPaths); }; const handleDelayChange = (uuid: string, delay: number | string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, actions: point.actions.map((action) => action.uuid === uuid ? { ...action, delay } : action ), } : point ), })); setSimulationPaths(updatedPaths); }; const handleSpawnIntervalChange = (uuid: string, spawnInterval: number | string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, actions: point.actions.map((action) => action.uuid === uuid ? { ...action, spawnInterval } : action ), } : point ), })); setSimulationPaths(updatedPaths); }; const handleSpeedChange = (speed: number) => { if (!selectedPath) return; const updatedPaths = simulationPaths.map((path) => path.modeluuid === selectedPath.path.modeluuid ? { ...path, speed } : path ); setSimulationPaths(updatedPaths); setSelectedPath({ ...selectedPath, path: { ...selectedPath.path, speed } }); }; const handleAddTrigger = () => { if (!selectedActionSphere) return; const newTrigger = { uuid: THREE.MathUtils.generateUUID(), type: '', isUsed: false }; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, triggers: [...point.triggers, newTrigger] } : point ), })); setSimulationPaths(updatedPaths); }; const handleDeleteTrigger = (uuid: string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, triggers: point.triggers.filter(trigger => trigger.uuid !== uuid) } : point ), })); setSimulationPaths(updatedPaths); }; const handleTriggerSelect = (uuid: string, triggerType: string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, triggers: point.triggers.map((trigger) => trigger.uuid === uuid ? { ...trigger, type: triggerType } : trigger ), } : point ), })); setSimulationPaths(updatedPaths); }; const handleResetPath = () => { if (!selectedPath) return; }; const handleActionToggle = (uuid: string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, actions: point.actions.map((action) => ({ ...action, isUsed: action.uuid === uuid ? !action.isUsed : false, })), } : point ), })); setSimulationPaths(updatedPaths); }; const handleTriggerToggle = (uuid: string) => { if (!selectedActionSphere) return; const updatedPaths = simulationPaths.map((path) => ({ ...path, points: path.points.map((point) => point.uuid === selectedActionSphere.point.uuid ? { ...point, triggers: point.triggers.map((trigger) => ({ ...trigger, isUsed: trigger.uuid === uuid ? !trigger.isUsed : false, })), } : point ), })); setSimulationPaths(updatedPaths); }; const selectedPoint = useMemo(() => { if (!selectedActionSphere) return null; return simulationPaths.flatMap((path) => path.points).find((point) => point.uuid === selectedActionSphere.point.uuid); }, [selectedActionSphere, simulationPaths]); return ( <> {activeModule === "simulation" && (
{!ToggleView && ( <> {selectedPath && (
handleSpeedChange(parseFloat(e.target.value))} />
)} {selectedActionSphere && (
{selectedPoint?.actions.map((action) => (
{(action.type === 'Spawn' || action.type === 'Swap') && (
)} {action.type === 'Delay' && (
handleDelayChange(action.uuid, parseInt(e.target.value) || 'Inherit')} />
)} {action.type === 'Spawn' && (
handleSpawnIntervalChange(action.uuid, parseInt(e.target.value) || 'Inherit')} />
)}
))}
{selectedPoint?.triggers.map((trigger) => (

))}
)} {selectedPath && (
)} )}
)} ); } export default SimulationUI;