فهرست منبع

[FC]:修改EventSystem;TimerSystem;新增MathUtil

fengcong 5 سال پیش
والد
کامیت
efc8b4926e

+ 2 - 12
mk_framework/assets/script/mk/system/EventSystem.ts

@@ -5,13 +5,9 @@
  */
 export default class EventSystem {
 
-    //参数部分-----------------------------------------------------------------------------------
-
     /** 监听数组 */
     private listeners: { [key: string]: Observer[] } = {};
-
-    //逻辑部分-----------------------------------------------------------------------------------
-
+    
     /** 
      * 注册事件
      * @param name 事件名称
@@ -113,10 +109,4 @@ class Observer {
     compar(context: any): boolean {
         return context == this.context;
     }
-}
-
-/**自定义事件类型 */
-export enum EVENT_TYPE {
-    LoadDone_Config = "LoadDone_Config",
-    Update_Gold = 'Update_Gold',
-}
+}

+ 25 - 27
mk_framework/assets/script/mk/system/TimerSystem.ts

@@ -6,23 +6,20 @@
  */
 export default class TimerSystem extends cc.Component {
 
-    //参数部分-----------------------------------------------------------------------------------
-    /**计数器数组 */
-    public gameTimerArr: GameTimer[] = [];
+    /** 计数器数组 */
+    private timerArr: Timer[] = [];
 
-    //逻辑部分-----------------------------------------------------------------------------------
-
-    /**开启游戏计时器 */
-    public startGameTimer() {
+    /** 开启游戏计时器 */
+    public startTimer() {
         this.schedule(() => {
-            this.runGameTimer();
+            this.runTimer();
         }, 1);
     }
 
-    /**运行游戏计时器 */
-    private runGameTimer() {
-        for (var i: number = 0; i < this.gameTimerArr.length; i++) {
-            var gameTimer: GameTimer = this.gameTimerArr[i];
+    /** 运行游戏计时器 */
+    private runTimer() {
+        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;
             gameTimer.curTime--;
@@ -40,14 +37,14 @@ export default class TimerSystem extends cc.Component {
      * @param callBack 计时结束之后的回调
      * @param object   计时绑定的对象
      */
-    public addGameTimer(time: number, callBack: Function, object: any) {
+    public addTimer(time: number, callBack: Function, object: any) {
         if (!this.ifHasSameTimer(callBack, object)) {
-            let gameTimer = new GameTimer();
+            let gameTimer = new Timer();
             gameTimer.curTime = time;
             gameTimer.totalTime = time;
             gameTimer.callBack = callBack;
             gameTimer.object = object;
-            this.gameTimerArr.push(gameTimer);
+            this.timerArr.push(gameTimer);
         }
     }
 
@@ -57,8 +54,8 @@ export default class TimerSystem extends cc.Component {
      * @param object   比对的对象
      */
     private ifHasSameTimer(callBack: Function, object: any): boolean {
-        if (this.getGameTimer(callBack, object)) {
-            console.error(`[GameTimerMgr]:${object.name}重复添加时间计时器`)
+        if (this.getTimer(callBack, object)) {
+            //重复添加时间计时器
             return true;
         }
         else {
@@ -66,10 +63,10 @@ export default class TimerSystem extends cc.Component {
         }
     }
 
-    /**获取时间管理器 */
-    private getGameTimer(callBack: Function, object: any): GameTimer {
-        for (var i: number = this.gameTimerArr.length - 1; i >= 0; i--) {
-            let gameTimer: GameTimer = this.gameTimerArr[i];
+    /** 获取时间管理器 */
+    private getTimer(callBack: Function, object: 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) {
                 return gameTimer;
             }
@@ -82,19 +79,20 @@ export default class TimerSystem extends cc.Component {
      * @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)
+    public removeTimer(callBack: Function, object: 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) {
+                this.timerArr.splice(i, 1)
                 //gameTimer = null; //要不要加null 看最后的效果
+                return;
             }
         }
     }
 }
 
 /**游戏计时器类 */
-export class GameTimer {
+export class Timer {
     /**当前计时时间 */
     curTime: number = 0;
     /**计时总时间 */

+ 240 - 0
mk_framework/assets/script/mk/utils/MathUtil.ts

@@ -0,0 +1,240 @@
+
+/**
+ * 数学计算工具类
+ * @author 冯聪
+ */
+export default class MathUtil {
+
+    /** 计算两点之间距离 */
+    dis(x1, y1, x2, y2): number {
+        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
+    }
+
+    /**
+    * 取一个区间内随机整数
+    * @param n 开始位置
+    * @param m 结束位置
+    * @param ifInt 是否是整数
+    * @returns 返回的随机整数
+    */
+    public rnd(n: number, m: number, ifInt: boolean = true): number {
+        let random: number = n;
+        if (ifInt) {
+            random = Math.floor(Math.random() * (m - n + 1) + n);
+        }
+        else {
+            random = Math.random() * (m - n) + n;
+        }
+        return random;
+    }
+
+    //---------------------------------------------------------科学计数法-----------------------------------------------------
+    /**数字单位 */
+    private numUnit = { 4: "万", 8: "亿", 12: "兆", 16: "万兆", 20: "亿兆", 24: "兆兆", 28: "万兆兆", 32: "亿兆兆", 36: "兆兆兆" };
+
+    /**
+     * 超大数值加法运算  忽略小数部分
+     * @param a
+     * @param b
+     */
+    public addition(a: string, b: string): string {
+        var res: any = '',
+            temp: any = 0;
+        let a1 = a.split('.')[0].split('');
+        let b1 = b.split('.')[0].split('');
+        while (a1.length || b1.length || temp) {
+            temp += ~~a1.pop() + ~~b1.pop();
+            res = (temp % 10) + res;
+            temp = temp > 9;
+        }
+        return res.replace(/^0+/, '');
+    }
+
+    /**
+     * 超大数值减法运算  忽略小数部分
+     * @param a
+     * @param b
+     */
+    public subtraction(a: any, b: any): string {
+        a = a.split('.')[0].split('');
+        b = b.split('.')[0].split('');
+        var aMaxb = a.length > b.length;
+        if (a.length == b.length) {
+            for (var i = 0, len = a.length; i < len; i++) {
+                if (a[i] == b[i]) continue;
+                aMaxb = a[i] > b[i];
+                break;
+            }
+        }
+        if (!aMaxb) a = [b, b = a][0];
+        var result = '';
+        while (a.length) {
+            var temp = parseInt(a.pop()) - parseInt(b.pop() || 0);
+            if (temp >= 0) result = temp + result;
+            else {
+                result = temp + 10 + result;
+                a[a.length - 1]--;
+            }
+        }
+        result = (aMaxb ? '' : '-') + result.replace(/^0*/g, '');
+        if (result == '-') {
+            result = '0'
+        }
+
+        return result
+    }
+
+    /**
+     * 超大数值高位对比  忽略小数部分
+     * @param a
+     * @param b
+     */
+    public contrastNumber(a: any, b: any): boolean {
+        a = a.split('.')[0].split('');
+        b = b.split('.')[0].split('');
+        var aMaxb = a.length > b.length;
+        if (a.length == b.length) {
+            for (var i = 0, len = a.length; i < len; i++) {
+                if (a[i] == b[i]) continue;
+                aMaxb = a[i] > b[i];
+                break;
+            }
+        }
+        if (!aMaxb) a = [b, b = a][0];
+        var result = '';
+        while (a.length) {
+            var temp = parseInt(a.pop()) - parseInt(b.pop() || 0);
+            if (temp >= 0) result = temp + result;
+            else {
+                result = temp + 10 + result;
+                a[a.length - 1]--;
+            }
+        }
+        if (a === b) return true;
+        return Boolean(aMaxb);
+    }
+
+    /**
+     * 数值乘法
+     * @param a 
+     * @param b
+     */
+    public accMuls(a, b): string {
+        let arra = a.split('').reverse(),
+            arrb = b.split('').reverse(),
+            lena = arra.length,
+            lenb = arrb.length,
+            result: any = Array(lena + lenb + 1).join('0').split('');
+        arra.map((itema, indexa) => {
+            arrb.map((itemb, indexb) => {
+                result[indexa + indexb] = +result[indexa + indexb] + itema * itemb;
+            });
+        });
+        result.map((item, index) => {
+            if (item >= 10) {
+                result[index + 1] = ~~result[index + 1] + ~~(result[index] / 10);
+                result[index] %= 10;
+            }
+        });
+        return result.reverse().join('').replace(/^0+/, '');
+    }
+
+    /**
+     * 超大数值乘法
+     * @param arg1
+     * @param arg2
+     */
+    public accMul(arg1: string, arg2: string): string {
+        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
+        try { m += s1.split(".")[1].length } catch (e) { }
+        try { m += s2.split(".")[1].length } catch (e) { }
+        let targe = this.accMuls(s1.replace(".", ""), s2.replace(".", ""));
+
+        return this.accDiv(targe, Math.pow(10, m).toString())
+    }
+
+    /** 科学技术法转为数值字符串形式 */
+    public toNonExponential(num: number): string {
+        return num.toLocaleString().replace(/[,]/g, '')
+    }
+
+    /**
+     * 超大数值除法运算
+     * @param arg1
+     * @param arg2
+     */
+    public accDiv(arg1: string, arg2: string): string {
+
+        var t1 = 0, t2 = 0, t3 = 0, r1, r2;
+
+        try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
+
+        try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
+
+        r1 = Number(arg1.toString().replace(".", ""))
+
+        r2 = Number(arg2.toString().replace(".", ""))
+
+        if (r2 == 0)
+            return '0';
+
+        var result = String(r1 / r2);
+
+        try { t3 = result.toString().split(".")[1].length } catch (e) { }
+
+        var index = t2 - t1 - t3;
+
+        if (index < 0) {
+            result = result.replace(".", "");
+
+            while (result.length <= Math.abs(index)) {
+                result = '0' + result;
+            }
+
+            var start = result.substring(0, result.length + index);
+            var end = result.substring(result.length + index, result.length);
+
+            result = start + '.' + end;
+
+            return this.toNonExponential(Number(result));
+        }
+        else if (index > 0) {
+            result = result.replace(".", "");
+
+            while (result.length <= Math.abs(index)) {
+                result += '0';
+            }
+            return this.toNonExponential(Number(result));
+        }
+        else return this.toNonExponential(Number(result.replace(".", "")));
+    }
+
+    /**
+     * 数值格式化
+     * @param value
+     * @param showDeci
+     */
+    public format(value: string, showDeci: boolean = true): string {
+        var a = value.split('.')[0];
+        var b;
+        for (let i in this.numUnit) {
+            let ii = Number(i);
+            if (a.length - 1 >= ii) {
+                if (showDeci) {
+                    let deci = a.slice(a.length - ii, a.length - ii + 1)
+                    if (Number(deci) >= 1) {
+                        b = a.slice(0, a.length - ii) + "." + deci + this.numUnit[ii];
+                    }
+                    else {
+                        b = a.slice(0, a.length - ii) + this.numUnit[ii];
+                    }
+                }
+                else {
+                    b = a.slice(0, a.length - ii) + this.numUnit[ii];
+                }
+            }
+        };
+        if (!b) return a;
+        return b;
+    }
+}

+ 9 - 0
mk_framework/assets/script/mk/utils/MathUtil.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.0.8",
+  "uuid": "2e942b06-e892-49a5-a5a0-be51898ae38e",
+  "isPlugin": false,
+  "loadPluginInWeb": true,
+  "loadPluginInNative": true,
+  "loadPluginInEditor": false,
+  "subMetas": {}
+}