pinball/assets/_Game/Scripts/Base/Timer.ts

47 lines
945 B
TypeScript

import { Director, director, game } from 'cc';
export enum TimerType {
countDown,
CountUp,
}
export default class Timer {
private _timer: number = 0;
private _paused: boolean = true;
private _type: TimerType;
constructor(type: TimerType) {
this._type = type;
director.on(Director.EVENT_AFTER_UPDATE, this.update, this);
}
public get time() {
return this._timer;
}
public get timeRound() {
return Math.round(this._timer);
}
public set time(value: number) {
this._timer = value;
}
protected update(dt: number): void {
if (this._paused) return;
if (this._type == TimerType.CountUp) {
this._timer += game.deltaTime;
} else {
this._timer -= game.deltaTime;
}
}
public startCount() {
this._paused = false;
}
public stopCount() {
this._paused = true;
}
}