pinball/assets/_Game/Scripts/Environments/CumulativeBar.ts

263 lines
9.2 KiB
TypeScript
Raw Normal View History

2024-04-03 02:43:18 -07:00
import {
_decorator,
2024-05-15 00:42:31 -07:00
AudioClip,
CCFloat,
2024-04-03 02:43:18 -07:00
CCInteger,
clamp,
2024-05-15 00:42:31 -07:00
clamp01,
2024-04-03 02:43:18 -07:00
Component,
lerp,
Node,
2024-05-15 00:42:31 -07:00
ParticleSystem,
2024-04-03 02:43:18 -07:00
Prefab,
2024-05-15 00:42:31 -07:00
randomRange,
Sprite,
tween,
2024-04-03 02:43:18 -07:00
Tween,
Vec3,
} from 'cc';
2024-05-15 00:42:31 -07:00
import BoosterType from '../Enum/BoosterType';
2024-03-27 04:00:23 -07:00
import ScoreType from '../Enum/ScoreType';
2024-05-15 00:42:31 -07:00
import GameEvent from '../Events/GameEvent';
2024-05-27 02:19:31 -07:00
import AudioManager from '../Manager/AudioManager';
2024-05-15 00:42:31 -07:00
import { EventManger } from '../Manager/EventManger';
2024-03-27 04:00:23 -07:00
import { GameManager } from '../Manager/GameManager';
2024-05-15 00:42:31 -07:00
import ObjectPool from '../Pool/ObjectPool';
import Utilities from '../Utilities';
2024-04-24 01:52:31 -07:00
import { SequenceSound } from './SequenceSound';
2024-03-27 04:00:23 -07:00
const { ccclass, property } = _decorator;
2024-04-23 03:36:31 -07:00
@ccclass('Reward')
class Reward {
@property(CCInteger)
public scoreMin: number = 0;
@property(CCInteger)
public scoreMax: number = 0;
2024-04-24 01:52:31 -07:00
@property(AudioClip)
public sound: AudioClip;
2024-04-23 03:36:31 -07:00
@property(Prefab)
public rewardEffect: Prefab;
public pool: ObjectPool;
Init() {
this.pool = new ObjectPool(this.rewardEffect, 5, true);
}
}
2024-03-27 04:00:23 -07:00
@ccclass('CumulativeBar')
export class CumulativeBar extends Component {
@property({ type: Sprite, visible: true })
private _fillBar: Sprite;
@property({ type: CCInteger, visible: true })
private _maxValue = 1000;
2024-03-28 20:35:44 -07:00
@property({ type: Node, visible: true })
private _scoreUI: Node;
2024-04-04 04:27:04 -07:00
@property({ type: Prefab, visible: true })
private _starFxObjectPrefab: Prefab;
2024-03-28 20:35:44 -07:00
@property({ type: Prefab, visible: true })
private _scoreObjectPrefab: Prefab;
2024-03-27 04:00:23 -07:00
2024-04-04 21:43:09 -07:00
@property({ visible: true })
private _centerOffset: Vec3 = new Vec3();
2024-04-04 04:27:04 -07:00
@property({ type: CCFloat, visible: true })
private _radius: number = 0;
2024-04-04 21:43:09 -07:00
@property({ type: CCFloat, visible: true })
private _minAngle: number = 0;
@property({ type: CCFloat, visible: true })
private _maxAngle: number = 0;
2024-04-04 04:27:04 -07:00
2024-04-24 01:52:31 -07:00
@property({ type: SequenceSound, visible: true })
private _soundFx: SequenceSound;
2024-04-03 02:43:18 -07:00
@property({ type: AudioClip, visible: true })
2024-04-24 01:52:31 -07:00
private _goalSound: AudioClip;
@property({ type: AudioClip, visible: true })
private _cheeseModeGoalSound: AudioClip;
2024-04-04 04:27:04 -07:00
@property({ type: AudioClip, visible: true })
private _collectStartSoundFx;
2024-04-03 02:43:18 -07:00
2024-04-23 03:36:31 -07:00
@property({ type: Reward, visible: true })
private _rewards: Reward[] = [];
2024-04-04 21:43:09 -07:00
private _starPool: ObjectPool;
2024-04-04 04:27:04 -07:00
private _fxPool: ObjectPool;
2024-03-27 04:00:23 -07:00
private _currentValue = 0;
private _fillValue = 0;
private _active = false;
2024-03-28 20:35:44 -07:00
private _goal = false;
2024-03-27 04:00:23 -07:00
private _timer = 0;
2024-04-03 02:43:18 -07:00
private _multiplier = 1;
private _comboComplete = false;
2024-04-04 04:27:04 -07:00
private _currentValuePosition = new Vec3();
private _center = new Vec3();
2024-03-27 04:00:23 -07:00
protected onLoad(): void {
this._fillBar.fillRange = 0;
2024-04-04 21:43:09 -07:00
this._starPool = new ObjectPool(this._scoreObjectPrefab, 50, true);
2024-04-04 04:27:04 -07:00
this._fxPool = new ObjectPool(this._starFxObjectPrefab, 50, true);
2024-03-27 04:00:23 -07:00
EventManger.instance.on(GameEvent.Score, this.onScore, this);
EventManger.instance.on(GameEvent.BoosterActive, this.onBoosterActive, this);
EventManger.instance.on(GameEvent.BoosterDisable, this.onBoosterDisable, this);
2024-04-04 04:27:04 -07:00
this._center = this._fillBar.node.getWorldPosition();
2024-04-04 21:43:09 -07:00
this._center.add(this._centerOffset);
this.calcPositionOnCircleLine(this._minAngle);
2024-04-23 03:36:31 -07:00
this._rewards.forEach((reward) => reward.Init());
2024-03-27 04:00:23 -07:00
}
protected update(dt: number): void {
2024-03-28 20:35:44 -07:00
if (!this._goal && !this._active && this._currentValue > 0) {
2024-03-27 04:00:23 -07:00
this._timer += dt;
2024-03-28 20:35:44 -07:00
if (this._timer >= 0.1) {
2024-03-27 04:00:23 -07:00
this._timer = 0;
2024-04-03 02:43:18 -07:00
this._currentValue -= 2;
2024-03-27 04:00:23 -07:00
if (this._currentValue < 0) {
this._currentValue = 0;
}
this._fillValue = -clamp(this._currentValue / 2 / this._maxValue, 0, 0.5);
}
}
2024-03-28 20:35:44 -07:00
if (Math.abs(this._fillValue - this._fillBar.fillRange) >= 0.001) {
this._fillBar.fillRange = lerp(this._fillBar.fillRange, this._fillValue, dt * 3);
2024-04-04 04:27:04 -07:00
const progress = clamp01(this._fillBar.fillRange / -0.5);
2024-04-04 21:43:09 -07:00
const angle = lerp(this._minAngle, this._maxAngle, progress);
2024-04-04 04:27:04 -07:00
this.calcPositionOnCircleLine(angle);
2024-03-28 20:35:44 -07:00
}
2024-03-27 04:00:23 -07:00
}
2024-04-04 04:27:04 -07:00
private async onScore(score: number, points: number, type: ScoreType, position: Vec3) {
2024-03-27 04:00:23 -07:00
switch (type) {
case ScoreType.DestroyObject:
2024-03-28 20:35:44 -07:00
if (!this._active) return;
2024-04-25 02:23:23 -07:00
const star = this._starPool.get(GameManager.instance.topContainer);
2024-04-04 04:27:04 -07:00
star.setWorldPosition(position);
tween(star)
.to(
1,
{ worldPosition: this._currentValuePosition },
{
onUpdate: (target: Node, ratio: number) => {
target.worldPosition = target.worldPosition.lerp(this._currentValuePosition, ratio);
},
},
)
.call(async () => {
2024-05-15 00:42:31 -07:00
const fx = this._fxPool.get(ParticleSystem, GameManager.instance.topContainer);
const pos = star.getWorldPosition();
pos.z = 10;
fx.node.setWorldPosition(pos);
2024-04-04 21:43:09 -07:00
this._starPool.release(star);
2024-05-27 02:19:31 -07:00
AudioManager.playSfx(this._collectStartSoundFx);
2024-04-04 04:27:04 -07:00
await Utilities.waitUntil(() => {
return fx.isStopped;
}, 0.1);
this._fxPool.release(fx);
})
.start();
2024-04-03 02:43:18 -07:00
this._multiplier++;
this._currentValue += points * this._multiplier;
2024-04-04 04:27:04 -07:00
2024-04-03 02:43:18 -07:00
if (this._currentValue > this._maxValue) this._currentValue = this._maxValue;
2024-03-27 04:00:23 -07:00
2024-04-04 04:27:04 -07:00
break;
2024-03-27 04:00:23 -07:00
case ScoreType.Goal:
2024-04-24 01:52:31 -07:00
if (this._currentValue == 0) {
2024-05-27 02:19:31 -07:00
AudioManager.playSfx(this._goalSound, { volume: 3 });
2024-04-24 01:52:31 -07:00
return;
}
2024-05-27 02:19:31 -07:00
AudioManager.playSfx(this._cheeseModeGoalSound, { volume: 3 });
2024-04-03 02:43:18 -07:00
this._multiplier = 0;
2024-03-28 20:35:44 -07:00
this._goal = true;
2024-04-03 02:43:18 -07:00
let items = Math.ceil(this._currentValue / 10);
2024-04-23 03:36:31 -07:00
this.playCollectEffect(items, this._currentValue);
2024-04-03 02:43:18 -07:00
this._goal = false;
this._currentValue = 0;
2024-03-27 04:00:23 -07:00
break;
}
this._fillValue = -clamp(this._currentValue / 2 / this._maxValue, 0, 0.5);
}
2024-04-23 03:36:31 -07:00
private async playCollectEffect(items: number, score: number) {
2024-04-03 02:43:18 -07:00
const time = 0.04;
2024-04-04 04:27:04 -07:00
const offset = new Vec3();
this._comboComplete = false;
GameManager.instance.addScoreWithWaiting(
Math.round(score),
ScoreType.Combo,
this.node.getWorldPosition(),
() => this._comboComplete,
{
scaleMin: 2,
scaleMax: 4,
duration: 0.8,
},
);
2024-04-23 03:36:31 -07:00
while (items > 0) {
items--;
2024-04-04 21:43:09 -07:00
const obj = this._starPool.get(this._scoreUI);
2024-04-04 04:27:04 -07:00
Vec3.random(offset, 30);
offset.y = 0;
obj.setWorldPosition(this.node.getWorldPosition().add(offset));
2024-04-24 01:52:31 -07:00
this._soundFx.playSound(0.2);
2024-03-28 20:35:44 -07:00
tween(obj)
2024-04-04 04:27:04 -07:00
.to(randomRange(0.3, 0.4), { worldPosition: this._scoreUI.worldPosition }, { easing: 'sineIn' })
2024-04-04 21:43:09 -07:00
.call(() => this._starPool.release(obj))
2024-03-29 04:24:58 -07:00
.call(() => {
Tween.stopAllByTarget(this._scoreUI);
tween(this._scoreUI)
.set({ scale: Vec3.ONE })
2024-04-23 03:36:31 -07:00
.to(0.1, { scale: new Vec3(1.3, 1.3, 1.3) })
.to(0.1, { scale: Vec3.ONE })
2024-03-29 04:24:58 -07:00
.start();
})
2024-03-28 20:35:44 -07:00
.start();
await Utilities.delay(time);
}
await Utilities.waitUntil(() => this._starPool.countActive == 0, 0.1);
this._comboComplete = true;
2024-04-24 01:52:31 -07:00
this._soundFx.playSound();
await Utilities.delay(0.8);
2024-04-23 03:36:31 -07:00
this.calcReward(score);
}
private async calcReward(score: number) {
let selectReward: Reward;
for (let i = 0; i < this._rewards.length; i++) {
const reward = this._rewards[i];
if (score >= reward.scoreMin && score <= reward.scoreMax) {
selectReward = reward;
break;
}
}
if (selectReward) {
const fx = selectReward.pool.get(ParticleSystem, GameManager.instance.topContainer);
2024-05-15 00:42:31 -07:00
const pos = this.node.getWorldPosition();
pos.z = 10;
fx.node.setWorldPosition(pos);
2024-05-27 02:19:31 -07:00
AudioManager.playSfx(selectReward.sound);
await Utilities.waitUntil(() => fx.isStopped, 0.1);
2024-04-23 03:36:31 -07:00
selectReward.pool.release(fx);
}
2024-03-28 20:35:44 -07:00
}
2024-04-04 04:27:04 -07:00
private calcPositionOnCircleLine(angle: number) {
2024-05-07 03:20:37 -07:00
this._currentValuePosition = this._center.getPositionOnCircle(angle, this._radius);
2024-04-04 04:27:04 -07:00
}
2024-03-27 04:00:23 -07:00
private onBoosterActive(type: BoosterType) {
if (type == BoosterType.CumulativeBar) this._active = true;
}
2024-04-04 04:27:04 -07:00
2024-03-27 04:00:23 -07:00
private onBoosterDisable(type: BoosterType) {
2024-04-03 02:43:18 -07:00
if (type == BoosterType.CumulativeBar) {
this._multiplier = 0;
this._active = false;
}
2024-03-27 04:00:23 -07:00
}
}