208 lines
5.3 KiB
JavaScript
208 lines
5.3 KiB
JavaScript
/*******************************************************************************
|
|
*
|
|
* Nx 时间管理器
|
|
*
|
|
*
|
|
*
|
|
* 2021.12.10
|
|
******************************************************************************/
|
|
|
|
const NxObject = require( "nx.object" );
|
|
|
|
// 最小精度(秒)
|
|
const TICK_INTERVAL = 0.5;
|
|
|
|
var NxTimerManager = cc.Class( {
|
|
|
|
extends: NxObject,
|
|
|
|
name: "NxTimerManager",
|
|
|
|
// 初始化
|
|
initialize: function( _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() {
|
|
|
|
this.clean();
|
|
|
|
// 全局服务关闭
|
|
nx.timers = null;
|
|
|
|
// USPER
|
|
return this._super();
|
|
},
|
|
|
|
// 查询计时器
|
|
findTimer: function( _uuname, _notify = false ) {
|
|
|
|
// 合法性检查
|
|
if( nx.dt.strEmpty( _uuname ) ) {
|
|
nx.error( "[计时器]查找计时器失败,非法的名字!" );
|
|
return null;
|
|
}
|
|
|
|
for( let 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( _uuname, _interval, _times, _callback, _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( _uuname ) {
|
|
|
|
// 合法性检查
|
|
if( nx.dt.strEmpty( _uuname ) ) {
|
|
nx.error( "[计时器]删除计时器失败,非法的名字!" );
|
|
return false;
|
|
}
|
|
|
|
for( let 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() {
|
|
|
|
this.timers = [];
|
|
|
|
var sched = cc.director.getScheduler();
|
|
if( sched ) {
|
|
sched.unschedule( this._hbTicker, this );
|
|
}
|
|
|
|
nx.error( "[计时器]计时器全清理完毕!" );
|
|
},
|
|
|
|
// 心跳
|
|
_hbTicker: function() {
|
|
|
|
// 计时器检查
|
|
var _hbTimer = function( _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();
|
|
let rate = 1;
|
|
for( let i in this.timers ) {
|
|
var timer = this.timers[ i ];
|
|
if( timer ) {
|
|
_hbTimer( timer, timer.bscale ? TICK_INTERVAL : ( TICK_INTERVAL / rate ) );
|
|
}
|
|
}
|
|
|
|
// 删除过期计时器
|
|
for( let 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;
|