import { Node, UITransform, Vec3 } from 'cc'; /** * 背包字块拖拽到地图时,ui_line 从字块中心指向目标格中心。 * 仅设置 ui_line 根节点 Content Size 的 width(配合 Mask 裁剪子节点线条图),不拉伸子图。 */ export class DragPlaceLinePresenter { private _root: Node | null = null; private _defaultHeight = 9; bind(root: Node | null): void { this._root = root?.isValid ? root : null; if (!this._root) return; const ut = this._root.getComponent(UITransform); if (ut) { this._defaultHeight = ut.contentSize.height; } this.hide(); } unbind(): void { this.hide(); this._root = null; } /** 从起点世界坐标拉线到终点世界坐标 */ show(fromWorld: Readonly, toWorld: Readonly): void { if (!this._root?.isValid) return; const parent = this._root.parent; const parentUt = parent?.getComponent(UITransform); if (!parentUt) return; const startLocal = parentUt.convertToNodeSpaceAR(new Vec3(fromWorld.x, fromWorld.y, fromWorld.z)); const endLocal = parentUt.convertToNodeSpaceAR(new Vec3(toWorld.x, toWorld.y, toWorld.z)); const dx = endLocal.x - startLocal.x; const dy = endLocal.y - startLocal.y; const dist = Math.sqrt(dx * dx + dy * dy); if (dist < 1) { this.hide(); return; } const angle = Math.atan2(dy, dx) * 180 / Math.PI; this._root.active = true; this._root.setPosition(startLocal); this._root.angle = angle; const ut = this._root.getComponent(UITransform); ut?.setContentSize(dist, this._defaultHeight); } hide(): void { if (this._root?.isValid) { this._root.active = false; } } }