TimerManager定时器支持添加多个时间回调

This commit is contained in:
dgflash
2025-09-16 11:57:02 +08:00
parent bac2c0c710
commit 4dbf10bb77
2 changed files with 75 additions and 26 deletions

View File

@@ -8,10 +8,29 @@ import { Component, game } from "cc";
import { StringUtil } from "../../utils/StringUtil";
import { Timer } from "./Timer";
interface ITimer {
/** 倒计时编号 */
id: string;
/** 定时器 */
timer: Timer;
/** 数据对象 */
object: any;
/** 修改数据对象的字段 */
field: string;
/** 事件侦听器的目标和被叫方 */
target: any;
/** 开始时间 */
startTime: number;
/** 每秒触发事件 */
onSeconds: Function[];
/** 时间完成事件 */
onCompletes: Function[];
}
/** 时间管理 */
export class TimerManager extends Component {
/** 倒计时数据 */
private times: any = {};
private times: { [key: string]: ITimer } = {};
/** 服务器时间 */
private date_s: Date = new Date();
/** 服务器初始时间 */
@@ -25,7 +44,7 @@ export class TimerManager extends Component {
protected update(dt: number) {
for (let key in this.times) {
let data = this.times[key];
let timer = data.timer as Timer;
let timer = data.timer;
if (timer.update(dt)) {
if (data.object[data.field] > 0) {
data.object[data.field]--;
@@ -35,8 +54,8 @@ export class TimerManager extends Component {
this.onTimerComplete(data);
}
// 触发每秒回调事件
else if (data.onSecond) {
data.onSecond.call(data.object);
else if (data.onSeconds) {
data.onSeconds.forEach(fn => fn.call(data.object));
}
}
}
@@ -44,10 +63,8 @@ export class TimerManager extends Component {
}
/** 触发倒计时完成事件 */
private onTimerComplete(data: any) {
if (data.onComplete) data.onComplete.call(data.target, data.object);
if (data.event) this.node.dispatchEvent(data.event);
private onTimerComplete(data: ITimer) {
if (data.onCompletes) data.onCompletes.forEach(fn => fn.call(data.target, data.object));
delete this.times[data.id];
}
@@ -58,7 +75,7 @@ export class TimerManager extends Component {
* @param target 触发事件的对象
* @param onSecond 每秒事件
* @param onComplete 倒计时完成事件
* @returns
* @returns 倒计时编号
* @example
export class Test extends Component {
private timeId!: string;
@@ -81,18 +98,37 @@ export class TimerManager extends Component {
const timer = new Timer();
timer.step = 1;
let data: any = {};
data.id = StringUtil.guid();
data.timer = timer;
data.object = object; // 管理对象
data.field = field; // 时间字段
data.onSecond = onSecond; // 每秒事件
data.onComplete = onComplete; // 倒计时完成事件
data.target = target
let data: ITimer = {
id: StringUtil.guid(),
timer: timer,
object: object,
field: field,
onSeconds: [],
onCompletes: [],
target: target,
startTime: this.getTime()
};
if (onSecond) data.onSeconds.push(onSecond);
if (onComplete) data.onCompletes.push(onComplete);
this.times[data.id] = data;
return data.id;
}
/**
* 为指定倒计时添加回调事件
* @param id 倒计时编号
* @param onSecond 每秒事件
* @param onComplete 倒计时完成事件
*/
addCallback(id: string, onSecond: Function, onComplete: Function) {
let data = this.times[id];
if (data) {
if (onSecond) data.onSeconds.push(onSecond);
if (onComplete) data.onCompletes.push(onComplete);
}
}
/**
* 在指定对象上注销一个倒计时的回调管理器
* @param id 时间对象唯一表示
@@ -153,23 +189,24 @@ export class TimerManager extends Component {
/** 游戏最小化时记录时间数据 */
save(): void {
for (let key in this.times) {
this.times[key].startTime = this.getTime();
let data: ITimer = this.times[key];
data.startTime = this.getTime();
}
}
/** 游戏最大化时回复时间数据 */
load(): void {
for (let key in this.times) {
let interval = Math.floor((this.getTime() - (this.times[key].startTime || this.getTime())) / 1000);
let data = this.times[key];
let interval = Math.floor((this.getTime() - (data.startTime || this.getTime())) / 1000);
data.object[data.field] = data.object[data.field] - interval;
if (data.object[data.field] <= 0) {
data.object[data.field] = 0;
this.onTimerComplete(data);
}
else {
this.times[key].startTime = null;
data.object[data.field] = 0;
}
}
}
}
}

View File

@@ -1,10 +1,22 @@
declare global {
interface Date {
/** 时间格式化 */
/**
* 时间格式化
* @param format 时间格式例如yy-mm-dd hh:mm:ss
*/
format(format: string): string;
/** 时间加法 */
/**
* 时间加法
* @param addMillis 时间加法,单位毫秒
*/
addTime(addMillis: number): Date;
/** 验证时间是否在指定范围 */
/**
* 验证时间是否在指定范围
* @param t1 范围开始时间
* @param t2 范围结束时间
*/
range(t1: number | Date, t2: number | Date): boolean;
}
}
@@ -39,11 +51,11 @@ Date.prototype.format = function (format: string): string {
return r;
};
Date.prototype.addTime = function (addMillis: number) {
Date.prototype.addTime = function (addMillis: number): Date {
return new Date(this.getTime() + addMillis);
}
Date.prototype.range = function (d1: number | Date, d2: number | Date) {
Date.prototype.range = function (d1: number | Date, d2: number | Date): boolean {
let t1: number = -1;
let t2: number = -1;
if (d1 instanceof Date)