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

126 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-06-14 04:22:53 -07:00
import { _decorator, AudioClip, Component, Label, Node, sp, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
2024-06-10 06:13:32 -07:00
import GachaBase from '../Base/GachaBase';
import SpineAnimationHandler, { SocketPath, SpineAnimation, SpineSkin } from '../Base/SpineAnimationHandler';
2024-06-14 04:22:53 -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;
2024-06-10 21:35:13 -07:00
class RewardCard {
2024-06-10 06:13:32 -07:00
public socket: sp.SpineSocket;
public node: Node;
2024-06-10 21:35:13 -07:00
public reward: RewardConfig;
2024-06-10 06:13:32 -07:00
2024-06-10 21:35:13 -07:00
constructor(node: Node, reward: RewardConfig) {
2024-06-10 06:13:32 -07:00
this.node = node;
2024-06-10 21:35:13 -07:00
this.reward = reward;
2024-06-10 06:13:32 -07:00
}
public setActive(value: boolean) {
this.node.setActive(value);
}
}
@ccclass('LuckyChain')
export default class LuckyChain extends GachaBase {
@property(SpineAnimationHandler)
private animationHandler: SpineAnimationHandler;
@property(Node)
private spriteRoot: Node;
@property(Node)
private cards: Node[] = [];
2024-06-18 04:34:07 -07:00
@property(Sprite)
private sprites: Sprite[] = [];
2024-06-10 06:13:32 -07:00
@property({ type: SocketPath })
private cardSlotPath: SocketPath[] = [];
2024-06-14 04:22:53 -07:00
@property(AudioClip)
private sounds: AudioClip[] = [];
2024-06-10 06:13:32 -07:00
2024-06-10 21:35:13 -07:00
private _activeCards: RewardCard[] = [];
private _allCards: RewardCard[] = [];
private _reward: RewardCard;
2024-06-10 06:13:32 -07:00
onFocusInEditor(): void {
this.animationHandler.setPropertySocketPath(this, 'cardSlotPath');
}
protected onLoad(): void {
this.setReward();
for (let i = 0; i < 4; i++) {
const card = this._allCards.shift();
const path = this.cardSlotPath[i];
const socket = this.animationHandler.addSocket(path, card.node);
card.socket = socket;
this._activeCards.push(card);
}
this.animationHandler.setListener((_, e) => {
switch (e.data.name) {
2024-06-12 04:48:19 -07:00
case 'card-appear':
2024-06-14 04:22:53 -07:00
AudioManager.playSfx(this.sounds[0]);
2024-06-12 04:48:19 -07:00
this._activeCards.forEach((card) => {
card.setActive(true);
});
break;
2024-06-10 06:13:32 -07:00
case 'card-claimed':
2024-06-14 04:22:53 -07:00
AudioManager.playSfx(this.sounds[1]);
2024-06-10 06:13:32 -07:00
const cardRemove = this._activeCards.shift();
2024-06-10 21:35:13 -07:00
this._reward = cardRemove;
2024-06-10 06:13:32 -07:00
this.animationHandler.removeSocket(cardRemove.socket);
cardRemove.setActive(false);
cardRemove.socket = null;
this._allCards.push(cardRemove);
break;
case 'new-card-spawned':
2024-06-14 04:22:53 -07:00
AudioManager.playSfx(this.sounds[2]);
2024-06-10 06:13:32 -07:00
const card = this._allCards.shift();
const path = this.cardSlotPath[this.cardSlotPath.length - 1];
const socket = this.animationHandler.addSocket(path, card.node);
card.setActive(true);
card.socket = socket;
this._activeCards.push(card);
break;
}
});
}
protected onEnable(): void {
this.spriteRoot.setActive(false);
2024-06-12 04:48:19 -07:00
this._activeCards.forEach((card) => {
card.setActive(false);
});
2024-06-10 06:13:32 -07:00
}
private setReward() {
2024-06-18 04:34:07 -07:00
this._allCards = this.cards.map((card, i) => {
2024-06-12 04:48:19 -07:00
const rw = GachaManager.instance.getRandomReward();
2024-06-18 04:34:07 -07:00
this.sprites[i].spriteFrame = rw.icon;
2024-06-12 04:48:19 -07:00
card.getComponentInChildren(Label).string = 'x' + rw.quantity;
2024-06-10 06:13:32 -07:00
card.setActive(false);
2024-06-10 21:35:13 -07:00
return new RewardCard(card, rw);
2024-06-10 06:13:32 -07:00
});
}
public async show(): Promise<void> {
this.spriteRoot.setActive(true);
await this.animationHandler.setAnimationAsync('appear');
await this.animationHandler.addAnimationAsync('active');
2024-06-10 21:35:13 -07:00
GachaManager.instance.showFloatingText(
this._reward.reward.quantity.toString(),
this.node.worldPosition,
this._reward.reward.icon,
);
GachaManager.instance.setReward(this._reward.reward.id);
2024-06-10 06:13:32 -07:00
await Utils.delay(1.5);
//update socket path
for (let i = 0; i < 4; i++) {
const card = this._activeCards[i];
const path = this.cardSlotPath[i];
this._activeCards[i].socket = this.animationHandler.updateSocketPath(card.socket, path);
}
}
}