pinball/assets/_Game/Scripts/UI/TimeUI.ts

76 lines
2.6 KiB
TypeScript

import { _decorator, clamp01, Color, Component, Label, lerp, Node, Sprite, Tween, tween, Vec3 } from 'cc';
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
import GameState from '../Enum/GameState';
import { GameManager } from '../Manager/GameManager';
const { ccclass, property } = _decorator;
@ccclass('TimeUI')
export class TimeUI extends Component {
@property({ type: Label, visible: true })
private _timeLabel: Label;
@property({ type: Sprite, visible: true })
private _fill: Sprite;
@property({ type: Node, visible: true })
private _timeIcon: Node;
private _gameTime: number = 1;
private _fillValue: number = 1;
start() {
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
EventManger.instance.on(GameEvent.TimeUpdate, this.onTimeUpdate, this);
EventManger.instance.on(GameEvent.WarningTime, this.onWarningTime, this);
this._gameTime = GameManager.instance.gameTime;
}
protected update(dt: number): void {
this._fill.fillRange = lerp(this._fill.fillRange, this._fillValue, dt);
}
private async onGameStateChange(state: GameState) {
switch (state) {
case GameState.Init:
break;
case GameState.Ready:
this._timeLabel.string = this._gameTime.toString();
break;
case GameState.Playing:
break;
case GameState.GameOver:
break;
case GameState.End:
break;
case GameState.Relive:
break;
}
}
private onTimeUpdate(time: number) {
this._timeLabel.string = time.toString();
this._fillValue = clamp01(time / this._gameTime);
}
private onWarningTime(warning: boolean) {
if (warning) {
this._fill.color = Color.RED;
tween(this._timeIcon)
.to(0.05, { position: new Vec3(3.117, 3.177) })
.to(0.08, { scale: new Vec3(0.9, 1.1) })
.to(0.12, { position: new Vec3(1.559, 1.559) })
.to(0.17, {
scale: new Vec3(1, 1, 1),
position: new Vec3(1.559, -0.803),
})
.union()
.repeatForever()
.start();
this._timeLabel.color = Color.RED;
} else {
Tween.stopAllByTarget(this._timeIcon);
this._fill.color = Color.GREEN;
this._timeLabel.color = Color.WHITE;
}
}
}