refactor(missionCard): 优化英雄槽位加载逻辑并清理废弃代码
1. 移除不再需要的showCards节点和heroPrefab引用 2. 简化ensureHeroBoxSlots方法,改为直接缓存预放置的槽位 3. 更新对应注释说明当前槽位由编辑器预创建 4. 新增装备数量显示节点的属性声明
This commit is contained in:
@@ -86,8 +86,6 @@ export class MissionCardComp extends CCComp {
|
||||
|
||||
/** 英雄抽卡面板(战斗阶段收起,准备阶段展开) */
|
||||
@property(Node)
|
||||
showCards: Node = null!
|
||||
@property(Node)
|
||||
cardPanNode: Node = null!
|
||||
@property(Node)
|
||||
cards_node: Node = null!
|
||||
@@ -108,8 +106,6 @@ export class MissionCardComp extends CCComp {
|
||||
herosPanNode: Node = null!
|
||||
@property(Node)
|
||||
herosBoxNode: Node = null!
|
||||
@property(Prefab)
|
||||
heroPrefab: Prefab = null!
|
||||
|
||||
/** 装备面板显示按钮 */
|
||||
@property(Node)
|
||||
@@ -160,6 +156,9 @@ export class MissionCardComp extends CCComp {
|
||||
/** 英雄数量显示节点(含 icon + num 子节点) */
|
||||
@property(Node)
|
||||
hero_num_node: Node = null!
|
||||
/** 装备数量显示节点(含 icon + num 子节点) */
|
||||
@property(Node)
|
||||
refresh_stone_num_node: Node = null!
|
||||
|
||||
|
||||
// ======================== 运行时状态 ========================
|
||||
@@ -240,9 +239,6 @@ export class MissionCardComp extends CCComp {
|
||||
if (this.showHeros && this.showHeros.isValid) {
|
||||
this.showHeros.off(NodeEventType.TOUCH_END, this.onShowHerosClick, this);
|
||||
}
|
||||
if (this.showCards && this.showCards.isValid) {
|
||||
this.showCards.off(NodeEventType.TOUCH_END, this.onShowCardsClick, this);
|
||||
}
|
||||
if (this.closeCards && this.closeCards.isValid) {
|
||||
this.closeCards.off(NodeEventType.TOUCH_END, this.onCloseCardsClick, this);
|
||||
}
|
||||
@@ -391,8 +387,7 @@ export class MissionCardComp extends CCComp {
|
||||
this.cards_chou?.on(NodeEventType.TOUCH_END, this.onDrawTouchEnd, this);
|
||||
this.cards_chou?.on(NodeEventType.TOUCH_CANCEL, this.onDrawTouchCancel, this);
|
||||
|
||||
/** 显示卡牌面板按钮 */
|
||||
this.showCards?.on(NodeEventType.TOUCH_END, this.onShowCardsClick, this);
|
||||
|
||||
/** 隐藏卡牌面板按钮 */
|
||||
this.closeCards?.on(NodeEventType.TOUCH_END, this.onCloseCardsClick, this);
|
||||
|
||||
@@ -641,9 +636,7 @@ export class MissionCardComp extends CCComp {
|
||||
if (this.showHeros && this.showHeros.isValid) {
|
||||
this.showHeros.off(NodeEventType.TOUCH_END, this.onShowHerosClick, this);
|
||||
}
|
||||
if (this.showCards && this.showCards.isValid) {
|
||||
this.showCards.off(NodeEventType.TOUCH_END, this.onShowCardsClick, this);
|
||||
}
|
||||
|
||||
if (this.closeCards && this.closeCards.isValid) {
|
||||
this.closeCards.off(NodeEventType.TOUCH_END, this.onCloseCardsClick, this);
|
||||
}
|
||||
@@ -968,7 +961,7 @@ export class MissionCardComp extends CCComp {
|
||||
|
||||
/**
|
||||
* 同步英雄信息盒槽位与场上英雄:
|
||||
* 1. 确保 herosBoxNode 下存在 3 个 HeroBoxComp 槽位(复用 + 实例化补充)。
|
||||
* 1. 刷新 herosBoxNode 下 HeroBoxComp 槽位缓存(编辑器已预放 3 个)。
|
||||
* 2. 查询当前存活英雄列表,按序绑定到槽位。
|
||||
* 3. 多余槽位显示空英雄信息。
|
||||
*
|
||||
@@ -977,7 +970,7 @@ export class MissionCardComp extends CCComp {
|
||||
private refreshHeroBoxSlots() {
|
||||
if (!this.herosBoxNode || !this.herosBoxNode.isValid) return;
|
||||
|
||||
this.ensureHeroBoxSlots(3);
|
||||
this.ensureHeroBoxSlots();
|
||||
|
||||
// 查询场上存活英雄(含复活中的实体,按 eid 升序保证稳定顺序)
|
||||
const actors: Array<{ eid: number, model: HeroAttrsComp }> = [];
|
||||
@@ -1005,28 +998,15 @@ export class MissionCardComp extends CCComp {
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保英雄信息盒槽位数量足够。
|
||||
* 优先复用 herosBoxNode 现有子节点,不足时从 heroPrefab 实例化补充。
|
||||
* 缓存英雄信息盒槽位组件引用。
|
||||
* 槽位已在编辑器中预先放置(herosBoxNode 下挂 HeroBoxComp),
|
||||
* 运行时仅刷新缓存,不再动态实例化。
|
||||
*/
|
||||
private ensureHeroBoxSlots(targetCount: number) {
|
||||
private ensureHeroBoxSlots() {
|
||||
if (!this.herosBoxNode || !this.herosBoxNode.isValid) return;
|
||||
|
||||
// 刷新缓存
|
||||
this.heroBoxComps = this.herosBoxNode.children
|
||||
.map(node => node.getComponent(HeroBoxComp))
|
||||
.filter((comp): comp is HeroBoxComp => !!comp);
|
||||
|
||||
// 补充实例化不足的槽位
|
||||
while (this.heroBoxComps.length < targetCount) {
|
||||
if (!this.heroPrefab) break;
|
||||
const node = instantiate(this.heroPrefab);
|
||||
// 先禁用根节点 Widget 再挂到父节点,避免 lateUpdate 覆盖 position
|
||||
const widget = node.getComponent(Widget);
|
||||
if (widget) widget.enabled = false;
|
||||
this.herosBoxNode.addChild(node);
|
||||
const comp = node.getComponent(HeroBoxComp) || node.addComponent(HeroBoxComp);
|
||||
this.heroBoxComps.push(comp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user