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

197 lines
7.0 KiB
TypeScript
Raw Normal View History

2024-04-03 02:43:18 -07:00
import {
_decorator,
CCInteger,
clamp,
Component,
lerp,
Sprite,
Node,
tween,
Prefab,
Tween,
Vec3,
Label,
AudioClip,
2024-04-04 04:27:04 -07:00
randomRange,
math,
CCFloat,
clamp01,
ParticleSystem,
2024-04-03 02:43:18 -07:00
} from 'cc';
2024-03-27 04:00:23 -07:00
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
import ScoreType from '../Enum/ScoreType';
import Utilities from '../Utilities';
import { GameManager } from '../Manager/GameManager';
import BoosterType from '../Enum/BoosterType';
2024-03-28 20:35:44 -07:00
import ObjectPool from '../Pool/ObjectPool';
2024-04-03 02:43:18 -07:00
import { SoundManager } from '../Manager/SoundManager';
2024-03-27 04:00:23 -07:00
const { ccclass, property } = _decorator;
@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 04:27:04 -07:00
@property({ type: CCFloat, visible: true })
private _radius: number = 0;
2024-04-03 02:43:18 -07:00
@property({ type: AudioClip, visible: true })
private _soundFx;
2024-04-04 04:27:04 -07:00
@property({ type: AudioClip, visible: true })
private _collectStartSoundFx;
2024-04-03 02:43:18 -07:00
2024-03-29 04:24:58 -07:00
private _pool: 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;
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-03 02:43:18 -07:00
this._pool = 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();
this.calcPositionOnCircleLine(0);
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);
const angle = lerp(0, 180, progress);
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-04 04:27:04 -07:00
const star = this._pool.get(this.node);
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 () => {
const fx = this._fxPool.get(ParticleSystem, this.node);
fx.node.setWorldPosition(star.worldPosition);
this._pool.release(star);
SoundManager.instance.playSfx(this._collectStartSoundFx);
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:
if (this._currentValue == 0) return;
2024-04-03 02:43:18 -07:00
this._multiplier = 0;
2024-03-28 20:35:44 -07:00
this._goal = true;
2024-03-27 04:00:23 -07:00
await Utilities.delay(1);
2024-03-28 20:35:44 -07:00
GameManager.instance.addScore(
Math.round(this._currentValue),
ScoreType.Combo,
this.node.getWorldPosition(),
{
scaleMin: 2,
scaleMax: 4,
duration: 1,
},
);
2024-04-03 02:43:18 -07:00
let items = Math.ceil(this._currentValue / 10);
2024-03-28 20:35:44 -07:00
this.playCollectEffect(items);
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-03-28 20:35:44 -07:00
private async playCollectEffect(items: 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();
2024-03-28 20:35:44 -07:00
while (items > 0) {
2024-03-29 04:24:58 -07:00
const obj = this._pool.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-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-03-29 04:24:58 -07:00
.call(() => {
Tween.stopAllByTarget(this._scoreUI);
tween(this._scoreUI)
.set({ scale: Vec3.ONE })
.to(0.1, { scale: new Vec3(1.2, 1.2, 1.2) })
.set({ scale: Vec3.ONE })
.start();
})
2024-04-04 04:27:04 -07:00
.call(() => this._pool.release(obj))
2024-03-28 20:35:44 -07:00
.start();
items--;
2024-04-04 04:27:04 -07:00
SoundManager.instance.playSfx(this._soundFx, 0.5);
2024-03-28 20:35:44 -07:00
await Utilities.delay(time);
}
}
2024-04-04 04:27:04 -07:00
private calcPositionOnCircleLine(angle: number) {
this._currentValuePosition.x = this._center.x + this._radius * -Math.cos(math.toRadian(angle));
this._currentValuePosition.y = this._center.y + this._radius * Math.sin(math.toRadian(angle));
}
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
}
}