feat(地图): 添加卡片类型枚举并支持多种卡片数据

扩展MissionCardComp以支持多种卡片类型,添加CardType枚举
修改卡片数据处理逻辑,使其不局限于天赋类型
为后续添加技能和药水卡片类型预留扩展点
This commit is contained in:
walkpan
2026-01-04 20:23:23 +08:00
parent 71026ae9a5
commit 5648c5fbe2

View File

@@ -7,6 +7,12 @@ import { talConf, ItalConf } from "../common/config/TalSet";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
export enum CardType {
Talent = 1,
Skill = 2,
Potion = 3
}
/** 视图层对象 */ /** 视图层对象 */
@ccclass('MissionCardComp') @ccclass('MissionCardComp')
@ecs.register('MissionCard', false) @ecs.register('MissionCard', false)
@@ -21,10 +27,13 @@ export class MissionCardComp extends CCComp {
@property(Node) @property(Node)
card4:Node = null! card4:Node = null!
card1_data:ItalConf = null! card1_data:any = null!
card2_data:ItalConf = null! card2_data:any = null!
card3_data:ItalConf = null! card3_data:any = null!
card4_data:ItalConf = null! card4_data:any = null!
// 当前卡片类型
curCardType: CardType = CardType.Talent;
onLoad() { onLoad() {
oops.message.on(GameEvent.TalentSelect, this.onTalentSelect, this); oops.message.on(GameEvent.TalentSelect, this.onTalentSelect, this);
@@ -57,15 +66,23 @@ export class MissionCardComp extends CCComp {
private onTalentSelect(event: string, args: any) { private onTalentSelect(event: string, args: any) {
this.node.active = true; this.node.active = true;
this.hasSelected = false; // 重置选择状态 this.hasSelected = false; // 重置选择状态
this.curCardType = CardType.Talent; // 记录当前类型为天赋
this.resetCardStates(); // 每次刷新前重置卡片状态 this.resetCardStates(); // 每次刷新前重置卡片状态
this.refCards(); this.refCards();
} }
refCards(){ refCards(){
// 随机获取4个不一样的天赋 // 根据当前类型获取数据
const allTalents = Object.values(talConf); let allData: any[] = [];
const result: ItalConf[] = [];
const temp = [...allTalents]; if (this.curCardType === CardType.Talent) {
allData = Object.values(talConf);
}
// 后续扩展其他类型
// else if (this.curCardType === CardType.Skill) { ... }
const result: any[] = [];
const temp = [...allData];
// 简单的随机抽取算法 // 简单的随机抽取算法
for (let i = 0; i < 4 && temp.length > 0; i++) { for (let i = 0; i < 4 && temp.length > 0; i++) {
@@ -81,7 +98,7 @@ export class MissionCardComp extends CCComp {
if (result.length > 3) this.updateCardData(4, result[3]); if (result.length > 3) this.updateCardData(4, result[3]);
} }
updateCardInfo(card:Node, data:ItalConf){ updateCardInfo(card:Node, data:any){
if(!card) return if(!card) return
card.active = true; card.active = true;
// 隐藏选中状态 // 隐藏选中状态
@@ -94,11 +111,12 @@ export class MissionCardComp extends CCComp {
} }
let info = card.getChildByName("info")?.getChildByName("Label") let info = card.getChildByName("info")?.getChildByName("Label")
if(info){ if(info){
info.getComponent(Label)!.string = data.desc // 根据类型显示不同描述,目前天赋用desc
info.getComponent(Label)!.string = data.desc || "";
} }
} }
updateCardData(index:number, data:ItalConf){ updateCardData(index:number, data:any){
switch (index) { switch (index) {
case 1: case 1:
this.card1_data = data this.card1_data = data
@@ -125,38 +143,41 @@ export class MissionCardComp extends CCComp {
// 如果已经选择过,则不再处理 // 如果已经选择过,则不再处理
if(this.hasSelected) return; if(this.hasSelected) return;
let selectedTalent: ItalConf | null = null; let selectedData: any = null;
let selectedCardNode: Node | null = null; let selectedCardNode: Node | null = null;
switch (_index) { switch (_index) {
case 1: case 1:
selectedTalent = this.card1_data; selectedData = this.card1_data;
selectedCardNode = this.card1; selectedCardNode = this.card1;
break; break;
case 2: case 2:
selectedTalent = this.card2_data; selectedData = this.card2_data;
selectedCardNode = this.card2; selectedCardNode = this.card2;
break; break;
case 3: case 3:
selectedTalent = this.card3_data; selectedData = this.card3_data;
selectedCardNode = this.card3; selectedCardNode = this.card3;
break; break;
case 4: case 4:
selectedTalent = this.card4_data; selectedData = this.card4_data;
selectedCardNode = this.card4; selectedCardNode = this.card4;
break; break;
} }
if (selectedTalent && selectedCardNode) { if (selectedData && selectedCardNode) {
this.hasSelected = true; this.hasSelected = true;
console.log("选择天赋:", selectedTalent.name); console.log("选择卡片:", selectedData.name, "类型:", this.curCardType);
// 显示当前选中的 selected 节点 // 显示当前选中的 selected 节点
const selected = selectedCardNode.getChildByName("selected"); const selected = selectedCardNode.getChildByName("selected");
if(selected) selected.active = true; if(selected) selected.active = true;
// 发送事件 // 根据类型发送不同事件
oops.message.dispatchEvent(GameEvent.UseTalentCard, selectedTalent.uuid); if (this.curCardType === CardType.Talent) {
oops.message.dispatchEvent(GameEvent.UseTalentCard, selectedData.uuid);
}
// 后续扩展其他类型事件
// 延迟关闭界面,让玩家看到选中效果 // 延迟关闭界面,让玩家看到选中效果
// this.scheduleOnce(() => { // this.scheduleOnce(() => {