pinball/assets/_Game/Scripts/Gacha/FlipCard.ts

69 lines
2.3 KiB
TypeScript

import { _decorator, Component, Node, Sprite } from 'cc';
import GachaBase from '../Base/GachaBase';
import SpineAnimationHandler from '../Base/SpineAnimationHandler';
import GachaManager from '../Manager/GachaManager';
const { ccclass, property } = _decorator;
@ccclass('FlipCard')
export default class FlipCard extends GachaBase {
@property(SpineAnimationHandler)
private animationHandler: SpineAnimationHandler;
@property(Node)
private spineRoot: Node;
@property(Sprite)
private cards: Sprite[] = [];
private _opened: boolean = false;
protected onLoad(): void {
this.animationHandler.setListener((_, e) => {
switch (e.data.name) {
case 'card1-active':
this.cards[0].setNodeActive(true);
break;
case 'card2-active':
this.cards[1].setNodeActive(true);
break;
case 'card3-active':
this.cards[2].setNodeActive(true);
break;
case 'card4-active':
this.cards[3].setNodeActive(true);
break;
}
});
}
protected onEnable(): void {
this._opened = false;
this.cards.forEach((card) => card.setNodeActive(false));
this.spineRoot.setActive(false);
}
public async show(): Promise<void> {
this.spineRoot.setActive(true);
await this.animationHandler.setAnimationAsync('appear');
this.animationHandler.addAnimation('idle', { loop: true });
}
public async open(event: Event, value: string) {
if (this._opened) return;
this._opened = true;
if (this.cards[+value - 1].node.active) return;
const reward = await GachaManager.instance.getReward();
if (reward) {
this.cards[+value - 1].spriteFrame = reward.icon;
await this.animationHandler.setAnimationAsync(`card${value}-active`, { trackIndex: +value });
GachaManager.instance.showFloatingText(
reward.quantity.toString(),
this.cards[+value - 1].node.worldPosition,
reward.icon,
);
GachaManager.instance.gachaDone();
}
GachaManager.instance.gachaDone();
}
}