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

71 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-06-10 06:13:32 -07:00
import { _decorator, clamp01, Component, easing, game, Label, Node, Sprite, Vec3 } from 'cc';
import GachaBase from '../Base/GachaBase';
import SpineAnimationHandler from '../Base/SpineAnimationHandler';
import GachaManager 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(Sprite)
private rewardSprite: Sprite;
@property(Label)
private rewardLabel: Label;
private _opened: boolean = false;
private _startShowReward: boolean = false;
private _timer: number = 0;
protected onEnable(): void {
this.spineRoot.setActive(false);
this.rewardSprite.setNodeActive(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.rewardSprite.node.scale, Vec3.ZERO, Vec3.ONE, k);
const targetPosition = Vec3.lerp(this.rewardSprite.node.position, new Vec3(0, 150), new Vec3(0, 700), k);
this.rewardSprite.node.setScale(targetScale);
this.rewardSprite.node.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;
const reward = await GachaManager.instance.getReward();
if (reward) {
this.animationHandler.setAnimation('open');
this.rewardSprite.spriteFrame = reward.icon;
this.rewardLabel.string = reward.quantity.toString();
await Utils.delay(1);
this.rewardSprite.setNodeActive(true);
this._startShowReward = true;
return;
}
this._startShowReward = false;
GachaManager.instance.gachaDone();
}
}