feat: 优化机器人状态更新逻辑,合并图元更新和位置可见性设置,提升渲染效率

This commit is contained in:
xudan 2025-09-16 10:26:50 +08:00
parent 7ffd5623f0
commit e2cb39b27c

View File

@ -98,7 +98,9 @@ const monitorScene = async () => {
const batchUpdateRobots = (updates: Array<{ id: string; data: RobotRealtimeInfo }>) => {
if (!editor.value || updates.length === 0) return;
//
// pen
const allPenUpdates: any[] = [];
updates.forEach(({ id, data }) => {
const {
x,
@ -114,76 +116,66 @@ const monitorScene = async () => {
...rest
} = data;
//
// //便
// 1.
editor.value?.updateRobot(id, {
...rest,
isCharging: isCharging as any as boolean,
isCarrying: isCarrying as any as boolean,
isNotAcceptingOrders: isNotAcceptingOrders as any as boolean,
isCharging: isCharging as any,
isCarrying: isCarrying as any,
isNotAcceptingOrders: isNotAcceptingOrders as any,
});
// refreshRobot
let processedPath: Array<{ x: number; y: number }> | undefined;
// 2. pen
const penUpdatePayload: any = { id };
const robotState: any = {};
// 2.1 robotState
if (points?.length) {
//
const cx = x || 37; // X37
const cy = y || 37; // Y37
processedPath = points.map((p) => ({ x: p.x - cx, y: p.y - cy }));
const cx = x || 37;
const cy = y || 37;
robotState.path = points.map((p) => ({ x: p.x - cx, y: p.y - cy }));
}
//
const robotState = {
active,
isWaring,
isFault,
path: processedPath,
angle,
isCharging,
isCarrying,
isNotAcceptingOrders,
};
if (Object.values(robotState).some((v) => v !== undefined)) {
editor.value?.setValue({ id, robot: robotState }, { render: false, history: false, doEvent: false });
// 2.2
if (active !== undefined) robotState.active = active;
if (isWaring !== undefined) robotState.isWaring = isWaring;
if (isFault !== undefined) robotState.isFault = isFault;
if (isCharging !== undefined) robotState.isCharging = isCharging;
if (isCarrying !== undefined) robotState.isCarrying = isCarrying;
if (isNotAcceptingOrders !== undefined) robotState.isNotAcceptingOrders = isNotAcceptingOrders;
// payload
if (Object.keys(robotState).length > 0) {
penUpdatePayload.robot = robotState;
}
});
//
const positionUpdates = updates
.map(({ id, data }) => {
const { x, y, angle } = data;
// xy angle / false
if (isNil(x) && isNil(y) && angle == null) {
return undefined;
}
const payload: any = { id };
if (!isNil(x) && !isNil(y)) {
const newX = x - 60;
const newY = y - 60;
payload.x = newX;
payload.y = newY;
payload.visible = true;
payload.locked = LockState.None;
}
if (angle != null) {
// angle
payload.rotate = -angle + 180;
}
return payload;
})
.filter((v) => v);
// 2.3
if (!isNil(x) && !isNil(y)) {
penUpdatePayload.x = x - 60;
penUpdatePayload.y = y - 60;
penUpdatePayload.visible = true;
penUpdatePayload.locked = LockState.None;
}
if (angle != null) {
penUpdatePayload.rotate = -angle + 180;
}
// 使Meta2D
positionUpdates.forEach((update) => {
editor.value?.setValue(update, { render: false, history: false, doEvent: false });
});
//
if (Object.keys(penUpdatePayload).length > 1) {
allPenUpdates.push(penUpdatePayload);
}
// > >
updates.forEach(({ id }) => {
// 3. (API)
editor.value?.updateRobotStatusOverlay?.(id, false);
});
//
// 4. 使Meta2D
if (allPenUpdates.length > 0) {
allPenUpdates.forEach((update) => {
editor.value?.setValue(update, { render: false, history: false, doEvent: false });
});
}
// 5.
editor.value?.render();
};