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.2, { scale: new Vec3(1.2, 1.2, 1.2) }) .to(0.2, { scale: Vec3.ONE }) .union() .repeatForever() .start(); this._timeLabel.color = Color.RED; } else { Tween.stopAllByTarget(this._timeIcon); this._fill.color = Color.GREEN; this._timeLabel.color = Color.WHITE; } } }