90 lines
2.9 KiB
TypeScript
90 lines
2.9 KiB
TypeScript
import IkInstances from "./IkInstances";
|
|
import armModel from "../../../assets/gltf-glb/rigged/ik_arm_4.glb";
|
|
import { useEffect, useState } from "react";
|
|
import { useThree } from "@react-three/fiber";
|
|
import { Vector3 } from "three";
|
|
|
|
interface Process {
|
|
triggerId: string;
|
|
startPoint?: Vector3;
|
|
endPoint?: Vector3;
|
|
speed: number;
|
|
}
|
|
|
|
interface ArmBotState {
|
|
uuid: string;
|
|
position: [number, number, number];
|
|
rotation: [number, number, number];
|
|
status: string;
|
|
material: string;
|
|
triggerId: string;
|
|
connections: {
|
|
source: { modelUUID: string; pointUUID: string };
|
|
targets: { modelUUID: string; pointUUID: string }[];
|
|
};
|
|
actions: { uuid: string; name: string; speed: number; processes: { triggerId: string; startPoint: string; endPoint: string }[]; };
|
|
isActive?: boolean;
|
|
}
|
|
|
|
interface StaticMachineState {
|
|
uuid: string;
|
|
status: string;
|
|
actions: { uuid: string; name: string; buffer: number; material: string; };
|
|
machineTriggerId: string;
|
|
connectedArmBot: string;
|
|
}
|
|
|
|
interface ArmbotInstancesProps {
|
|
index: number;
|
|
armBot: ArmBotState;
|
|
setArmBots: React.Dispatch<React.SetStateAction<ArmBotState[]>>;
|
|
setStaticMachines: React.Dispatch<React.SetStateAction<StaticMachineState[]>>;
|
|
}
|
|
|
|
export const ArmbotInstances: React.FC<ArmbotInstancesProps> = ({ index, armBot, setArmBots, setStaticMachines }) => {
|
|
const { scene } = useThree();
|
|
const [processes, setProcesses] = useState<Process[]>([]);
|
|
|
|
useEffect(() => {
|
|
if (armBot.actions.processes.length > 0) {
|
|
const mappedProcesses = armBot.actions.processes.map((process) => {
|
|
return {
|
|
triggerId: process.triggerId,
|
|
startPoint: scene.getObjectByProperty('uuid', process.startPoint)?.getWorldPosition(new Vector3()),
|
|
endPoint: scene.getObjectByProperty('uuid', process.endPoint)?.getWorldPosition(new Vector3()),
|
|
speed: armBot.actions.speed
|
|
};
|
|
});
|
|
setProcesses(mappedProcesses);
|
|
} else {
|
|
setProcesses([]);
|
|
}
|
|
}, [armBot, scene]);
|
|
|
|
const updateArmBotStatus = (status: string) => {
|
|
setArmBots((prevArmBots) => {
|
|
return prevArmBots.map(bot => {
|
|
if (bot.uuid === armBot.uuid) {
|
|
return { ...bot, status, triggerId: status === 'idle' ? '' : armBot.triggerId };
|
|
}
|
|
return bot;
|
|
});
|
|
});
|
|
};
|
|
|
|
return (
|
|
<IkInstances
|
|
key={index}
|
|
uuid={armBot.uuid}
|
|
selectedTrigger={armBot.triggerId}
|
|
modelUrl={armModel}
|
|
position={armBot.position}
|
|
rotation={armBot.rotation}
|
|
processes={processes}
|
|
armBot={armBot}
|
|
setArmBots={setArmBots}
|
|
setStaticMachines={setStaticMachines}
|
|
updateArmBotStatus={updateArmBotStatus}
|
|
/>
|
|
);
|
|
}; |