import { _decorator, CCFloat, Component, game, Game, Node, ProgressBar, tween } from 'cc'; const { ccclass, property } = _decorator; @ccclass('UmButtonCountdown') export class UmButtonCountdown extends Component { @property(ProgressBar) public barCountdown: ProgressBar = null!; @property(Node) public blockInput: Node = null!; @property(CCFloat) public TIME_COUNTDOWN = 10; private _time_available = 0; private _tween: any = null!; onLoad() { game.on(Game.EVENT_SHOW, this._onShowGame, this); } onDestroy() { game.off(Game.EVENT_SHOW, this._onShowGame, this); } private _onShowGame() { this._checkCountdown(); } onEnable() { this._checkCountdown(); } private _checkCountdown() { this._tween?.stop(); this._tween = null; let now_time = (new Date()).getTime(); if (now_time >= this._time_available) { this.barCountdown.progress = 0; this.blockInput?.setNodeActive(false); return; } this._startCountdown(now_time); } private _startCountdown(now_time: number) { this.blockInput?.setNodeActive(true); let action_time = this._time_available - now_time; action_time = action_time / 1000; this.barCountdown.progress = action_time / this.TIME_COUNTDOWN; this.tweenBar(action_time); } onDisable() { this._tween?.stop(); this._tween = null; } public updateTimeAvailAble() { let now_time = (new Date()).getTime(); this._time_available = now_time + this.TIME_COUNTDOWN * 1000; this._startCountdown(now_time); } public tweenBar(actionTime: number) { this._tween = tween(this.barCountdown).to(actionTime, { progress: 0 }).call(() => { this._tween = null; if (this.node) { this.barCountdown.progress = 0; this.blockInput?.setNodeActive(false); } }); this._tween.start(); } }