2025-07-03 16:55:30 +05:30
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
import { useFrame } from '@react-three/fiber';
|
|
|
|
|
import { usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../store/usePlayButtonStore';
|
|
|
|
|
import { useSceneContext } from '../../../scene/sceneContext';
|
|
|
|
|
|
|
|
|
|
export function useHumanEventManager() {
|
|
|
|
|
const { humanStore } = useSceneContext();
|
|
|
|
|
const { getHumanById } = humanStore();
|
2025-07-09 16:34:38 +05:30
|
|
|
|
|
|
|
|
const callbacksRef = useRef<Map<string, (() => void)[]>>(new Map());
|
2025-07-03 16:55:30 +05:30
|
|
|
const isMonitoringRef = useRef(false);
|
2025-07-09 16:34:38 +05:30
|
|
|
const isCooldownRef = useRef<Map<string, boolean>>(new Map());
|
|
|
|
|
|
2025-07-03 16:55:30 +05:30
|
|
|
const { isPlaying } = usePlayButtonStore();
|
|
|
|
|
const { isPaused } = usePauseButtonStore();
|
|
|
|
|
const { isReset } = useResetButtonStore();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isReset) {
|
2025-07-09 16:34:38 +05:30
|
|
|
callbacksRef.current.clear();
|
|
|
|
|
isCooldownRef.current.clear();
|
2025-07-03 16:55:30 +05:30
|
|
|
}
|
2025-07-09 16:34:38 +05:30
|
|
|
}, [isReset]);
|
2025-07-03 16:55:30 +05:30
|
|
|
|
2025-07-03 18:01:11 +05:30
|
|
|
const addHumanToMonitor = (humanId: string, callback: () => void) => {
|
2025-07-09 16:34:38 +05:30
|
|
|
if (!callbacksRef.current.has(humanId)) {
|
|
|
|
|
callbacksRef.current.set(humanId, []);
|
2025-07-03 16:55:30 +05:30
|
|
|
}
|
2025-07-09 16:34:38 +05:30
|
|
|
callbacksRef.current.get(humanId)!.push(callback);
|
2025-07-03 16:55:30 +05:30
|
|
|
|
|
|
|
|
if (!isMonitoringRef.current) {
|
|
|
|
|
isMonitoringRef.current = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const removeHumanFromMonitor = (humanId: string) => {
|
2025-07-09 16:34:38 +05:30
|
|
|
callbacksRef.current.delete(humanId);
|
|
|
|
|
isCooldownRef.current.delete(humanId);
|
2025-07-03 16:55:30 +05:30
|
|
|
|
2025-07-09 16:34:38 +05:30
|
|
|
if (callbacksRef.current.size === 0) {
|
2025-07-03 16:55:30 +05:30
|
|
|
isMonitoringRef.current = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useFrame(() => {
|
|
|
|
|
|
2025-07-09 16:34:38 +05:30
|
|
|
if (!isMonitoringRef.current || !isPlaying || isPaused) return;
|
|
|
|
|
|
|
|
|
|
callbacksRef.current.forEach((queue, humanId) => {
|
|
|
|
|
if (queue.length === 0 || isCooldownRef.current.get(humanId)) return;
|
|
|
|
|
|
2025-07-03 16:55:30 +05:30
|
|
|
const human = getHumanById(humanId);
|
2025-07-09 16:34:38 +05:30
|
|
|
const actionType = human?.point.action.actionType;
|
|
|
|
|
|
|
|
|
|
let conditionMet = false;
|
|
|
|
|
|
|
|
|
|
if (actionType === 'worker') {
|
|
|
|
|
conditionMet = Boolean(
|
|
|
|
|
human &&
|
|
|
|
|
human.isActive === false &&
|
|
|
|
|
human.state === 'idle' &&
|
|
|
|
|
human.isScheduled === false &&
|
|
|
|
|
human.currentLoad < human.point.action.loadCapacity
|
|
|
|
|
);
|
|
|
|
|
} else if (actionType === 'assembly') {
|
|
|
|
|
conditionMet = Boolean(
|
|
|
|
|
human &&
|
|
|
|
|
human.isActive === false &&
|
|
|
|
|
human.state === 'idle' &&
|
|
|
|
|
human.isScheduled === false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (conditionMet) {
|
|
|
|
|
const callback = queue.shift();
|
|
|
|
|
if (callback) {
|
2025-07-08 16:31:20 +05:30
|
|
|
callback();
|
2025-07-09 16:34:38 +05:30
|
|
|
|
|
|
|
|
if (queue.length === 0) {
|
|
|
|
|
removeHumanFromMonitor(humanId);
|
|
|
|
|
} else {
|
|
|
|
|
isCooldownRef.current.set(humanId, true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
isCooldownRef.current.set(humanId, false);
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
2025-07-08 16:31:20 +05:30
|
|
|
}
|
2025-07-03 16:55:30 +05:30
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
2025-07-09 16:34:38 +05:30
|
|
|
callbacksRef.current.clear();
|
2025-07-03 16:55:30 +05:30
|
|
|
isMonitoringRef.current = false;
|
2025-07-09 16:34:38 +05:30
|
|
|
isCooldownRef.current.clear();
|
2025-07-03 16:55:30 +05:30
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
addHumanToMonitor,
|
|
|
|
|
removeHumanFromMonitor,
|
|
|
|
|
};
|
2025-07-09 16:34:38 +05:30
|
|
|
}
|