Files
fc/dev/project/library/imports/7d/7d42e312-83c1-4929-84c6-7972391fd2e6.js
T
2026-05-24 10:21:26 +08:00

197 lines
4.8 KiB
JavaScript

"use strict";
cc._RF.push(module, '7d42eMSg8FJKYTGeXI5H9Lm', 'nx.timers');
// Scripts/nx/kernel/nx.timers.js
"use strict";
/*******************************************************************************
*
* Nx 时间管理器
*
*
*
* 2021.12.10
******************************************************************************/
var NxObject = require("nx.object");
// 最小精度(秒)
var TICK_INTERVAL = 0.5;
var NxTimerManager = cc.Class({
"extends": NxObject,
name: "NxTimerManager",
// 初始化
initialize: function initialize(_args) {
// USPER
if (!this._super(_args)) {
return false;
}
// 当前计时器列表
this.timers = [];
this.refCounter = 0;
var sched = cc.director.getScheduler();
if (!sched) {
nx.error("[计时器]计时器无法脱离引擎独自运行!");
return;
}
// 重复初始化判断
sched.enableForTarget(this);
if (sched.isScheduled(this._hbTicker, this)) {
return;
}
// 初始化
sched.enableForTarget(this);
sched.schedule(this._hbTicker, this, TICK_INTERVAL, false);
nx.debug("[计时器]计时器初始化完毕!");
// 全局服务开启
nx.timers = this;
return true;
},
// 销毁
uninitialize: function uninitialize() {
this.clean();
// 全局服务关闭
nx.timers = null;
// USPER
return this._super();
},
// 查询计时器
findTimer: function findTimer(_uuname, _notify) {
if (_notify === void 0) {
_notify = false;
}
// 合法性检查
if (nx.dt.strEmpty(_uuname)) {
nx.error("[计时器]查找计时器失败,非法的名字!");
return null;
}
for (var i in this.timers) {
var timer = this.timers[i];
if (timer && timer.name == _uuname) {
return timer;
}
}
if (_notify != false) {
nx.warn("[计时器]未找到计时器:", _uuname);
}
return null;
},
// 新建计时器
newTimer: function newTimer(_uuname, _interval, _times, _callback, _scale) {
if (_scale === void 0) {
_scale = true;
}
// 合法性检查
if (nx.dt.strEmpty(_uuname) || !nx.dt.numPositive(_interval, false) || !nx.dt.numGood(_times) || !nx.dt.fnGood(_callback)) {
nx.error("[计时器]新建计时器失败,参数不合法!");
return false;
}
// 重复检查
if (this.findTimer(_uuname, false)) {
nx.error("[计时器]新建计时器失败,重复创建! ", _uuname);
return false;
}
// 创建
var timer = {
name: _uuname,
// 唯一名字
alive: true,
// 存活中
interval: _interval,
// 间隔
times: _times,
// 次数(-1为无限次)
handle: _callback,
// 回调函数
fSecs: 0,
// 当前已去秒数
nTimes: 0,
// 当前回调次数
bscale: _scale // 是否可加速
};
this.timers.push(timer);
this.refCounter++;
nx.debug("[计时器]新建:", _uuname);
return true;
},
// 删除计时器
delTimer: function delTimer(_uuname) {
// 合法性检查
if (nx.dt.strEmpty(_uuname)) {
nx.error("[计时器]删除计时器失败,非法的名字!");
return false;
}
for (var i in this.timers) {
var timer = this.timers[i];
if (timer && timer.name == _uuname) {
this.timers.splice(i, 1);
nx.debug("[计时器]删除:", _uuname);
return true;
}
}
// nx.warn( "删除计时器失败,未找到计时器:", _uuname );
// return false;
return true;
},
// 清理
clean: function clean() {
this.timers = [];
var sched = cc.director.getScheduler();
if (sched) {
sched.unschedule(this._hbTicker, this);
}
nx.error("[计时器]计时器全清理完毕!");
},
// 心跳
_hbTicker: function _hbTicker() {
// 计时器检查
var _hbTimer = function _hbTimer(_timer, _secs) {
// 计时,超过间隔通知
_timer.fSecs += _secs;
if (_timer.fSecs >= _timer.interval) {
_timer.handle();
_timer.fSecs -= _timer.interval;
_timer.nTimes++;
}
// 满足次数准备删除
if (_timer.times > 0 && _timer.nTimes >= _timer.times) {
_timer.alive = false;
}
};
// 遍历计时器
// let rate = cc.getSpeed();
var rate = 1;
for (var i in this.timers) {
var timer = this.timers[i];
if (timer) {
_hbTimer(timer, timer.bscale ? TICK_INTERVAL : TICK_INTERVAL / rate);
}
}
// 删除过期计时器
for (var _i in this.timers) {
var timer = this.timers[_i];
if (!timer || !timer.alive) {
nx.debug("[计时器]删除过期计时器:", timer.name);
this.timers.splice(_i, 1);
}
}
}
});
// 模块导出
module.exports = NxTimerManager;
cc._RF.pop();