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

72 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

2024-06-18 21:35:41 -07:00
import { _decorator, AudioClip, clamp01, Component, easing, game, Label, Node, Sprite, Vec3 } 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 21:35:13 -07:00
import GachaManager, { RewardConfig } from '../Manager/GachaManager';
2024-06-10 06:13:32 -07:00
import Utils from '../Utilities';
const { ccclass, property } = _decorator;
@ccclass('FreeReward')
export default class FreeReward extends GachaBase {
@property(SpineAnimationHandler)
private animationHandler: SpineAnimationHandler;
@property(Node)
private spineRoot: Node;
2024-06-10 23:38:06 -07:00
@property(Node)
private rewardRoot: Node;
2024-06-10 06:13:32 -07:00
@property(Sprite)
private rewardSprite: Sprite;
@property(Label)
private rewardLabel: Label;
2024-06-18 21:35:41 -07:00
@property(AudioClip)
private openSound: AudioClip;
2024-06-10 06:13:32 -07:00
private _opened: boolean = false;
2024-06-10 21:35:13 -07:00
private _reward: RewardConfig;
2024-06-10 06:13:32 -07:00
2024-06-12 04:48:19 -07:00
protected onLoad(): void {
this.animationHandler.setListener((_, e) => {
switch (e.data.name) {
case 'open':
this.rewardRoot.setActive(true);
break;
}
});
}
2024-06-10 06:13:32 -07:00
protected onEnable(): void {
this.spineRoot.setActive(false);
2024-06-10 23:38:06 -07:00
this.rewardRoot.setActive(false);
2024-06-10 06:13:32 -07:00
this._opened = false;
}
public async show() {
this.spineRoot.setActive(true);
await this.animationHandler.setAnimationAsync('appear');
this.animationHandler.addAnimation('idle', { loop: true });
}
public async open() {
if (this._opened) return;
this._opened = true;
2024-06-10 21:35:13 -07:00
this._reward = await GachaManager.instance.getReward();
if (this._reward) {
2024-06-18 21:35:41 -07:00
AudioManager.playSfx(this.openSound);
2024-06-10 21:35:13 -07:00
this.rewardSprite.spriteFrame = this._reward.icon;
2024-06-12 04:48:19 -07:00
this.rewardLabel.string = 'x' + this._reward.quantity;
await this.animationHandler.setAnimationAsync('open');
2024-06-10 23:38:06 -07:00
this.rewardRoot.setActive(true);
2024-06-18 21:35:41 -07:00
GachaManager.instance.showFloatingText(
this._reward.quantity.toString(),
this.node.worldPosition,
this._reward.icon,
);
2024-06-12 04:48:19 -07:00
GachaManager.instance.gachaDone();
2024-06-10 06:13:32 -07:00
return;
}
GachaManager.instance.gachaDone();
}
}