Ver código fonte

设置血条位置 空间转屏幕坐标

woso_javan 2 meses atrás
pai
commit
e9ab880ce6

+ 7 - 7
assets/module_storm_sunder/Prefabs/Blood.prefab

@@ -54,7 +54,7 @@
       "z": 1
     },
     "_mobility": 0,
-    "_layer": 1073741824,
+    "_layer": 33554432,
     "_euler": {
       "__type__": "cc.Vec3",
       "x": 0,
@@ -111,7 +111,7 @@
       "z": 1
     },
     "_mobility": 0,
-    "_layer": 1073741824,
+    "_layer": 33554432,
     "_euler": {
       "__type__": "cc.Vec3",
       "x": 0,
@@ -143,7 +143,7 @@
     },
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": -100,
+      "x": -50,
       "y": 0,
       "z": 0
     },
@@ -184,7 +184,7 @@
     },
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 200,
+      "width": 100,
       "height": 15
     },
     "_anchorPoint": {
@@ -270,7 +270,7 @@
     },
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 200,
+      "width": 100,
       "height": 15
     },
     "_anchorPoint": {
@@ -345,7 +345,7 @@
       "__id__": 6
     },
     "_mode": 0,
-    "_totalLength": 200,
+    "_totalLength": 100,
     "_progress": 1,
     "_reverse": false,
     "_id": ""
@@ -381,7 +381,7 @@
     },
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 200,
+      "width": 100,
       "height": 15
     },
     "_anchorPoint": {

+ 24 - 10
assets/module_storm_sunder/Script/Manager/Effect2DUIMgr.ts

@@ -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) {

+ 3 - 1
assets/module_storm_sunder/Script/StormSunderGlobalInstance.ts

@@ -1,4 +1,4 @@
-import { _decorator, assetManager, Component, find, Node, Prefab } from 'cc';
+import { _decorator, assetManager, Camera, Component, find, Node, Prefab } from 'cc';
 import { resLoader } from '../../core_tgx/base/ResLoader';
 import { ResourcePool } from './ResourcePool';
 import { GameUtil } from './GameUtil';
@@ -18,6 +18,7 @@ export class StormSunderGlobalInstance {
     }
 
     public async initUI() {
+        this.camera = find("MainCamera").getComponent(Camera)!;
         this.map = find("Canvas/Map");
         this.gameRoot = find("GameRoot");
         this.homeUI = find("Canvas/HomeUI");
@@ -30,6 +31,7 @@ export class StormSunderGlobalInstance {
         this.btnStart = find('Canvas/HomeUI/BtnStart')!;
     }
 
+    public camera: Camera = null!; //3d相机
     public map: Node = null!;
     public gameRoot: Node = null!;
     public homeUI: Node = null!;

+ 88 - 22
assets/module_storm_sunder/rooster_stormsunder.scene

@@ -29,7 +29,7 @@
     "_active": true,
     "_components": [],
     "_prefab": {
-      "__id__": 99
+      "__id__": 101
     },
     "_lpos": {
       "__type__": "cc.Vec3",
@@ -60,7 +60,7 @@
     },
     "autoReleaseAssets": false,
     "_globals": {
-      "__id__": 100
+      "__id__": 102
     },
     "_id": "be14c61f-22d8-4bb9-b444-ad9f29740469"
   },
@@ -685,22 +685,22 @@
         "__id__": 73
       },
       {
-        "__id__": 93
+        "__id__": 95
       }
     ],
     "_active": true,
     "_components": [
       {
-        "__id__": 95
+        "__id__": 97
       },
       {
-        "__id__": 96
+        "__id__": 98
       },
       {
-        "__id__": 97
+        "__id__": 99
       },
       {
-        "__id__": 98
+        "__id__": 100
       }
     ],
     "_prefab": null,
@@ -2506,16 +2506,16 @@
         "__id__": 87
       },
       {
-        "__id__": 89
+        "__id__": 90
       }
     ],
     "_active": false,
     "_components": [
       {
-        "__id__": 91
+        "__id__": 93
       },
       {
-        "__id__": 92
+        "__id__": 94
       }
     ],
     "_prefab": null,
