"multiple spawn bug fixed"

This commit is contained in:
SreeNath14 2025-04-16 15:07:11 +05:30
parent 8b7b7f589a
commit 0eedbdd58e
1 changed files with 45 additions and 14 deletions

View File

@ -1,4 +1,10 @@
import React, { useRef, useEffect, useMemo, useCallback } from "react"; import React, {
useRef,
useEffect,
useMemo,
useCallback,
useState,
} from "react";
import { useLoader, useFrame } from "@react-three/fiber"; import { useLoader, useFrame } from "@react-three/fiber";
import { GLTFLoader } from "three-stdlib"; import { GLTFLoader } from "three-stdlib";
import * as THREE from "three"; import * as THREE from "three";
@ -48,6 +54,8 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
const gltf = useLoader(GLTFLoader, crate) as GLTF; const gltf = useLoader(GLTFLoader, crate) as GLTF;
const groupRef = useRef<THREE.Group>(null); const groupRef = useRef<THREE.Group>(null);
const tempStackedObjectsRef = useRef<Record<string, boolean>>({}); const tempStackedObjectsRef = useRef<Record<string, boolean>>({});
const [previouslyConnected, setPreviouslyConnected] = useState<any>({});
const currentSpawnedObjectRef = useRef<any>(null);
const { const {
animationStates, animationStates,
@ -116,8 +124,6 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
}); });
}, [animationStates, MaterialRef, agvRef]); }, [animationStates, MaterialRef, agvRef]);
// In processAnimator.tsx - only the relevant spawn logic part that needs fixes
// Add this function to ProcessAnimator component // Add this function to ProcessAnimator component
const isConnectedToActiveArmBot = useCallback( const isConnectedToActiveArmBot = useCallback(
(processId: any) => { (processId: any) => {
@ -153,6 +159,26 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
// Check connection status // Check connection status
const isConnected = isConnectedToActiveArmBot(process.id); const isConnected = isConnectedToActiveArmBot(process.id);
const wasConnected = previouslyConnected[process.id] || false;
// Track connection state changes
if (wasConnected !== isConnected) {
setTimeout(() => {
setPreviouslyConnected((prev: any) => ({
...prev,
[process.id]: isConnected,
}));
}, 0);
}
// If just disconnected, reset the spawn timer to allow new spawns
if (wasConnected && !isConnected) {
newStates[process.id] = {
...processState,
nextSpawnTime: currentTime + 0.1,
hasSpawnedZeroIntervalObject: false,
};
}
if (processState.isProcessDelaying) { if (processState.isProcessDelaying) {
// Existing delay handling logic... // Existing delay handling logic...
@ -192,7 +218,10 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
return; // Don't spawn more objects for zero interval return; // Don't spawn more objects for zero interval
} }
const effectiveSpawnInterval = spawnInterval / speedRef.current; const effectiveSpawnInterval = Math.max(
0.1,
spawnInterval / speedRef.current
);
if (currentTime >= processState.nextSpawnTime) { if (currentTime >= processState.nextSpawnTime) {
const objectId = `obj-${process.id}-${processState.objectIdCounter}`; const objectId = `obj-${process.id}-${processState.objectIdCounter}`;
@ -213,6 +242,9 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
progress: 0.005, // Start with tiny progress to ensure animation begins progress: 0.005, // Start with tiny progress to ensure animation begins
}; };
// Calculate next spawn time
const nextSpawnAt = currentTime + effectiveSpawnInterval;
// Update state with the new object and flag for zero interval // Update state with the new object and flag for zero interval
newStates[process.id] = { newStates[process.id] = {
...processState, ...processState,
@ -221,7 +253,7 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
[objectId]: newObject, [objectId]: newObject,
}, },
objectIdCounter: processState.objectIdCounter + 1, objectIdCounter: processState.objectIdCounter + 1,
nextSpawnTime: currentTime + effectiveSpawnInterval, nextSpawnTime: nextSpawnAt,
// Mark that we've spawned an object for zero interval case // Mark that we've spawned an object for zero interval case
hasSpawnedZeroIntervalObject: hasSpawnedZeroIntervalObject:
spawnInterval === 0 spawnInterval === 0
@ -237,6 +269,8 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
// Second useFrame for animation logic // Second useFrame for animation logic
useFrame((_, delta) => { useFrame((_, delta) => {
let currentProcessObject: any = null;
// Animation logic frame // Animation logic frame
const currentTime = const currentTime =
clockRef.current.getElapsedTime() - elapsedBeforePauseRef.current; clockRef.current.getElapsedTime() - elapsedBeforePauseRef.current;
@ -250,7 +284,7 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
return; return;
} }
// Check connection status with debugging // Check connection status
const isConnected = isConnectedToActiveArmBot(process.id); const isConnected = isConnectedToActiveArmBot(process.id);
if (isConnected) { if (isConnected) {
@ -263,6 +297,7 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
[id]: { [id]: {
...obj, ...obj,
state: { state: {
visible: true,
...obj.state, ...obj.state,
isAnimating: false, // Stop animation isAnimating: false, // Stop animation
isDelaying: false, // Clear delays isDelaying: false, // Clear delays
@ -511,12 +546,8 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
currentTime currentTime
); );
const newPointData = getPointDataForAnimationIndex(
process,
stateRef.currentIndex
);
// No action needed with newPointData here - will be handled in next frame // No action needed with newPointData here - will be handled in next frame
getPointDataForAnimationIndex(process, stateRef.currentIndex);
} else { } else {
// Update position with lerp // Update position with lerp
currentRef.position.lerpVectors( currentRef.position.lerpVectors(
@ -533,9 +564,9 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
// Log if no animation is occurring when it should // Log if no animation is occurring when it should
if (!animationOccurring && !isConnected) { if (!animationOccurring && !isConnected) {
// console.log( console.log(
// `Warning: No animation occurring for process ${process.id} despite not being connected` `Warning: No animation occurring for process ${process.id} despite not being connected`
// ); );
} }
newStates[process.id] = { newStates[process.id] = {