import { JungleRewardConfig } from "./JungleTypes"; /** 将活动奖励配置转换成项目现有道具接口调用。 */ export default class JungleRewardService { public grant(ownerNode: cc.Node, rewards: JungleRewardConfig[]): void { const gameTool = cc.fx && cc.fx.GameTool; rewards.forEach((reward) => { const itemId = this.getItemId(reward); if (gameTool && typeof gameTool.getPassCheckPorp === "function") { // 所有 Jungle 奖励统一由公共方法完成缓存、后端同步和 resource_get。 gameTool.getPassCheckPorp(itemId, reward.count, false, "jungle"); } }); let canvas = cc.find("Canvas"); let shopComponents: any[] = []; if (canvas) { canvas.children.forEach((child) => { let shop: any = child.getComponent("shop"); if (shop) { shopComponents.push(shop); } }); } shopComponents.forEach((shop) => { if (shop && shop.node && shop.node.activeInHierarchy && typeof shop.refreshJungleRewardDisplay === "function") { try { shop.refreshJungleRewardDisplay(); } catch (error) { cc.warn("[JungleTreasure] 刷新商城奖励显示失败", error); } } }); let home: any = canvas && canvas.getComponent("JiaZai"); if (home) { try { if (typeof home.updateCoin === "function") { home.updateCoin(); } if (typeof home.updatePower === "function") { home.updatePower(); } } catch (error) { cc.warn("[JungleTreasure] 刷新 HomeScene 奖励显示失败", error); } } let game: any = canvas && canvas.getComponent("SceneManager"); if (game && typeof game.refreshJungleRewardDisplay === "function") { try { game.refreshJungleRewardDisplay(); } catch (error) { cc.warn("[JungleTreasure] 刷新 GameScene 奖励显示失败", error); } } ownerNode.emit("jungle-reward-granted", rewards); } private getItemId(reward: JungleRewardConfig): number { // Jungle 无限体力统一使用 3004 标识,实际时长由 reward.count 配置。 if (reward.type === "infinite_health") { return 3004; } if (typeof reward.itemId === "number") { return reward.itemId; } switch (reward.type) { case "coin": return 1001; case "freeze": return 2001; case "hammer": return 2002; case "magic_wand": return 2003; default: return 1001; } } }