RewardData.ts 831 B

123456789101112131415161718192021222324252627282930313233
  1. import { _decorator, Component, Node, SpriteFrame } from 'cc';
  2. export interface RewardItem {
  3. id: string;
  4. name: string;
  5. spriteFrame: SpriteFrame | null;
  6. unlocked: boolean;
  7. }
  8. export class RewardData {
  9. private rewards: Map<string, RewardItem> = new Map();
  10. public addReward(reward: RewardItem) {
  11. this.rewards.set(reward.id, reward);
  12. }
  13. public getRewardById(id: string): RewardItem | undefined {
  14. return this.rewards.get(id);
  15. }
  16. public getAllRewards(): RewardItem[] {
  17. return Array.from(this.rewards.values());
  18. }
  19. public unlockReward(id: string): boolean {
  20. const reward = this.rewards.get(id);
  21. if (reward && !reward.unlocked) {
  22. reward.unlocked = true;
  23. return true;
  24. }
  25. return false;
  26. }
  27. }