import * as THREE from 'three' import BaseEntity from '@/core/base/BaseItemEntity.ts' import type Viewport from '@/core/engine/Viewport.ts' import gsap from 'gsap' import { nextTick } from 'vue' import Cl23dObject, {type Cl2Task} from "@/modules/cl2/Cl23dObject"; /** * CL2 机械臂实体类 * 0.4m/ss // a max 1.2m/s * 90 = 3.5s cl2 * 90 = 5s // cLX */ export default class Cl2Entity extends BaseEntity { constructor(viewport: Viewport, id: string) { super(viewport, id) } // 移动 addRobotTask(task: Cl2Task) { if (task.id === 10010) { let startX = task.content.StartX let startY = task.content.StartY if (task.content.Link?.length > 0) { for (let i = 0; i < task.content.Link.length; i++) { const link = task.content.Link[i]; let moveDirection: 0 | 1 | 2 | 3 | 15 = 15; // 计算 移动方向 if (startX !== link.X && startY !== link.Y) { throw new Error('Invalid task'); } else if (startX === link.X) { if (startY < link.Y && link.Speed > 0 || startY > link.Y && link.Speed < 0) { moveDirection = 3 } else { moveDirection = 1 } } else { if (startX > link.X && link.Speed > 0 || startX < link.X && link.Speed < 0) { moveDirection = 2 } else { moveDirection = 0 } } this.taskQueue.add(this.createTask('ROTATION', () => this.cl2Object.addRotation(moveDirection * Math.PI/2) )) this.taskQueue.add(this.createTask('TRAVEL', () => this.cl2Object.addTravel(Model.getPositionByEntityId(link.id) ,Math.abs(link.Speed/1000)) )) startX = link.X startY = link.Y } } //移动 if (task.content.OperationType === 0) { } else if (task.content.OperationType === 1) { } else if (task.content.OperationType === 2) { } else if (task.content.OperationType === 3) { } else if (task.content.OperationType === 4) { // 取货 if (task.content.PickMode === 1) { const gh = task.content.GoodsSlotHeight/1000 this.addArmRaise(gh) this.addArmExtender() // this.addArmRaise(gh + 0.1) this.addLoad(task.content.GoodsId) this.addArmRetractor() this.addArmLower() // 放货 } else if (task.content.PickMode === 2) { this.addArmRaise(task.content.GoodsSlotHeight/1000 + 0.2) this.addArmExtender() this.addArmLower() this.addUnload(task.content.GoodsId, '') this.addArmRetractor() } } else if (task.content.OperationType === 5) { } else if (task.content.OperationType === 135) { } else if (task.content.OperationType === 136) {} } } // 抬 addArmRaise(height: number) { // super.addArmRaise(height) this.taskQueue.add(this.createTask('ARM_RAISE', () => this.cl2Object.animationUpFork(height) )) } // 降 addArmLower() { // super.addArmLower() this.taskQueue.add(this.createTask('ARM_LOWER', () => this.cl2Object.animationDownFork() )) } // 伸 addArmExtender(z: number = 1.2) { // super.addArmExtender() this.taskQueue.add(this.createTask('ARM_EXTEND', () => this.cl2Object.animationShowFork(z) )) } // 缩 addArmRetractor() { // super.addArmRetractor() this.taskQueue.add(this.createTask('ARM_RETRACT', () => this.cl2Object.animationHideFork() )) } // 装 addLoad(item: string): void { const ptrForkMesh = this.getArmObject() this.taskQueue.add(this.createTask('LOAD', async () => { // 实际业务中应包含装载逻辑 this.isCarrying = true this.isLoading = true // 将物品拾取到机械臂上 const mesh = this.pickupItem(item) mesh.position.set(0, 0, -0.15) mesh.rotation.set(0, THREE.MathUtils.degToRad(90), 0) ptrForkMesh.add(mesh) })) this.taskQueue.add(this.createTask('ARM_RAISE', ()=>{ return new Promise(resolve => { gsap.to(ptrForkMesh.position, { y: ptrForkMesh.position.y + 0.2, duration: 1, ease: 'sine.inOut', onComplete: () => { this.isCarrying = true this.isLoading = false resolve() } }) }) })) } // 卸 addUnload(itemId: string, rackId: string, bay?: number, level?: number, cell?: number): void { this.taskQueue.add(this.createTask('UNLOAD', async () => { const item = this.viewport.entityManager.findItemById(itemId) this.isCarrying = false this.isUnloading = false // 将物品从机械臂上卸下 this.dropItem(item, rackId, bay, level) })) } // 帮助方法:获取机械臂对象 getArmObject(): THREE.Object3D | undefined { const agv = this.object as THREE.Group if (agv.children.length > 1) { const pillar = agv.children[1] if (pillar.children.length > 1) { return pillar.children[1] } } return undefined } // 帮助方法:获取机械臂立柱 getArmPillar(): THREE.Object3D | undefined { const agv = this.object as THREE.Group if (agv.children.length > 1) { return agv.children[1] } return undefined } get cl2Object(): Cl23dObject { return this.object as Cl23dObject } }