|
@@ -1,6 +1,6 @@
|
|
|
/** 2dUI 特效 血条 粒子等管理器*/
|
|
|
|
|
|
-import { assetManager, instantiate, Prefab, Node, UITransform, Vec3 } from "cc";
|
|
|
+import { assetManager, instantiate, Prefab, Node, UITransform, Vec3, Vec2, view } from "cc";
|
|
|
import { resLoader } from "db://assets/core_tgx/base/ResLoader";
|
|
|
import { StormSunderGlobalInstance } from "../StormSunderGlobalInstance";
|
|
|
import { BloodComponent } from "../Component/BloodComponent";
|
|
@@ -19,25 +19,39 @@ export class Effect2DUIMgr {
|
|
|
}
|
|
|
|
|
|
private bloodMap: Map<Node, Node> = new Map(); // 存储节点与对应血条的映射
|
|
|
-
|
|
|
async showBlood(target: Node, hpPercent: number) {
|
|
|
let bloodNode = this.bloodMap.get(target) as any;
|
|
|
|
|
|
if (!bloodNode) {
|
|
|
- // 加载血条预制体
|
|
|
const bloodPrefab = await resLoader.loadAsync(resLoader.gameBundleName, "Prefabs/Blood");
|
|
|
bloodNode = instantiate(bloodPrefab);
|
|
|
-
|
|
|
- // 添加到全局UI节点下
|
|
|
bloodNode.parent = StormSunderGlobalInstance.instance.bloodUI;
|
|
|
-
|
|
|
- // 设置血条位置跟随目标
|
|
|
- const worldPos = target.worldPosition.clone();
|
|
|
- bloodNode.setWorldPosition(worldPos.x, worldPos.y + 100, worldPos.z);
|
|
|
-
|
|
|
this.bloodMap.set(target, bloodNode);
|
|
|
}
|
|
|
|
|
|
+ // 获取主摄像机
|
|
|
+ const mainCamera = StormSunderGlobalInstance.instance.camera;
|
|
|
+ if (!mainCamera) {
|
|
|
+ console.error("Main camera not found!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换 3D 坐标到屏幕坐标
|
|
|
+ const targetWorldPos = target.worldPosition;
|
|
|
+ const screenPos = mainCamera.worldToScreen(targetWorldPos);
|
|
|
+
|
|
|
+ // 转换到 UI 节点的本地坐标
|
|
|
+ const uiPos = new Vec3();
|
|
|
+ StormSunderGlobalInstance.instance.bloodUI.getComponent(UITransform)!.convertToNodeSpaceAR(
|
|
|
+ new Vec3(screenPos.x, screenPos.y, 0),
|
|
|
+ uiPos
|
|
|
+ );
|
|
|
+
|
|
|
+ // 设置位置偏移
|
|
|
+ const offsetY = 300;
|
|
|
+ const offsetX = 150;
|
|
|
+ bloodNode.setPosition(uiPos.x + offsetX, uiPos.y + offsetY);
|
|
|
+
|
|
|
// 更新血量显示
|
|
|
const bloodComp = bloodNode.getComponent(BloodComponent);
|
|
|
if (bloodComp) {
|