74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
import { _decorator, clamp01, Component, easing, game, Label, Node, Sprite, Vec3 } from 'cc';
|
|
import GachaBase from '../Base/GachaBase';
|
|
import SpineAnimationHandler from '../Base/SpineAnimationHandler';
|
|
import GachaManager, { RewardConfig } from '../Manager/GachaManager';
|
|
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;
|
|
@property(Node)
|
|
private rewardRoot: Node;
|
|
@property(Sprite)
|
|
private rewardSprite: Sprite;
|
|
@property(Label)
|
|
private rewardLabel: Label;
|
|
|
|
private _opened: boolean = false;
|
|
private _startShowReward: boolean = false;
|
|
private _timer: number = 0;
|
|
private _reward: RewardConfig;
|
|
|
|
protected onEnable(): void {
|
|
this.spineRoot.setActive(false);
|
|
this.rewardRoot.setActive(false);
|
|
this._opened = false;
|
|
}
|
|
|
|
protected update(dt: number): void {
|
|
if (this._startShowReward) {
|
|
let k = clamp01(this._timer / 0.3);
|
|
k = easing.quintInOut(k);
|
|
const targetScale = Vec3.lerp(this.rewardRoot.scale, Vec3.ZERO, Vec3.ONE, k);
|
|
const targetPosition = Vec3.lerp(this.rewardRoot.position, new Vec3(0, 150), new Vec3(0, 700), k);
|
|
this.rewardRoot.setScale(targetScale);
|
|
this.rewardRoot.setPosition(targetPosition);
|
|
this._timer += game.deltaTime;
|
|
if (k === 1) {
|
|
this._startShowReward = false;
|
|
GachaManager.instance.gachaDone();
|
|
}
|
|
}
|
|
}
|
|
|
|
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._timer = 0;
|
|
this._opened = true;
|
|
this._reward = await GachaManager.instance.getReward();
|
|
if (this._reward) {
|
|
this.animationHandler.setAnimation('open');
|
|
this.rewardSprite.spriteFrame = this._reward.icon;
|
|
this.rewardLabel.string = this._reward.quantity.toString();
|
|
await Utils.delay(1);
|
|
this.rewardRoot.setActive(true);
|
|
this._startShowReward = true;
|
|
return;
|
|
}
|
|
|
|
this._startShowReward = false;
|
|
GachaManager.instance.gachaDone();
|
|
}
|
|
}
|