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

83 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-07-10 20:35:04 -07:00
import { _decorator, AudioClip, Component, Label, Node, Sprite } from 'cc';
2024-06-10 06:13:32 -07:00
import GachaBase from '../Base/GachaBase';
import SpineAnimationHandler from '../Base/SpineAnimationHandler';
2024-06-18 21:35:41 -07:00
import AudioManager from '../Manager/AudioManager';
2024-06-10 06:13:32 -07:00
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[] = [];
2024-06-18 21:35:41 -07:00
@property(AudioClip)
private showSfx: AudioClip;
@property(AudioClip)
private selectCardSfx: AudioClip;
@property(AudioClip)
private openCardSfx: AudioClip;
2024-06-10 06:13:32 -07:00
private _opened: boolean = false;
protected onLoad(): void {
this.animationHandler.setListener((_, e) => {
switch (e.data.name) {
case 'card1-active':
this.cards[0].setNodeActive(true);
2024-06-18 21:35:41 -07:00
AudioManager.playSfx(this.openCardSfx);
2024-06-10 06:13:32 -07:00
break;
case 'card2-active':
this.cards[1].setNodeActive(true);
2024-06-18 21:35:41 -07:00
AudioManager.playSfx(this.openCardSfx);
2024-06-10 06:13:32 -07:00
break;
case 'card3-active':
this.cards[2].setNodeActive(true);
2024-06-18 21:35:41 -07:00
AudioManager.playSfx(this.openCardSfx);
2024-06-10 06:13:32 -07:00
break;
case 'card4-active':
this.cards[3].setNodeActive(true);
2024-06-18 21:35:41 -07:00
AudioManager.playSfx(this.openCardSfx);
2024-06-10 06:13:32 -07:00
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);
2024-06-12 04:48:19 -07:00
this.animationHandler.clearTracks();
2024-06-18 21:35:41 -07:00
AudioManager.playSfx(this.showSfx);
2024-06-10 06:13:32 -07:00
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) {
2024-06-18 21:35:41 -07:00
AudioManager.playSfx(this.selectCardSfx);
2024-06-10 06:13:32 -07:00
this.cards[+value - 1].spriteFrame = reward.icon;
2024-07-10 20:35:04 -07:00
this.cards[+value - 1].getComponentInChildren(Label).string = 'x' + reward.quantity.toString();
2024-06-10 06:13:32 -07:00
await this.animationHandler.setAnimationAsync(`card${value}-active`, { trackIndex: +value });
2024-06-12 04:48:19 -07:00
const pos = this.cards[+value - 1].node.worldPosition.clone();
pos.y += 150;
GachaManager.instance.showFloatingText(reward.quantity.toString(), pos, reward.icon);
2024-06-10 06:13:32 -07:00
GachaManager.instance.gachaDone();
2024-06-12 04:48:19 -07:00
return;
2024-06-10 06:13:32 -07:00
}
GachaManager.instance.gachaDone();
}
}