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

123 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-06-10 21:35:13 -07:00
import {
_decorator,
CCInteger,
CCString,
Component,
game,
Node,
random,
randomRangeInt,
RealCurve,
sp,
Sprite,
} from 'cc';
2024-06-10 06:13:32 -07:00
import GachaBase from '../Base/GachaBase';
import SpineAnimationHandler from '../Base/SpineAnimationHandler';
2024-06-10 21:35:13 -07:00
import GachaManager, { RewardConfig } from '../Manager/GachaManager';
2024-06-10 06:13:32 -07:00
const { ccclass, property } = _decorator;
2024-06-10 21:35:13 -07:00
class RewardCard {
public node: Node;
public reward: RewardConfig;
constructor(node: Node, reward: RewardConfig) {
this.node = node;
this.reward = reward;
}
public setActive(value: boolean) {
this.node.setActive(value);
}
}
2024-06-10 06:13:32 -07:00
@ccclass('LuckyWheel')
export default class LuckyWheel extends GachaBase {
@property(SpineAnimationHandler)
private animationHandler: SpineAnimationHandler;
@property(Node)
private spineRoot: Node;
@property(CCString)
private wheelBoneName: string = '';
@property(CCInteger)
private speed: number = 1;
@property(RealCurve)
private spinCurve: RealCurve = new RealCurve();
@property(Sprite)
private sprites: Sprite[] = [];
private _wheel: sp.spine.Bone;
private _targetAngle: number = 0;
private _spinning: boolean = false;
private _timer: number = 0;
private _timeSpin: number = 0;
private _maxAngle: number = 0;
2024-06-10 21:35:13 -07:00
private _allCards: RewardCard[];
private _random: number;
2024-06-10 06:13:32 -07:00
protected onLoad(): void {
this._wheel = this.animationHandler.findBone(this.wheelBoneName);
}
protected onEnable(): void {
2024-06-10 21:35:13 -07:00
this.setReward();
2024-06-10 06:13:32 -07:00
this.spineRoot.setActive(false);
this._wheel.rotation = 0;
this._wheel.update();
}
protected update(): void {
if (this._spinning) {
this._timer += game.deltaTime * this.speed;
const angle = this._maxAngle * this.spinCurve.evaluate(this._timer / this._timeSpin);
this._wheel.rotation = this._targetAngle + angle;
this._wheel.update();
if (this._timer >= this._timeSpin) {
this._spinning = false;
2024-06-10 21:35:13 -07:00
GachaManager.instance.showFloatingText(
this._allCards[this._random].reward.quantity.toString(),
this.node.worldPosition,
this._allCards[this._random].reward.icon,
);
GachaManager.instance.setReward(this._allCards[this._random].reward.id);
2024-06-10 06:13:32 -07:00
}
}
}
2024-06-10 21:35:13 -07:00
private setReward() {
this._allCards = this.sprites.map((card) => {
const rw = GachaManager.instance.getRandomReward([60, 35, 5]);
card.spriteFrame = rw.icon;
return new RewardCard(card.node, rw);
});
}
2024-06-10 06:13:32 -07:00
public async show(): Promise<void> {
for (let i = 0; i < GachaManager.instance.rewards.length; i++) {
this.sprites[i].spriteFrame = GachaManager.instance.rewards[i].icon;
}
this.spineRoot.setActive(true);
await this.animationHandler.setAnimationAsync('appear');
this.animationHandler.addAnimation('idle', { loop: true });
}
public async spin() {
if (this._spinning) return;
this._spinning = true;
2024-06-10 21:35:13 -07:00
this._random = this._allCards.getRandomIndex();
if (this._random) {
2024-06-10 06:13:32 -07:00
this.animationHandler.clearTrack(0);
2024-06-10 21:35:13 -07:00
this._targetAngle = -36 * this._random;
2024-06-10 06:13:32 -07:00
this._timer = 0;
this._timeSpin = randomRangeInt(10, 15);
this._maxAngle = 360 * this._timeSpin;
return;
}
this._spinning = true;
2024-06-10 21:35:13 -07:00
GachaManager.instance.setReward(this._allCards[this._random].reward.id);
2024-06-10 06:13:32 -07:00
}
}