Преглед на файлове

[FC]:TimerSystem;EventSystem 绑定的域对象 统一为target

fengcong преди 5 години
родител
ревизия
64026e0c63
променени са 2 файла, в които са добавени 37 реда и са изтрити 33 реда
  1. 15 15
      mk_framework/assets/script/mk/system/EventSystem.ts
  2. 22 18
      mk_framework/assets/script/mk/system/TimerSystem.ts

+ 15 - 15
mk_framework/assets/script/mk/system/EventSystem.ts

@@ -10,11 +10,11 @@ export default class EventSystem {
     
     /** 
      * 注册事件
-     * @param name 事件名称
+     * @param name     事件名称
      * @param callback 回调函数
-     * @param context 上下文
+     * @param target   目标脚本(this)
      */
-    public register(name: string, callback: Function, context: any) {
+    public register(name: string, callback: Function, target: any) {
 
         //如果没有该事件
         if (!this.listeners[name]) {
@@ -26,28 +26,28 @@ export default class EventSystem {
         let length = observers.length;
         for (let i = 0; i < length; i++) {
             let observer = observers[i];
-            if (observer.compar(context)) {
+            if (observer.compar(target)) {
                 console.warn("[EventMgr]重复添加监听,请检查事件:", name)
                 return;
             }
         }
-        this.listeners[name].push(new Observer(callback, context));
+        this.listeners[name].push(new Observer(callback, target));
     }
 
     /**
      * 移除事件
-     * @param name 事件名称
+     * @param name     事件名称
      * @param callback 回调函数
-     * @param context 上下文
+     * @param target   目标脚本(this)
      */
-    public remove(name: string, callback: Function, context: any) {
+    public remove(name: string, callback: Function, target: any) {
 
         let observers: Observer[] = this.listeners[name];
         if (!observers) return;
         let length = observers.length;
         for (let i = 0; i < length; i++) {
             let observer = observers[i];
-            if (observer.compar(context)) {
+            if (observer.compar(target)) {
                 observers.splice(i, 1);
                 break;
             }
@@ -83,14 +83,14 @@ class Observer {
 
     /** 回调函数 */
     private callback: Function = null;
-    /** 上下文 */
-    private context: any = null;
+    /** 目标脚本 */
+    private target: any = null;
 
     /** 构造函数 */
-    constructor(callback: Function, context: any) {
+    constructor(callback: Function, target: any) {
         let self = this;
         self.callback = callback;
-        self.context = context;
+        self.target = target;
     }
 
     /**
@@ -99,7 +99,7 @@ class Observer {
      */
     notify(...args: any[]): void {
         let self = this;
-        self.callback.call(self.context, ...args);
+        self.callback.call(self.target, ...args);
     }
 
     /**
@@ -107,6 +107,6 @@ class Observer {
      * @param context 上下文
      */
     compar(context: any): boolean {
-        return context == this.context;
+        return context == this.target;
     }
 }

+ 22 - 18
mk_framework/assets/script/mk/system/TimerSystem.ts

@@ -21,11 +21,11 @@ export default class TimerSystem extends cc.Component {
         for (var i: number = 0; i < this.timerArr.length; i++) {
             var gameTimer: Timer = this.timerArr[i];
             var callBack: Function = gameTimer.callBack;
-            var object: any = gameTimer.object;
+            var target: any = gameTimer.target;
             gameTimer.curTime--;
             if (gameTimer.curTime <= 0) {
                 gameTimer.curTime = gameTimer.totalTime;
-                callBack.call(object);
+                callBack.call(target);
             }
         }
     }
@@ -35,26 +35,26 @@ export default class TimerSystem extends cc.Component {
      * @description
      * @param time     计时的时间
      * @param callBack 计时结束之后的回调
-     * @param object   计时绑定的对象
+     * @param target   计时绑定的脚本对象
      */
-    public addTimer(time: number, callBack: Function, object: any) {
-        if (!this.ifHasSameTimer(callBack, object)) {
+    public addTimer(time: number, callBack: Function, target: any) {
+        if (!this.ifHasSameTimer(callBack, target)) {
             let gameTimer = new Timer();
             gameTimer.curTime = time;
             gameTimer.totalTime = time;
             gameTimer.callBack = callBack;
-            gameTimer.object = object;
+            gameTimer.target = target;
             this.timerArr.push(gameTimer);
         }
     }
 
     /**
      * 是否有相同的时间管理器
-     * @param callBack 比对的回调
-     * @param object   比对的对象
+     * @param callBack 回调方法
+     * @param target   绑定的对象
      */
-    private ifHasSameTimer(callBack: Function, object: any): boolean {
-        if (this.getTimer(callBack, object)) {
+    private ifHasSameTimer(callBack: Function, target: any): boolean {
+        if (this.getTimer(callBack, target)) {
             //重复添加时间计时器
             return true;
         }
@@ -63,11 +63,15 @@ export default class TimerSystem extends cc.Component {
         }
     }
 
-    /** 获取时间管理器 */
-    private getTimer(callBack: Function, object: any): Timer {
+    /** 
+     * 获取时间管理器
+     * @param callBack 回调方法
+     * @param target   绑定的对象
+     */
+    private getTimer(callBack: Function, target: any): Timer {
         for (var i: number = this.timerArr.length - 1; i >= 0; i--) {
             let gameTimer: Timer = this.timerArr[i];
-            if (gameTimer.callBack == callBack && gameTimer.object == object) {
+            if (gameTimer.callBack == callBack && gameTimer.target == target) {
                 return gameTimer;
             }
         }
@@ -76,13 +80,13 @@ export default class TimerSystem extends cc.Component {
 
     /**
      * 移除时间管理器
-     * @param callBack 
-     * @param object 
+     * @param callBack 回调方法
+     * @param target   绑定的对象
      */
-    public removeTimer(callBack: Function, object: any) {
+    public removeTimer(callBack: Function, target: any) {
         for (var i: number = this.timerArr.length - 1; i >= 0; i--) {
             let timer: Timer = this.timerArr[i];
-            if (timer.callBack == callBack && timer.object == object) {
+            if (timer.callBack == callBack && timer.target == target) {
                 this.timerArr.splice(i, 1)
                 //gameTimer = null; //要不要加null 看最后的效果
                 return;
@@ -100,5 +104,5 @@ export class Timer {
     /** 计时的回调 */
     callBack: Function = null;
     /** 绑定的脚本对象 */
-    object: any = null;
+    target: any = null;
 }