diff --git a/assets/script/game/common/config/GameEvent.ts b/assets/script/game/common/config/GameEvent.ts index c8fa706d..eb0d0de6 100644 --- a/assets/script/game/common/config/GameEvent.ts +++ b/assets/script/game/common/config/GameEvent.ts @@ -69,4 +69,5 @@ export enum GameEvent { ToCallFriend = "ToCallFriend", CallFriend = "CallFriend", UpdateCollection = "UpdateCollection", + UpdateMissionGet = "UpdateMissionGet", } \ No newline at end of file diff --git a/assets/script/game/map/MissionCardComp.ts b/assets/script/game/map/MissionCardComp.ts index 10a199fb..06801171 100644 --- a/assets/script/game/map/MissionCardComp.ts +++ b/assets/script/game/map/MissionCardComp.ts @@ -471,6 +471,13 @@ export class MissionCardComp extends CCComp { break; } + // 记录已获取的卡牌 + oops.message.dispatchEvent(GameEvent.UpdateMissionGet, { + uuid: selectedData.uuid, + icon: selectedData.icon, + type: selectedData.type + }); + this.close(); }) .start(); diff --git a/assets/script/game/map/MissionGetsComp.ts b/assets/script/game/map/MissionGetsComp.ts index 9846f641..20481a3d 100644 --- a/assets/script/game/map/MissionGetsComp.ts +++ b/assets/script/game/map/MissionGetsComp.ts @@ -11,29 +11,72 @@ const { ccclass, property } = _decorator; @ccclass('MissionGetsCompComp') @ecs.register('MissionGetsComp', false) export class MissionGetsCompComp extends CCComp { - get_datas:any={}; - get_nodes:Node[]=[]; + get_datas: { [key: number]: { num: number, node: Node, type?: number } } = {}; + get_nodes: Node[] = []; // 图标图集缓存 private uiconsAtlas: SpriteAtlas | null = null; + + onLoad() { + oops.message.on(GameEvent.UpdateMissionGet, this.onUpdateMissionGet, this); + oops.message.on(GameEvent.MissionStart, this.onMissionStart, this); + } + start() { } onDestroy() { - + oops.message.off(GameEvent.UpdateMissionGet, this.onUpdateMissionGet, this); + oops.message.off(GameEvent.MissionStart, this.onMissionStart, this); } - load_hui(uuid:string){ + + onMissionStart(){ + // 清理旧的节点 + this.get_nodes.forEach(node => { + if (node && node.isValid) { + node.destroy(); + } + }); + this.get_nodes = []; + this.get_datas = {}; + } + + private onUpdateMissionGet(event: string, args: any) { + if (!args || !args.uuid) return; + const { uuid, icon, type } = args; + this.addGet(uuid, icon, type); + } + + addGet(uuid: number, iconName?: string, type?: number) { + if (this.get_datas[uuid]) { + this.get_datas[uuid].num++; + this.updateNodeNum(this.get_datas[uuid].node, this.get_datas[uuid].num); + } else { + this.load_hui(uuid, iconName || uuid.toString(), type); + } + } + + load_hui(uuid: number, iconName: string, type?: number){ var path = "game/gui/get"; var prefab: Prefab = oops.res.get(path, Prefab)!; + if (!prefab) { + console.warn("Prefab not found:", path); + return; + } var node = instantiate(prefab); // 将节点添加到父节点下 this.node.addChild(node); + + // 记录数据 + this.get_datas[uuid] = { num: 1, node: node, type: type }; + this.get_nodes.push(node); + node.getChildByName("num").getComponent(Label).string = "1"; const sprite = node.getChildByName("icon").getComponent(Sprite); if (!sprite) return; if (this.uiconsAtlas) { - const frame = this.uiconsAtlas.getSpriteFrame(uuid); + const frame = this.uiconsAtlas.getSpriteFrame(iconName); if (frame) { sprite.spriteFrame = frame; } @@ -41,11 +84,11 @@ export class MissionGetsCompComp extends CCComp { // 加载图集 resources.load("gui/uicons", SpriteAtlas, (err, atlas) => { if (err) { - console.error("[MissionCardComp] Failed to load uicons atlas", err); + console.error("[MissionGetsComp] Failed to load uicons atlas", err); return; } this.uiconsAtlas = atlas; - const frame = atlas.getSpriteFrame(uuid); + const frame = atlas.getSpriteFrame(iconName); if (frame) { sprite.spriteFrame = frame; } @@ -54,7 +97,10 @@ export class MissionGetsCompComp extends CCComp { } private updateNodeNum(node: Node, num: number) { - + const label = node.getChildByName("num")?.getComponent(Label); + if (label) { + label.string = num.toString(); + } } /** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */