Просмотр исходного кода

[FC]:修改LogUtil;新增EventSystem(事件),TimerSystem(计时器)

fengcong 5 лет назад
Родитель
Сommit
9e5e38d3c5

+ 122 - 0
mk_framework/assets/script/mk/system/EventSystem.ts

@@ -0,0 +1,122 @@
+
+/**
+ * 游戏事件管理类
+ * @author 冯聪
+ */
+export default class EventSystem {
+
+    //参数部分-----------------------------------------------------------------------------------
+
+    /** 监听数组 */
+    private listeners: { [key: string]: Observer[] } = {};
+
+    //逻辑部分-----------------------------------------------------------------------------------
+
+    /** 
+     * 注册事件
+     * @param name 事件名称
+     * @param callback 回调函数
+     * @param context 上下文
+     */
+    public register(name: string, callback: Function, context: any) {
+
+        //如果没有该事件
+        if (!this.listeners[name]) {
+            this.listeners[name] = [];
+        }
+
+        let observers: Observer[] = this.listeners[name];
+
+        let length = observers.length;
+        for (let i = 0; i < length; i++) {
+            let observer = observers[i];
+            if (observer.compar(context)) {
+                console.warn("[EventMgr]重复添加监听,请检查事件:", name)
+                return;
+            }
+        }
+        this.listeners[name].push(new Observer(callback, context));
+    }
+
+    /**
+     * 移除事件
+     * @param name 事件名称
+     * @param callback 回调函数
+     * @param context 上下文
+     */
+    public remove(name: string, callback: Function, context: 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)) {
+                observers.splice(i, 1);
+                break;
+            }
+        }
+        if (observers.length == 0) {
+            delete this.listeners[name];
+        }
+    }
+
+    /**
+     * 发送事件
+     * @param name 事件名称
+     */
+    public fire(name: string, ...args: any[]) {
+
+        let observers: Observer[] = this.listeners[name];
+        if (!observers) return;
+
+        //克隆一个新数组(暂时concat代替),然后分发事件,防止那边remove之后,[0 1] 变成 [1] 然后1号位为undefined
+        let tempObservers: Observer[] = observers.concat();
+        let length = tempObservers.length;
+        for (let i = 0; i < length; i++) {
+            let observer = tempObservers[i];
+            observer.notify(...args, name);
+        }
+        //置空释放
+        tempObservers = null;
+    }
+}
+
+/**事件观察者类*/
+class Observer {
+
+    /** 回调函数 */
+    private callback: Function = null;
+    /** 上下文 */
+    private context: any = null;
+
+    /**构造函数 */
+    constructor(callback: Function, context: any) {
+        let self = this;
+        self.callback = callback;
+        self.context = context;
+    }
+
+    /**
+     * 发送通知
+     * @param args 不定参数
+     */
+    notify(...args: any[]): void {
+        let self = this;
+        self.callback.call(self.context, ...args);
+    }
+
+    /**
+     * 上下文比较
+     * @param context 上下文
+     */
+    compar(context: any): boolean {
+        return context == this.context;
+    }
+}
+
+/**自定义事件类型 */
+export enum EVENT_TYPE {
+    LoadDone_Config = "LoadDone_Config",
+    Update_Gold = 'Update_Gold',
+}

+ 9 - 0
mk_framework/assets/script/mk/system/EventSystem.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.0.8",
+  "uuid": "aab2795c-5fda-40cf-9490-d7a52412b6e1",
+  "isPlugin": false,
+  "loadPluginInWeb": true,
+  "loadPluginInNative": true,
+  "loadPluginInEditor": false,
+  "subMetas": {}
+}

+ 106 - 0
mk_framework/assets/script/mk/system/TimerSystem.ts

@@ -0,0 +1,106 @@
+
+/**
+ * 计时器管理类 
+ * @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;
+}

+ 9 - 0
mk_framework/assets/script/mk/system/TimerSystem.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.0.8",
+  "uuid": "70a46dc2-1209-45cb-8f4e-1905ab19da1c",
+  "isPlugin": false,
+  "loadPluginInWeb": true,
+  "loadPluginInNative": true,
+  "loadPluginInEditor": false,
+  "subMetas": {}
+}

+ 5 - 7
mk_framework/assets/script/mk/utils/LogUtil.ts

@@ -1,4 +1,5 @@
-/**日志打印工具类
+/**
+ * 日志打印工具类
  * @description 
  * @author 冯聪
  */
@@ -39,8 +40,7 @@ export default class LogUtil {
             return;
         }
         let totalTag = tag + this._tagChar;
-        let message = this.getMessage(data);
-        console.log("%c " + totalTag, "color: " + LogTagColor.bule, message);
+        console.log("%c " + totalTag, "color: " + LogTagColor.bule, ...data);
     }
 
     /**
@@ -54,8 +54,7 @@ export default class LogUtil {
             return;
         }
         let totalTag = tag + this._tagChar;
-        let message = this.getMessage(data);
-        console.warn("%c " + totalTag, "color: " + LogTagColor.orange, message);
+        console.warn("%c " + totalTag, "color: " + LogTagColor.orange, ...data);
     }
 
     /**
@@ -69,8 +68,7 @@ export default class LogUtil {
             return;
         }
         let totalTag = tag + this._tagChar;
-        let message = this.getMessage(data);
-        console.warn("%c " + totalTag, "color: " + LogTagColor.red, message);
+        console.warn("%c " + totalTag, "color: " + LogTagColor.red,...data);
     }
 
     /**