| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /**
- * 计时器管理类
- * @description
- * @author 冯聪
- */
- export default class TimerSystem extends cc.Component {
- //参数部分-----------------------------------------------------------------------------------
- /**计数器数组 */
- public gameTimerArr: GameTimer[] = [];
- //逻辑部分-----------------------------------------------------------------------------------
- /**开启游戏计时器 */
- public startGameTimer() {
- this.schedule(() => {
- this.runGameTimer();
- }, 1);
- }
- /**运行游戏计时器 */
- private runGameTimer() {
- for (var i: number = 0; i < this.gameTimerArr.length; i++) {
- var gameTimer: GameTimer = this.gameTimerArr[i];
- var callBack: Function = gameTimer.callBack;
- var object: any = gameTimer.object;
- gameTimer.curTime--;
- if (gameTimer.curTime <= 0) {
- gameTimer.curTime = gameTimer.totalTime;
- callBack.call(object);
- }
- }
- }
- /**
- * 添加游戏计时器
- * @description
- * @param time 计时的时间
- * @param callBack 计时结束之后的回调
- * @param object 计时绑定的对象
- */
- public addGameTimer(time: number, callBack: Function, object: any) {
- if (!this.ifHasSameTimer(callBack, object)) {
- let gameTimer = new GameTimer();
- gameTimer.curTime = time;
- gameTimer.totalTime = time;
- gameTimer.callBack = callBack;
- gameTimer.object = object;
- this.gameTimerArr.push(gameTimer);
- }
- }
- /**
- * 是否有相同的时间管理器
- * @param callBack 比对的回调
- * @param object 比对的对象
- */
- private ifHasSameTimer(callBack: Function, object: any): boolean {
- if (this.getGameTimer(callBack, object)) {
- console.error(`[GameTimerMgr]:${object.name}重复添加时间计时器`)
- return true;
- }
- else {
- return false;
- }
- }
- /**获取时间管理器 */
- private getGameTimer(callBack: Function, object: any): GameTimer {
- for (var i: number = this.gameTimerArr.length - 1; i >= 0; i--) {
- let gameTimer: GameTimer = this.gameTimerArr[i];
- if (gameTimer.callBack == callBack && gameTimer.object == object) {
- return gameTimer;
- }
- }
- return null;
- }
- /**
- * 移除时间管理器
- * @param callBack
- * @param object
- */
- public removeGameTimer(callBack: Function, object: any) {
- for (var i: number = this.gameTimerArr.length - 1; i >= 0; i--) {
- let gameTimer: GameTimer = this.gameTimerArr[i];
- if (gameTimer.callBack == callBack && gameTimer.object == object) {
- this.gameTimerArr.splice(i, 1)
- //gameTimer = null; //要不要加null 看最后的效果
- }
- }
- }
- }
- /**游戏计时器类 */
- export class GameTimer {
- /**当前计时时间 */
- curTime: number = 0;
- /**计时总时间 */
- totalTime: number = 0;
- /**计时的回调 */
- callBack: Function = null;
- /**绑定的脚本对象 */
- object: any = null;
- }
|