import { _decorator, AudioClip, clamp01, Color, Component, Label, lerp, Node, Sprite, Tween, tween, Vec3 } from 'cc'; import GameState from '../Enum/GameState'; import GameEvent from '../Events/GameEvent'; import AudioManager from '../Manager/AudioManager'; import { EventManger } from '../Manager/EventManger'; 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; @property(Color) private normalColor: Color = new Color(); @property(Color) private warnColor: Color = new Color(); @property({ type: AudioClip, visible: true }) private _countDownSound: AudioClip; 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: AudioManager.stopSfx(this._countDownSound); Tween.stopAllByTarget(this._timeIcon); this._fill.color = this.normalColor; this._timeLabel.color = Color.WHITE; break; case GameState.GameOver: AudioManager.stopSfx(this._countDownSound); 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) { AudioManager.playSfx(this._countDownSound, { loop: true }); this._fill.color = this.warnColor; 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 { AudioManager.stopSfx(this._countDownSound); Tween.stopAllByTarget(this._timeIcon); this._fill.color = this.normalColor; this._timeLabel.color = Color.WHITE; } } }