Cocos Creator MessageSystem

export default class Dictionary {

    private items = {};
    public length: number = 0;

    constructor(key?:any[],value?:any[]){
        if(!key || !value){
            return;
        }
        if(key.length != value.length){
            console.log("%c The length of the key and value do not match.", "color:#FF0000");
            return;
        }
        for(var i = 0; i < key.length; ++i){
            if(this.has(key[i])){
                //有相同的键
                var last_value = this.get(key[i]);
                console.log("%c The key already has a value( %c " + last_value +") and will be overwritten by the value( %c "+ value[i] +")", "color:#FF0000","color:#0000FF","color:#00FF00");
            }
            this.set(key[i],value[i]);
        }
    }

    /**
     * @param key 键
     * @returns 是否包含
     */
    public has(key: any): boolean {
        return key in this.items;
    }

    /**
     * @param key 键
     * @param valye 值
     */
    public set(key: any, value: any) {
        this.items[key] = value;
        this.length = this.get_length();
    }

    /**
     * @param 
     * @returns 查找结果,如果未查询到返回undefined
     */
    public get(key: any) {
        return this.has(key) ? this.items[key] : undefined;
    }

    /**
     * 
     * @param key 
     * @param item 
     * @param create 如果未查询到是否自动创建,ps:默认自动创建
     */
    public push(key:any,item:any,create :boolean = true){
        if(this.has(key)){
            var items = this.get(key);
            items.push(item);
            this.set(key,items);
        }else{
            if(create){
                var new_items = [item];
                this.set(key,new_items);
                console.log("%c key : " + key + " not found.will craete one", "color:#FF0000");
            }else{
                console.log("%c key : " + key + " not found.", "color:#FF0000");
            }
        }
    }

    /**
     * 获取当前字典中所有的键数组
     * @returns 所有键的数组
     */
    public all_key(): string[] {
        var values: string[] = [];
        for (var key in this.items) {
            values.push(key)
        }
        return values;
    }

    /**
     * @param key 键
     * @returns 是否移除成功
     */
    public remove(key: any): boolean {
        if (this.has(key)) {
            delete this.items[key];
            this.length = this.get_length();
            return true;
        } else {
            console.log("%c key : " + key + " not found.", "color:#FF0000");
        }
        return false;
    }

    /**
     * 移除指定单元
     * @param key 
     * @param item 
     * @returns 是否移除成功
     */
    public remove_item(key: any, item: any) {
        if (this.has(key)) {
            var items = this.get(key);
            for (var i = 0; i < items.length; ++i) {
                if (items[i] == item) {
                    items.splice(i, 1);
                    break;
                }
            }
            this.set(key, items);
            this.length = this.get_length();
            return true;
        } else {
            console.log("%c key : " + key + " not found.", "color:#FF0000");
        }
        return false;
    }

    /**
     * 清理字典
     */
    public clear() {
        this.items = {};
        this.length = 0;
    }

    /**
     * 复制地点
     * @returns 新的字典
     */
    public copy(): Dictionary {
        var dic = new Dictionary();
        var all = this.all_key();
        for (var i = 0; i < all.length; ++i) {
            dic.set(all[i], this.get(all[i]));
        }
        dic.length = this.length;
        return dic;
    }

    /**
     * 字典的长度
     * @returns 字典的长度
     */
    private get_length(): number {
        return Object.keys(this.items).length;
    }

}

export default class MessageSystemHelper {

    callback: Function = null;
    target = null;
    
    constructor(target, call) {
        this.target = target;
        this.callback = call;
    }

    same(target) {
        return this.target == target;
    }
    
}

import Dictionary from "./Dictionary";
import MessageSystemHelper from "./MessageSystemHelper";

export default class MessageSystem {

    private static list: Dictionary = new Dictionary();

    /**
     * 
     * @param message 消息
     * @param target 作用域(仅作为匹配标识使用)
     * @param callbacks 回调
     * @returns 
     */
    public static add_message(message: String, target?, callbacks?: Function) {
        if (callbacks == null || target == null){
            var msg = "add message '" + message + "' failed,"
            msg += callbacks == null && target == null ?"target and callback is null":target == null ? "target is null" : "callback is null";
            msg += ",please check it.";
            console.log("%c "+ msg,"color:#FF0000");
            return;
        }
        var call = [];
        if (this.list.has(message)) {
            call = this.list.get(message);
        }
        for (var i = 0; i < call.length; ++i) {
            if (call[i].same(target)) {
                console.log("%c '" + message + "' has same callback.","color:#FF0000");
                return;
            }
        }
        call.push(new MessageSystemHelper(target, callbacks));
        this.list.set(message, call);
    }

    /**
     * 
     * @param message 消息
     * @param target 作用域(确保删除对应的回调)
     * @returns 
     */
    public static remove_message(message: String, target?) {
        if (target == null){
            console.log("%c remove '" + message +"' failed, target is null.","color:#FF0000");
            return;
        }
        var call: MessageSystemHelper[] = this.list.get(message);
        if (call) {
            for (var i = 0; i < call.length; ++i) {
                if (call[i].same(target)) {
                    call.splice(i, 1);
                    break;
                }
            }
            if (call.length > 0) {
                this.list.set(message, call);
            } else {
                this.list.remove(message);
            }
        }
    }

    /**
     * 清理消息机
     */
    public static clear_message() {
        this.list.clear();
    }

    /**
     * 
     * @param message 消息
     * @param arg 参数,如需要带参回调,需提前处理,同时需要注意如果使用匿名函数需要注意作用域, MessageSystem.add_message("msg",target,(arg?)=>{if(arg)console.log(arg)})
     */
    public static send_messasge(message, arg?) {
        //遍历字典中的记录,如果有则执行
        console.log("%c send message " + message,"color:#0000FF");
        var call: MessageSystemHelper[] = this.list.get(message);
        if (call) {
            for (var i = 0; i < call.length; ++i) {
                var fun: Function = call[i].callback;
                fun && fun(arg);
            }
        } else {
            console.log("%c send message '" + message + "' failed,'" + message + "' no function.","color:#FF0000");
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值