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

76 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-03-27 04:00:23 -07:00
import { _decorator, CCInteger, clamp, Component, lerp, Sprite } from 'cc';
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';
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;
private _currentValue = 0;
private _fillValue = 0;
private _active = false;
private _timer = 0;
protected onLoad(): void {
this._fillBar.fillRange = 0;
EventManger.instance.on(GameEvent.Score, this.onScore, this);
EventManger.instance.on(GameEvent.BoosterActive, this.onBoosterActive, this);
EventManger.instance.on(GameEvent.BoosterDisable, this.onBoosterDisable, this);
}
protected update(dt: number): void {
if (Math.abs(this._fillValue - this._fillBar.fillRange) >= 0.001) {
this._fillBar.fillRange = lerp(this._fillBar.fillRange, this._fillValue, dt * 3);
}
if (!this._active && this._currentValue > 0) {
this._timer += dt;
if (this._timer >= 1) {
this._timer = 0;
this._currentValue -= 25;
if (this._currentValue < 0) {
this._currentValue = 0;
}
this._fillValue = -clamp(this._currentValue / 2 / this._maxValue, 0, 0.5);
}
}
}
private async onScore(score: number, points: number, type: ScoreType) {
if (!this._active) return;
switch (type) {
case ScoreType.DestroyObject:
this._currentValue += points;
break;
case ScoreType.Goal:
if (this._currentValue == 0) return;
await Utilities.delay(1);
GameManager.instance.addScore(this._currentValue, ScoreType.Combo, this.node.getWorldPosition(), {
scaleMin: 2,
scaleMax: 4,
duration: 1,
});
this._currentValue = 0;
break;
}
this._fillValue = -clamp(this._currentValue / 2 / this._maxValue, 0, 0.5);
}
private onBoosterActive(type: BoosterType) {
if (type == BoosterType.CumulativeBar) this._active = true;
}
private onBoosterDisable(type: BoosterType) {
if (type == BoosterType.CumulativeBar) this._active = false;
}
}