@@ -3044,6 +3044,9 @@
     "_components": [
       {
         "__id__": 88
+      },
+      {
+        "__id__": 89
       }
     ],
     "_prefab": null,
@@ -3098,6 +3101,36 @@
     },
     "_id": "63Xsvoy4dMrKv5p5rROgzD"
   },
+  {
+    "__type__": "cc.Widget",
+    "_name": "",
+    "_objFlags": 0,
+    "__editorExtras__": {},
+    "node": {
+      "__id__": 87
+    },
+    "_enabled": true,
+    "__prefab": null,
+    "_alignFlags": 45,
+    "_target": null,
+    "_left": 0,
+    "_right": 0,
+    "_top": 0,
+    "_bottom": 0,
+    "_horizontalCenter": 0,
+    "_verticalCenter": 0,
+    "_isAbsLeft": true,
+    "_isAbsRight": true,
+    "_isAbsTop": true,
+    "_isAbsBottom": true,
+    "_isAbsHorizontalCenter": true,
+    "_isAbsVerticalCenter": true,
+    "_originalWidth": 720,
+    "_originalHeight": 1280,
+    "_alignMode": 2,
+    "_lockFlags": 0,
+    "_id": "a7SHsquIhEeI5aN3Cieu8r"
+  },
   {
     "__type__": "cc.Node",
     "_name": "Blood",
@@ -3110,7 +3143,10 @@
     "_active": true,
     "_components": [
       {
-        "__id__": 90
+        "__id__": 91
+      },
+      {
+        "__id__": 92
       }
     ],
     "_prefab": null,
@@ -3149,7 +3185,7 @@
     "_objFlags": 0,
     "__editorExtras__": {},
     "node": {
-      "__id__": 89
+      "__id__": 90
     },
     "_enabled": true,
     "__prefab": null,
@@ -3165,6 +3201,36 @@
     },
     "_id": "66Uegy/NpMkLrXHvalYxBr"
   },
+  {
+    "__type__": "cc.Widget",
+    "_name": "",
+    "_objFlags": 0,
+    "__editorExtras__": {},
+    "node": {
+      "__id__": 90
+    },
+    "_enabled": true,
+    "__prefab": null,
+    "_alignFlags": 45,
+    "_target": null,
+    "_left": 0,
+    "_right": 0,
+    "_top": 0,
+    "_bottom": 0,
+    "_horizontalCenter": 0,
+    "_verticalCenter": 0,
+    "_isAbsLeft": true,
+    "_isAbsRight": true,
+    "_isAbsTop": true,
+    "_isAbsBottom": true,
+    "_isAbsHorizontalCenter": true,
+    "_isAbsVerticalCenter": true,
+    "_originalWidth": 720,
+    "_originalHeight": 1280,
+    "_alignMode": 2,
+    "_lockFlags": 0,
+    "_id": "e7TipOP7VBZI0HpiO5hsdV"
+  },
   {
     "__type__": "cc.UITransform",
     "_name": "",
@@ -3229,7 +3295,7 @@
     "_active": true,
     "_components": [
       {
-        "__id__": 94
+        "__id__": 96
       }
     ],
     "_prefab": null,
@@ -3268,7 +3334,7 @@
     "_objFlags": 0,
     "__editorExtras__": {},
     "node": {
-      "__id__": 93
+      "__id__": 95
     },
     "_enabled": true,
     "__prefab": null,
@@ -3378,28 +3444,28 @@
   {
     "__type__": "cc.SceneGlobals",
     "ambient": {
-      "__id__": 101
+      "__id__": 103
     },
     "shadows": {
-      "__id__": 102
+      "__id__": 104
     },
     "_skybox": {
-      "__id__": 103
+      "__id__": 105
     },
     "fog": {
-      "__id__": 104
+      "__id__": 106
     },
     "octree": {
-      "__id__": 105
+      "__id__": 107
     },
     "skin": {
-      "__id__": 106
+      "__id__": 108
     },
     "lightProbeInfo": {
-      "__id__": 107
+      "__id__": 109
     },
     "postSettings": {
-      "__id__": 108
+      "__id__": 110
     },
     "bakedWithStationaryMainLight": false,
     "bakedWithHighpLightmap": false