From 087f4010be6bd4c67bf59da6bdc9bd3f7dea8bea Mon Sep 17 00:00:00 2001 From: panw Date: Tue, 4 Nov 2025 14:23:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B8=B2=E6=9F=93):=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E5=AE=9E=E4=BD=93=E5=B1=82=E7=BA=A7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除通过 setSiblingIndex 手动设置层级的方式 - 新增 HERO、LINE1、LINE2、SKILL 等容器节点自动管理层级 - 调整英雄、怪物、技能等实体的父节点到对应容器 - 优化提示信息的位置偏移量 --- assets/resources/game/map/map_rpg.prefab | 435 +++++++++++++++++++++-- assets/script/game/hero/Hero.ts | 5 +- assets/script/game/hero/HeroMove.ts | 8 +- assets/script/game/hero/HeroViewComp.ts | 6 +- assets/script/game/hero/Mon.ts | 18 +- assets/script/game/map/HInfoComp.ts | 2 +- assets/script/game/skill/Skill.ts | 4 +- 7 files changed, 419 insertions(+), 59 deletions(-) diff --git a/assets/resources/game/map/map_rpg.prefab b/assets/resources/game/map/map_rpg.prefab index 32da81ff..d8ec1a6f 100644 --- a/assets/resources/game/map/map_rpg.prefab +++ b/assets/resources/game/map/map_rpg.prefab @@ -28,17 +28,17 @@ "_active": true, "_components": [ { - "__id__": 110 + "__id__": 126 }, { - "__id__": 112 + "__id__": 128 }, { - "__id__": 114 + "__id__": 130 } ], "_prefab": { - "__id__": 116 + "__id__": 132 }, "_lpos": { "__type__": "cc.Vec3", @@ -197,17 +197,17 @@ "_active": true, "_components": [ { - "__id__": 103 + "__id__": 119 }, { - "__id__": 105 + "__id__": 121 }, { - "__id__": 107 + "__id__": 123 } ], "_prefab": { - "__id__": 109 + "__id__": 125 }, "_lpos": { "__type__": "cc.Vec3", @@ -257,17 +257,17 @@ "__id__": 88 }, { - "__id__": 94 + "__id__": 110 } ], "_active": true, "_components": [ { - "__id__": 100 + "__id__": 116 } ], "_prefab": { - "__id__": 102 + "__id__": 118 }, "_lpos": { "__type__": "cc.Vec3", @@ -2023,23 +2023,83 @@ { "__type__": "cc.Node", "_name": "EntityLayer", - "_objFlags": 512, + "_objFlags": 0, "__editorExtras__": {}, "_parent": { "__id__": 7 }, + "_children": [ + { + "__id__": 89 + }, + { + "__id__": 93 + }, + { + "__id__": 97 + }, + { + "__id__": 101 + } + ], + "_active": true, + "_components": [ + { + "__id__": 105 + }, + { + "__id__": 107 + } + ], + "_prefab": { + "__id__": 109 + }, + "_lpos": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_lrot": { + "__type__": "cc.Quat", + "x": 0, + "y": 0, + "z": 0, + "w": 1 + }, + "_lscale": { + "__type__": "cc.Vec3", + "x": 1, + "y": 1, + "z": 1 + }, + "_mobility": 0, + "_layer": 1, + "_euler": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_id": "" + }, + { + "__type__": "cc.Node", + "_name": "LINE1", + "_objFlags": 0, + "__editorExtras__": {}, + "_parent": { + "__id__": 88 + }, "_children": [], "_active": true, "_components": [ { - "__id__": 89 - }, - { - "__id__": 91 + "__id__": 90 } ], "_prefab": { - "__id__": 93 + "__id__": 92 }, "_lpos": { "__type__": "cc.Vec3", @@ -2070,6 +2130,311 @@ }, "_id": "" }, + { + "__type__": "cc.UITransform", + "_name": "", + "_objFlags": 0, + "__editorExtras__": {}, + "node": { + "__id__": 89 + }, + "_enabled": true, + "__prefab": { + "__id__": 91 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 100, + "height": 100 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_id": "" + }, + { + "__type__": "cc.CompPrefabInfo", + "fileId": "b8C5CFg5lM4oUHMeSrc/sV" + }, + { + "__type__": "cc.PrefabInfo", + "root": { + "__id__": 1 + }, + "asset": { + "__id__": 0 + }, + "fileId": "4dcNJaDM1CLI9q3fIqHZ/U", + "instance": null, + "targetOverrides": null, + "nestedPrefabInstanceRoots": null + }, + { + "__type__": "cc.Node", + "_name": "HERO", + "_objFlags": 0, + "__editorExtras__": {}, + "_parent": { + "__id__": 88 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 94 + } + ], + "_prefab": { + "__id__": 96 + }, + "_lpos": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_lrot": { + "__type__": "cc.Quat", + "x": 0, + "y": 0, + "z": 0, + "w": 1 + }, + "_lscale": { + "__type__": "cc.Vec3", + "x": 1, + "y": 1, + "z": 1 + }, + "_mobility": 0, + "_layer": 1, + "_euler": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_id": "" + }, + { + "__type__": "cc.UITransform", + "_name": "", + "_objFlags": 0, + "__editorExtras__": {}, + "node": { + "__id__": 93 + }, + "_enabled": true, + "__prefab": { + "__id__": 95 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 100, + "height": 100 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_id": "" + }, + { + "__type__": "cc.CompPrefabInfo", + "fileId": "dasmpuTSVM7qA3QW9/3XjQ" + }, + { + "__type__": "cc.PrefabInfo", + "root": { + "__id__": 1 + }, + "asset": { + "__id__": 0 + }, + "fileId": "eaiasKNEFO6qvodGhL4a0b", + "instance": null, + "targetOverrides": null, + "nestedPrefabInstanceRoots": null + }, + { + "__type__": "cc.Node", + "_name": "LINE2", + "_objFlags": 0, + "__editorExtras__": {}, + "_parent": { + "__id__": 88 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 98 + } + ], + "_prefab": { + "__id__": 100 + }, + "_lpos": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_lrot": { + "__type__": "cc.Quat", + "x": 0, + "y": 0, + "z": 0, + "w": 1 + }, + "_lscale": { + "__type__": "cc.Vec3", + "x": 1, + "y": 1, + "z": 1 + }, + "_mobility": 0, + "_layer": 1, + "_euler": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_id": "" + }, + { + "__type__": "cc.UITransform", + "_name": "", + "_objFlags": 0, + "__editorExtras__": {}, + "node": { + "__id__": 97 + }, + "_enabled": true, + "__prefab": { + "__id__": 99 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 100, + "height": 100 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_id": "" + }, + { + "__type__": "cc.CompPrefabInfo", + "fileId": "ea767WaZRHo4s0bZQfN8zP" + }, + { + "__type__": "cc.PrefabInfo", + "root": { + "__id__": 1 + }, + "asset": { + "__id__": 0 + }, + "fileId": "60UdPtJn9ALLHCveLwT1bT", + "instance": null, + "targetOverrides": null, + "nestedPrefabInstanceRoots": null + }, + { + "__type__": "cc.Node", + "_name": "SKILL", + "_objFlags": 0, + "__editorExtras__": {}, + "_parent": { + "__id__": 88 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 102 + } + ], + "_prefab": { + "__id__": 104 + }, + "_lpos": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_lrot": { + "__type__": "cc.Quat", + "x": 0, + "y": 0, + "z": 0, + "w": 1 + }, + "_lscale": { + "__type__": "cc.Vec3", + "x": 1, + "y": 1, + "z": 1 + }, + "_mobility": 0, + "_layer": 1, + "_euler": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_id": "" + }, + { + "__type__": "cc.UITransform", + "_name": "", + "_objFlags": 0, + "__editorExtras__": {}, + "node": { + "__id__": 101 + }, + "_enabled": true, + "__prefab": { + "__id__": 103 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 100, + "height": 100 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_id": "" + }, + { + "__type__": "cc.CompPrefabInfo", + "fileId": "47rZJ0Uv9HkIhkIUzm2ES9" + }, + { + "__type__": "cc.PrefabInfo", + "root": { + "__id__": 1 + }, + "asset": { + "__id__": 0 + }, + "fileId": "f0bFfyrcRF77DMu5a9MYtG", + "instance": null, + "targetOverrides": null, + "nestedPrefabInstanceRoots": null + }, { "__type__": "cc.UITransform", "_name": "", @@ -2080,7 +2445,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 90 + "__id__": 106 }, "_contentSize": { "__type__": "cc.Size", @@ -2108,7 +2473,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 92 + "__id__": 108 }, "_id": "" }, @@ -2141,14 +2506,14 @@ "_active": true, "_components": [ { - "__id__": 95 + "__id__": 111 }, { - "__id__": 97 + "__id__": 113 } ], "_prefab": { - "__id__": 99 + "__id__": 115 }, "_lpos": { "__type__": "cc.Vec3", @@ -2185,11 +2550,11 @@ "_objFlags": 0, "__editorExtras__": {}, "node": { - "__id__": 94 + "__id__": 110 }, "_enabled": true, "__prefab": { - "__id__": 96 + "__id__": 112 }, "_contentSize": { "__type__": "cc.Size", @@ -2213,11 +2578,11 @@ "_objFlags": 0, "__editorExtras__": {}, "node": { - "__id__": 94 + "__id__": 110 }, "_enabled": true, "__prefab": { - "__id__": 98 + "__id__": 114 }, "light": null, "_id": "" @@ -2249,7 +2614,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 101 + "__id__": 117 }, "_contentSize": { "__type__": "cc.Size", @@ -2290,7 +2655,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 104 + "__id__": 120 }, "_contentSize": { "__type__": "cc.Size", @@ -2318,7 +2683,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 106 + "__id__": 122 }, "camera": { "__id__": 3 @@ -2333,10 +2698,10 @@ "__id__": 84 }, "entityLayer": { - "__id__": 91 + "__id__": 107 }, "SkillLayer": { - "__id__": 97 + "__id__": 113 }, "isFollowPlayer": true, "_id": "" @@ -2355,7 +2720,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 108 + "__id__": 124 }, "_id": "" }, @@ -2386,7 +2751,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 111 + "__id__": 127 }, "_contentSize": { "__type__": "cc.Size", @@ -2414,7 +2779,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 113 + "__id__": 129 }, "_cameraComponent": { "__id__": 3 @@ -2436,7 +2801,7 @@ }, "_enabled": true, "__prefab": { - "__id__": 115 + "__id__": 131 }, "_alignFlags": 45, "_target": null, diff --git a/assets/script/game/hero/Hero.ts b/assets/script/game/hero/Hero.ts index 33b7fba4..3bf5c465 100644 --- a/assets/script/game/hero/Hero.ts +++ b/assets/script/game/hero/Hero.ts @@ -46,15 +46,14 @@ export class Hero extends ecs.Entity { var prefab: Prefab = oops.res.get(path, Prefab)!; var node = instantiate(prefab); var scene = smc.map.MapView.scene; - node.parent = scene.entityLayer!.node! + node.parent = scene.entityLayer!.node!.getChildByName("HERO")!; const collider = node.getComponent(BoxCollider2D); if (collider) collider.enabled = false; // 先禁用 node.setScale(size*node.scale.x,size*node.scale.y); node.setPosition(pos) // 🔥 设置初始 SiblingIndex - 英雄基础层级 + 位置偏移 - const initialSiblingIndex = IndexSet.HERO; - node.setSiblingIndex(initialSiblingIndex); + console.log("hero",node.getSiblingIndex()); // console.log("hero load",pos) diff --git a/assets/script/game/hero/HeroMove.ts b/assets/script/game/hero/HeroMove.ts index d1f4f0fc..e3e53fa0 100644 --- a/assets/script/game/hero/HeroMove.ts +++ b/assets/script/game/hero/HeroMove.ts @@ -293,10 +293,10 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd }); // 设置渲染顺序:x坐标越大的显示在上层(index越大,层级越高) - sortedUnits.forEach((unit, index) => { - const model = unit.get(HeroViewComp); - model.node.setSiblingIndex(index); // 直接使用index,x坐标大的index大,层级高 - }); + // sortedUnits.forEach((unit, index) => { + // const model = unit.get(HeroViewComp); + // model.node.setSiblingIndex(index); // 直接使用index,x坐标大的index大,层级高 + // }); } /** 检查指定位置是否已被占用 */ diff --git a/assets/script/game/hero/HeroViewComp.ts b/assets/script/game/hero/HeroViewComp.ts index 5d064c49..c832959a 100644 --- a/assets/script/game/hero/HeroViewComp.ts +++ b/assets/script/game/hero/HeroViewComp.ts @@ -99,7 +99,7 @@ export class HeroViewComp extends CCComp { /** 初始化 UI 节点引用 */ private initUINodes() { this.top_node = this.node.getChildByName("top"); - let hp_y = this.node.getComponent(UITransform).height; + let hp_y = this.node.getComponent(UITransform).height+10; this.top_node.setPosition(0, hp_y, 0); } @@ -245,7 +245,7 @@ export class HeroViewComp extends CCComp { } /** 技能提示 */ - private tooltip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 90) { + private tooltip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 120) { let tip = ecs.getEntity(Tooltip); let pos = v3(0, 60); pos.y = pos.y + y; @@ -253,7 +253,7 @@ export class HeroViewComp extends CCComp { } /** 血量提示(伤害数字) */ - private hp_tip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 100) { + private hp_tip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 120) { let tip = ecs.getEntity(Tooltip); let x = this.node.position.x; let ny = this.node.getComponent(UITransform).height + y; diff --git a/assets/script/game/hero/Mon.ts b/assets/script/game/hero/Mon.ts index fc09b407..49573a77 100644 --- a/assets/script/game/hero/Mon.ts +++ b/assets/script/game/hero/Mon.ts @@ -42,20 +42,15 @@ export class Monster extends ecs.Entity { var path = "game/heros/"+HeroInfo[uuid].path; var prefab: Prefab = oops.res.get(path, Prefab)!; var node = instantiate(prefab); - - node.parent = scene.entityLayer!.node! + let LINE1=scene.entityLayer!.node!.getChildByName("LINE1")!; + let LINE2=scene.entityLayer!.node!.getChildByName("LINE2")!; + // 🔥 设置初始 SiblingIndex - 防止溢出 + const baseLane = lane === 0 ? LINE1 : LINE2; + node.parent = baseLane const collider = node.getComponent(BoxCollider2D); if (collider) collider.enabled = false; // 先禁用 // 延迟一帧启用碰撞体 node.setScale(size*node.scale.x,size*node.scale.y); node.setPosition(pos) - - // 🔥 设置初始 SiblingIndex - 防止溢出 - const baseLane = lane === 0 ? IndexSet.MON1 : IndexSet.MON2; - // 使用模运算防止索引溢出,确保在安全范围内循环 - const safeSpawnOrder = spawnOrder % 999; // 限制在999以内,避免溢出到其他层级 - const initialSiblingIndex = baseLane + safeSpawnOrder; - node.setSiblingIndex(initialSiblingIndex); - console.log("mon",node.getSiblingIndex()); var view = node.getComponent(HeroViewComp)!; const model = this.get(HeroAttrsComp); const skillsComp = this.get(HeroSkillsComp); @@ -71,11 +66,10 @@ export class Monster extends ecs.Entity { model.type = hero.type; model.fac = FacSet.MON; model.is_boss = monType == MonType.BOSS; - if(model.is_boss) node.setSiblingIndex(IndexSet.BOSS); + if(model.is_boss) node.parent = scene.entityLayer!.node!.getChildByName("HERO")!; if(!model.is_boss){ model.is_kalami = true; } - // 根据等级和类型获取怪物属性 const {hp, mp, ap, map, def, mdef} = getMonAttr(lv, uuid, monType); // 初始化属性数组 diff --git a/assets/script/game/map/HInfoComp.ts b/assets/script/game/map/HInfoComp.ts index e5f3467c..a6a94213 100644 --- a/assets/script/game/map/HInfoComp.ts +++ b/assets/script/game/map/HInfoComp.ts @@ -141,7 +141,7 @@ export class HInfoComp extends CCComp { // 设置节点位置 node.setPosition(this.hero_pos[pos_index]); - node.setSiblingIndex(0); + // node.setSiblingIndex(0); // 根据位置设置不同的缩放值 node.setScale(this.getHeroScale(pos_index)); // 加载并播放动画 diff --git a/assets/script/game/skill/Skill.ts b/assets/script/game/skill/Skill.ts index 8b27aa85..4ed1ea48 100644 --- a/assets/script/game/skill/Skill.ts +++ b/assets/script/game/skill/Skill.ts @@ -8,6 +8,7 @@ import { SkillView } from "./SkillView"; import { SDataCom } from "./SDataCom"; import { SMoveDataComp } from "../skill/SMoveComp"; import { HeroViewComp } from "../hero/HeroViewComp"; +import { smc } from "../common/SingletonModuleComp"; /** Skill 模块 */ @ecs.register(`Skill`) @@ -46,7 +47,8 @@ export class Skill extends ecs.Entity { // console.log("load skill startPos",startPos) const node: Node = instantiate(prefab); // console.log("load skill node",node) - node.parent = parent; + var scene = smc.map.MapView.scene; + node.parent = scene.entityLayer!.node!.getChildByName("SKILL")!; // 设置节点属性 node.setPosition(startPos);