Files
MatchMaster/assets/ranking_v2/WeeklyRewardAnimation.ts

45 lines
2.6 KiB
TypeScript

import { RankingReward } from './RankingData';
import RewardNumbers from './RewardNumbers';
import RankingRewardEffect from './RankingRewardEffect';
/** Locally copied battle-pass presentation; never grants inventory. */
export default class WeeklyRewardAnimation {
private static prefab: cc.Prefab;
static countPrefix: cc.SpriteFrame;
static async prepare(bundle: cc.AssetManager.Bundle): Promise<void> {
const load = <T>(path: string, type: any) => new Promise<T>((resolve, reject) =>
bundle.load(path, type, (error, asset: any) => error ? reject(error) : resolve(asset)));
const results = await Promise.all([load<cc.Prefab>('reward/RewardWindow', cc.Prefab),
load<cc.SpriteAtlas>('img/reward/publicFont', cc.SpriteAtlas),
load<cc.SpriteAtlas>('img/reward/count-prefix', cc.SpriteAtlas)]);
this.prefab = results[0] as cc.Prefab;
RewardNumbers.publicFont = results[1];
this.countPrefix = (results[2] as cc.SpriteAtlas).getSpriteFrame('x');
}
static icon(parent: cc.Node, id: string, size: number): cc.Node {
const template: any = this.prefab.data.getComponent(RankingRewardEffect);
const icons = template.rewardNode.data.getChildByName('icon');
const source = icons.getChildByName(id);
if (!source) return null;
const node = cc.instantiate(source); parent.addChild(node); node.active = true;
node.setPosition(0, 0);
node.setScale(Math.min(size / Math.max(1, node.width), size / Math.max(1, node.height)));
return node;
}
static play(parent: cc.Node, height: number, rewards: RankingReward[], mock: boolean, done: () => void): () => void {
const overlay = cc.instantiate(this.prefab); overlay.name = 'weekly-reward-animation';
parent.addChild(overlay); overlay.zIndex = 100;
const widget = overlay.getComponent(cc.Widget); if (widget) widget.enabled = false;
overlay.setContentSize(1080, height); overlay.setPosition(540, -height / 2);
let closed = false;
const cleanup = () => { if (closed) return; closed = true; if (cc.isValid(overlay, true)) overlay.destroy(); };
const effect = overlay.getComponent(RankingRewardEffect);
effect.init(rewards.map(r => ({ type: r.id, count: r.amount })), () => { cleanup(); done(); }, true);
if (mock) {
const note = new cc.Node('MockNotice'); overlay.addChild(note); note.y = -height / 2 + 130;
const label = note.addComponent(cc.Label); label.string = '模拟奖励,不计入背包'; label.fontSize = 28;
}
return cleanup;
}
}