squidGame/tgx-games-client/assets/module_arean/level3/script/RewardManager.ts

98 lines
3.5 KiB
TypeScript

import { _decorator, Component, Node, Prefab, instantiate, director } from 'cc';
import { resLoader } from 'db://assets/core_tgx/base/utils/ResLoader';
import { ModuleDef } from 'db://assets/scripts/ModuleDef';
import { RewardChest } from './RewardChest';
import { RewardCard } from './RewardCard';
const { ccclass, property } = _decorator;
// 卡牌数据接口
interface CardData {
id: number;
name: string;
desc: string;
icon: string;
}
@ccclass('RewardManager')
export class RewardManager {
private static _instance: RewardManager = null;
private guiNode: Node = null;
// 卡牌配置数据
private readonly cardList: CardData[] = [
{ id: 1, name: "加速卡", desc: "提升移动速度30%", icon: "card_speed" },
{ id: 2, name: "力量卡", desc: "提升攻击力20%", icon: "card_power" },
{ id: 3, name: "护盾卡", desc: "获得一个护盾", icon: "card_shield" },
{ id: 4, name: "回复卡", desc: "恢复2颗弹珠", icon: "card_heal" }
];
public static get instance(): RewardManager {
if (!this._instance) {
this._instance = new RewardManager();
}
return this._instance;
}
// 初始化方法
public init(guiNode: Node) {
this.guiNode = guiNode;
}
// 显示宝箱奖励
public showRewardChest(callback?: () => void) {
resLoader.load(ModuleDef.Arean, 'common/prefabs/reward_chest', (err: Error | null, prefab: Prefab) => {
if (!err && prefab && this.guiNode) {
const rewardNode = instantiate(prefab);
this.guiNode.addChild(rewardNode);
// 点击宝箱显示卡牌
const chestBtn = rewardNode.getChildByPath("chest_container/chest_img");
if (chestBtn) {
chestBtn.once(Node.EventType.TOUCH_START, () => {
// 播放宝箱打开动画
const chestComp = rewardNode.getComponent(RewardChest);
if (chestComp) {
chestComp.playOpenAnimation();
}
// 显示随机卡牌
this.showRandomCard(rewardNode, callback);
});
}
}
});
}
// 显示随机卡牌
private showRandomCard(rewardNode: Node, callback?: () => void) {
// 随机选择一张卡牌
const randomCard = this.cardList[Math.floor(Math.random() * this.cardList.length)];
resLoader.load(ModuleDef.Arean, 'common/prefabs/reward_card', (err: Error | null, prefab: Prefab) => {
if (!err && prefab) {
const cardNode = instantiate(prefab);
rewardNode.addChild(cardNode);
// 设置卡牌数据
const cardComp = cardNode.getComponent(RewardCard);
if (cardComp) {
cardComp.setCardData(randomCard);
}
// 点击任意位置关闭
const maskNode = rewardNode.getChildByPath("mask");
if (maskNode) {
maskNode.once(Node.EventType.TOUCH_START, () => {
rewardNode.destroy();
if (callback) callback();
});
}
}
});
}
// 保存获得的卡牌
private saveCardReward(cardId: number) {
// TODO: 实现卡牌保存逻辑
console.log('获得卡牌:', cardId);
}
}