pinball/assets/_Game/Scripts/Manager/GachaManager.ts

139 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-06-10 06:13:32 -07:00
import {
_decorator,
CCInteger,
CCString,
clamp01,
Component,
easing,
Enum,
game,
lerp,
Node,
SpriteFrame,
tween,
UIOpacity,
} from 'cc';
import GachaBase from '../Base/GachaBase';
import GameEvent from '../Events/GameEvent';
import Singleton from '../Singleton';
import Utils from '../Utilities';
import { EventManger } from './EventManger';
const { ccclass, property } = _decorator;
export enum GachaType {
FreeReward,
LuckyWheel,
LuckyChain,
FlipCard,
}
Enum(GachaType);
export enum RewardType {
Oxi,
Gas,
Star,
Shield,
ScoreX2,
Magnet,
}
Enum(RewardType);
@ccclass('Gacha')
class Gacha {
@property(CCString)
public id: string = '';
@property({ type: GachaType })
public type: GachaType = GachaType.FreeReward;
@property(GachaBase)
public gacha: GachaBase;
}
2024-06-10 08:05:56 -07:00
@ccclass('RewardConfig')
2024-06-10 06:13:32 -07:00
class RewardConfig {
@property(CCString)
public id: string = '';
@property({ type: RewardType })
public type: RewardType = RewardType.Oxi;
@property(SpriteFrame)
public icon: SpriteFrame;
@property(CCInteger)
public quantity: number;
}
@ccclass('GachaManager')
export default class GachaManager extends Singleton<GachaManager>() {
@property(CCString)
public gachaId: string;
@property(UIOpacity)
private container: UIOpacity;
@property(Gacha)
private gachas: Gacha[] = [];
@property(RewardConfig)
public rewards: RewardConfig[] = [];
private _reward: RewardConfig;
private _showing: boolean = false;
private _showTimer: number = 0;
private _idType: string;
protected onLoad(): void {
super.onLoad();
this.container.setNodeActive(false);
}
public show(idType: string) {
game.timeScale = 0;
this.container.setNodeActive(true);
this._showTimer = 0;
this._showing = true;
this._idType = idType;
}
protected update(dt: number): void {
if (this._showing) {
this._showTimer += game.deltaTime;
const k = easing.smooth(clamp01(this._showTimer / 0.2));
this.container.opacity = lerp(0, 255, k);
if (k === 1) {
this._showing = false;
this.showGacha();
}
}
}
private showGacha() {
const gacha = this.gachas.find((gacha) => gacha.id == this._idType);
gacha.gacha.show();
}
public async getReward(): Promise<RewardConfig> {
this._reward = this.getRandomReward();
return this._reward;
}
public getRewardIndex(reward: RewardConfig): number {
return this.rewards.indexOf(reward);
}
public async gachaDone() {
console.log(`Gacha reward: ${RewardType[this._reward.type]} quantity: ${this._reward.quantity}`);
EventManger.instance.emit(GameEvent.GachaReward, [this._reward.type, this._reward.quantity]);
await Utils.delay(1);
game.timeScale = 1;
tween(this.container)
.to(0.1, { opacity: 0 })
.call(() => this.container.setNodeActive(false))
.start();
}
public getRandomReward() {
return this.rewards.getRandom();
}
public setReward(id: string) {
this._reward = this.rewards.find((r) => r.id == id);
this.gachaDone();
}
}