2025-05-08 09:49:21 +00:00
|
|
|
import {
|
|
|
|
usePlayButtonStore,
|
|
|
|
useResetButtonStore,
|
|
|
|
} from "../../../store/usePlayButtonStore";
|
2025-05-03 13:06:30 +00:00
|
|
|
import { useConveyorActions } from "./conveyor/useConveyorActions";
|
|
|
|
import { useMachineActions } from "./machine/useMachineActions";
|
|
|
|
import { useRoboticArmActions } from "./roboticArm/useRoboticArmActions";
|
|
|
|
import { useStorageActions } from "./storageUnit/useStorageUnitActions";
|
|
|
|
import { useVehicleActions } from "./vehicle/useVehicleActions";
|
2025-05-05 05:54:00 +00:00
|
|
|
import { useCallback, useEffect } from "react";
|
2025-05-03 13:06:30 +00:00
|
|
|
|
2025-05-05 05:54:00 +00:00
|
|
|
export function useActionHandler() {
|
2025-05-08 09:49:21 +00:00
|
|
|
const { isReset } = useResetButtonStore();
|
|
|
|
const { isPlaying } = usePlayButtonStore();
|
|
|
|
const { handleConveyorAction, cleanup: cleanupConveyor } =
|
|
|
|
useConveyorActions();
|
|
|
|
const { handleVehicleAction, cleanup: cleanupVehicle } = useVehicleActions();
|
|
|
|
const { handleRoboticArmAction, cleanup: cleanupRoboticArm } =
|
|
|
|
useRoboticArmActions();
|
|
|
|
const { handleMachineAction, cleanup: cleanupMachine } = useMachineActions();
|
|
|
|
const { handleStorageAction, cleanup: cleanupStorage } = useStorageActions();
|
2025-05-05 05:54:00 +00:00
|
|
|
|
2025-05-08 09:49:21 +00:00
|
|
|
const handleAction = useCallback(
|
|
|
|
(action: Action, materialId?: string) => {
|
|
|
|
if (!action) return;
|
2025-05-05 05:54:00 +00:00
|
|
|
try {
|
|
|
|
switch (action.actionType) {
|
|
|
|
case 'default': case 'spawn': case 'swap': case 'delay': case 'despawn':
|
2025-05-06 07:05:51 +00:00
|
|
|
handleConveyorAction(action as ConveyorAction, materialId as string);
|
2025-05-05 05:54:00 +00:00
|
|
|
break;
|
|
|
|
case 'travel':
|
2025-05-08 08:13:37 +00:00
|
|
|
handleVehicleAction(action as VehicleAction, materialId as string);
|
2025-05-05 05:54:00 +00:00
|
|
|
break;
|
|
|
|
case 'pickAndPlace':
|
2025-05-08 08:13:37 +00:00
|
|
|
handleRoboticArmAction(action as RoboticArmAction, materialId as string);
|
2025-05-05 05:54:00 +00:00
|
|
|
break;
|
|
|
|
case 'process':
|
|
|
|
handleMachineAction(action as MachineAction);
|
|
|
|
break;
|
|
|
|
case 'store':
|
|
|
|
handleStorageAction(action as StorageAction);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn(`Unknown action type: ${(action as Action).actionType}`);
|
|
|
|
}
|
2025-05-08 09:49:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
echo.error("Failed to handle action");
|
|
|
|
console.error("Error handling action:", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[
|
|
|
|
handleConveyorAction,
|
|
|
|
handleVehicleAction,
|
|
|
|
handleRoboticArmAction,
|
|
|
|
handleMachineAction,
|
|
|
|
handleStorageAction,
|
|
|
|
]
|
|
|
|
);
|
2025-05-05 05:54:00 +00:00
|
|
|
|
2025-05-08 09:49:21 +00:00
|
|
|
const cleanup = useCallback(() => {
|
|
|
|
cleanupConveyor();
|
|
|
|
cleanupVehicle();
|
|
|
|
cleanupRoboticArm();
|
|
|
|
cleanupMachine();
|
|
|
|
cleanupStorage();
|
|
|
|
}, [
|
|
|
|
cleanupConveyor,
|
|
|
|
cleanupVehicle,
|
|
|
|
cleanupRoboticArm,
|
|
|
|
cleanupMachine,
|
|
|
|
cleanupStorage,
|
|
|
|
]);
|
2025-05-05 05:54:00 +00:00
|
|
|
|
2025-05-08 09:49:21 +00:00
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
cleanup();
|
2025-05-05 05:54:00 +00:00
|
|
|
};
|
2025-05-08 09:49:21 +00:00
|
|
|
}, [cleanup, isReset, isPlaying]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
handleAction,
|
|
|
|
cleanup,
|
|
|
|
};
|
|
|
|
}
|