refactor(common): 抽离卡牌图标处理逻辑为通用工具类

1. 新建CardIconHelper工具类统一处理卡牌图标解析和设置逻辑
2. 移除SkillBoxComp、SCardComp、EquipListComp、ItemListComp中的重复图标处理代码
3. 统一所有卡牌组件的图标加载优先级和处理流程
4. 新增卡牌背景颜色随等级切换的通用逻辑
This commit is contained in:
pan
2026-07-27 14:27:59 +08:00
parent 903ec42905
commit 354a2894e4
6 changed files with 183 additions and 95 deletions

View File

@@ -18,6 +18,7 @@ import { CardConfig } from "../common/config/CardSet";
import { SkillCardList } from "../common/config/SCardSet";
import { FieldSkillSet, SkillSet, SkillConfig } from "../common/config/SkillSet";
import { oops } from "db://oops-framework/core/Oops";
import { getCardIconId, setSpriteFrame } from "../common/CardIconHelper";
import { GameEvent } from "../common/config/GameEvent";
import { smc } from "../common/SingletonModuleComp";
import { UIID } from "../common/config/GameUIConfig";
@@ -42,7 +43,7 @@ export class SCardComp extends CCComp {
@property({ type: Node })
private icon_node: Node = null;
/** 背景节点 */
/** 背景节点(含 green/blue/purple/red/yellow 颜色子节点,按 card_lv 激活) */
@property({ type: Node })
private bg_node: Node = null;
@@ -208,6 +209,9 @@ export class SCardComp extends CCComp {
this.level_label.string = lv >= 2 ? "★".repeat(lv - 1) : "";
}
// 背景:按 card_lv 激活对应颜色子节点1→green 2→blue 3→purple 4→red 5→yellow
this.updateBgNode();
// 描述词条(最多显示 3 条,对应 fied1/fied2/fied3
const fieldLabels = [this.fied1, this.fied2, this.fied3];
const desc = this.getDescription(skillCard, skill);
@@ -217,11 +221,11 @@ export class SCardComp extends CCComp {
fieldLabels[i].node.active = i === 0 && !!desc;
}
// 图标
// 图标共享工具解析cardData.icon → FieldSkillSet → SkillSet
if (this.icon_node) {
const iconId = this.getIconId(skill);
const iconId = getCardIconId(this.cardData);
if (iconId) {
this.updateIcon(this.icon_node, iconId);
setSpriteFrame(this.icon_node, iconId);
}
}
@@ -239,6 +243,30 @@ export class SCardComp extends CCComp {
this.playEnterAnim();
}
/**
* 根据 card_lv 切换 bg_node 下的颜色子节点显示。
* 等级与颜色对应关系:
* card_lv 1 → green
* card_lv 2 → blue
* card_lv 3 → purple
* card_lv 4 → red
* card_lv 5 → yellow
*/
private updateBgNode() {
if (!this.bg_node || !this.cardData) return;
const lvToColor: Record<number, string> = {
1: "green",
2: "blue",
3: "purple",
4: "red",
5: "yellow",
};
const targetColor = lvToColor[this.cardData.card_lv ?? 1];
this.bg_node.children.forEach(child => {
child.active = (child.name === targetColor);
});
}
// ======================== 动画 ========================
/** 播放入场动画卡牌由小变大scale 0→1+ 淡入 */
@@ -346,28 +374,6 @@ export class SCardComp extends CCComp {
return skillCard?.info || skill?.info || this.cardData.info || "";
}
/** 获取技能图标 id优先用卡牌自定义 icon驻场技能取 FieldSkillSet.icon其余取 SkillSet.icon */
private getIconId(skill: SkillConfig | null): string {
if (!this.cardData) return "";
if (this.cardData.icon) return this.cardData.icon;
if (!this.cardData.skill && this.cardData.field && this.cardData.field.length > 0) {
const fieldUuid = this.cardData.field[0];
return FieldSkillSet[fieldUuid]?.icon || `${fieldUuid}`;
}
return skill?.icon || `${this.cardData.skill ?? this.cardData.uuid}`;
}
/** 更新图标精灵 */
private updateIcon(node: Node, iconId: string) {
if (!node || !iconId) return;
const sprite = node.getComponent(Sprite) || node.getComponentInChildren(Sprite);
if (!sprite) return;
if (smc.uiconsAtlas) {
const frame = smc.uiconsAtlas.getSpriteFrame(iconId);
sprite.spriteFrame = frame || null;
}
}
// ======================== 长按信息框 ========================
/** 长按时长(秒) */