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

202 lines
5.9 KiB
TypeScript
Raw Normal View History

2024-06-10 06:13:32 -07:00
import {
_decorator,
CCInteger,
CCString,
clamp01,
Component,
easing,
Enum,
game,
2024-06-17 04:07:20 -07:00
Label,
2024-06-10 06:13:32 -07:00
lerp,
Node,
SpriteFrame,
tween,
UIOpacity,
2024-06-10 21:35:13 -07:00
Vec3,
2024-06-10 06:13:32 -07:00
} from 'cc';
2024-06-12 04:48:19 -07:00
import { EDITOR, PREVIEW } from 'cc/env';
2024-06-10 06:13:32 -07:00
import GachaBase from '../Base/GachaBase';
2024-06-12 04:48:19 -07:00
import BoosterType from '../Enum/BoosterType';
2024-06-10 21:35:13 -07:00
import ScoreType from '../Enum/ScoreType';
2024-06-10 06:13:32 -07:00
import GameEvent from '../Events/GameEvent';
2024-06-10 21:35:13 -07:00
import FloatingTextFactory from '../Factory/FloatingTextFactory';
2024-06-12 04:48:19 -07:00
import P4PSDK from '../P4PSDK';
2024-06-10 06:13:32 -07:00
import Singleton from '../Singleton';
import Utils from '../Utilities';
import { EventManger } from './EventManger';
2024-06-10 21:35:13 -07:00
import { GameManager } from './GameManager';
2024-06-12 04:48:19 -07:00
import { SpawnObjectManager } from './SpawnObjectManager';
2024-06-10 06:13:32 -07:00
const { ccclass, property } = _decorator;
export enum GachaType {
FreeReward,
LuckyWheel,
LuckyChain,
FlipCard,
}
Enum(GachaType);
export enum RewardType {
Star,
2024-06-10 21:35:13 -07:00
Time,
Cheese,
2024-06-12 04:48:19 -07:00
Ball,
2024-06-10 06:13:32 -07:00
}
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 21:35:13 -07:00
export class RewardConfig {
2024-06-10 06:13:32 -07:00
@property(CCString)
public id: string = '';
@property({ type: RewardType })
2024-06-10 21:35:13 -07:00
public type: RewardType = RewardType.Star;
2024-06-10 06:13:32 -07:00
@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;
2024-06-10 21:35:13 -07:00
@property(FloatingTextFactory)
private floatingScoreFactory: FloatingTextFactory;
2024-06-17 04:07:20 -07:00
@property(Label)
private countDownLabel: Label;
2024-06-10 06:13:32 -07:00
@property(Gacha)
private gachas: Gacha[] = [];
@property(RewardConfig)
public rewards: RewardConfig[] = [];
private _reward: RewardConfig;
private _showing: boolean = false;
private _showTimer: number = 0;
2024-06-10 21:35:13 -07:00
private _idType: GachaType;
2024-06-17 04:07:20 -07:00
private _currentGacha: GachaBase;
2024-06-10 06:13:32 -07:00
protected onLoad(): void {
super.onLoad();
this.container.setNodeActive(false);
2024-06-17 04:07:20 -07:00
this.countDownLabel.setNodeActive(false);
this.gachas.forEach((gacha) => gacha.gacha.setNodeActive(false));
2024-06-10 06:13:32 -07:00
}
2024-06-17 20:36:33 -07:00
public showBtn(_, data: string) {
this.show(GachaType[data]);
}
2024-06-10 21:35:13 -07:00
public async show(type: GachaType, delay: number = 0) {
2024-06-10 06:13:32 -07:00
game.timeScale = 0;
2024-06-10 21:35:13 -07:00
await Utils.delay(delay);
2024-06-10 06:13:32 -07:00
this.container.setNodeActive(true);
this._showTimer = 0;
this._showing = true;
2024-06-10 21:35:13 -07:00
this._idType = type;
2024-06-10 06:13:32 -07:00
}
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();
}
}
}
2024-06-10 21:35:13 -07:00
public showFloatingText(text: string, position: Vec3, icon: SpriteFrame) {
const floatingText = this.floatingScoreFactory.create(this.node);
floatingText.show(`+${text}`, position, 3, 1, icon);
}
2024-06-10 06:13:32 -07:00
private showGacha() {
2024-06-17 04:07:20 -07:00
this._currentGacha = this.gachas.find((gacha) => gacha.type == this._idType).gacha;
this._currentGacha.setNodeActive(true);
this._currentGacha.show();
2024-06-10 06:13:32 -07:00
}
public async getReward(): Promise<RewardConfig> {
2024-06-13 04:15:18 -07:00
// if (!EDITOR && !PREVIEW) {
// try {
// const rw = await P4PSDK.spinGacha(this.gachaId);
// this._reward = this.rewards.find((r) => r.id == rw.id);
// } catch (error) {
// console.log(error);
// return null;
// }
// } else {
// }
this._reward = this.getRandomReward();
2024-06-10 06:13:32 -07:00
return this._reward;
}
public getRewardIndex(reward: RewardConfig): number {
return this.rewards.indexOf(reward);
}
public async gachaDone() {
await Utils.delay(1);
2024-06-17 04:07:20 -07:00
this._currentGacha.setNodeActive(false);
this.countDownLabel.setNodeActive(true);
console.log(`Gacha reward: ${RewardType[this._reward.type]} quantity: ${this._reward.quantity}`);
let count = 3;
while (count >= 0) {
this.countDownLabel.setString(count == 0 ? 'Ready' : count);
count--;
await Utils.delay(0.5);
}
2024-06-10 06:13:32 -07:00
game.timeScale = 1;
2024-06-12 04:48:19 -07:00
2024-06-10 06:13:32 -07:00
tween(this.container)
.to(0.1, { opacity: 0 })
2024-06-10 21:35:13 -07:00
.call(() => {
this.container.setNodeActive(false);
2024-06-17 04:07:20 -07:00
this.countDownLabel.setNodeActive(false);
2024-06-10 21:35:13 -07:00
switch (this._reward.type) {
case RewardType.Star:
GameManager.instance.addScore(this._reward.quantity, ScoreType.DestroyObject);
break;
case RewardType.Time:
GameManager.instance.addTime(this._reward.quantity, Vec3.ZERO);
break;
2024-06-12 04:48:19 -07:00
case RewardType.Cheese:
const cheese = SpawnObjectManager.instance.getBoosterByType(BoosterType.CumulativeBar);
cheese.activeCollider = false;
cheese.hide();
GameManager.instance.addBooster(cheese);
break;
case RewardType.Ball:
EventManger.instance.emit(GameEvent.SpawnMultiBall, this._reward.quantity);
break;
2024-06-10 21:35:13 -07:00
}
})
2024-06-10 06:13:32 -07:00
.start();
}
2024-06-10 21:35:13 -07:00
public getRandomReward(weights?: number[]) {
return this.rewards.getRandom(weights);
2024-06-10 06:13:32 -07:00
}
public setReward(id: string) {
this._reward = this.rewards.find((r) => r.id == id);
this.gachaDone();
}
}