cocos 消息机制框架(小白学习笔记)

目录结构

 Message类

import { _decorator, Component, Node } from 'cc';

//Message类
//消息类,不继承任何内容,只是一个消息,一个单纯的类
//消息发送时需要携带信息,本例中需要三条内容:1、UI/NPC/Enemy(Manager类) 2、ComponentBase类 3、要发送的内容
                                          

export class Message  {
   //类型
    _Type: number;
    //命令
    _Command: number;
    //参数
    _Content: any;

    //构造方法
    constructor(type, command, content) {

        this._Type = type;
        this._Command = command;
        this._Content = content;

        //export class MessageType{}类内变量用法
        // MessageType.Type_UI;
    }

}

//因为类型为数字,不好区分,所以增加一些静态变量定义消息类型(传递消息类型),"export":外界可以引用

export class MessageType {
    static Type_UI = 1;
    static Type_NPC = 2;
    static Type_Enemy = 3;
    static Type_Audio = 4;

    static UI_RefreshHp = 101;
    static UI_RefreshScore = 102;

    static NPC_npc1 = 201;
    static NPC_npc2 = 202;

    static Enemy_enemy1 = 301;
    static Enemy_enemy2 = 302;
}


ComponentBase类

import { _decorator, Component, Node } from 'cc';
import { Message } from './Message';

//组件的基类,接受消息,所有该类的子类都能接收消息
export class ComponentBase extends Component {

    //接收消息
    ReceiveMessage(message: Message) {

    }

}


 ManagerBase类

import { _decorator, Component, Node } from 'cc';
//相当于include
import { ComponentBase } from './ComponentBase';
import { Message, MessageType } from './Message';
import { MessageCenter } from './MessageCenter';
const { ccclass, property } = _decorator;


//管理类,继承于ComponentBase

export class ManagerBase extends ComponentBase {

//管理的消息接收者数组
    ReceiveList: ComponentBase[] = [];

    //当前管理类接收的具体消息类型(Message.ts/MessageType)中定义的类型,本例中应该有4种,每一个类型一种
    //每一个管理类应该有不同的接收类型
    messageType: number;

    onLoad() {
        //设置当前管理类接收的消息类型
        //每次执行,调用下边的SetMessageType()方法,确定消息的类型
        this.messageType = this.SetMessageType();

        //把管理类添加到消息中心列表中,因为import了 { MessageCenter },所以能MessageCenter.Managers.push(this)把自己加进去
        MessageCenter.Managers.push(this);

    }

            //设置当前管理类接收的消息类型的方法

    SetMessageType() {

        return MessageType.Type_UI;

    }

    //注册消息监听、将小类传进来(背包、NPC...)数组
    RegisterReceiver(cb: ComponentBase) {
        this.ReceiveList.push(cb);
    }

    //重写接收消息的方法
    ReveiveMessage(message: Message) {

        //先调用父类的方法
        super.ReceiveMessage(message);

        //判断消息类型
        //if (message._Type != this.messageType) {
        if (message._Type != this.messageType) {
        return;
        }
        //向下层分发消息、遍历ReceiveList数组中的cb?
        for (let cb of this.ReceiveList) {
            //cb 接收消息的方法将message传过去
            cb.ReceiveMessage(message);
            console.debug("is ok ???");
        }
    }
}

//具体的管理类例子

//class EnemyManager extends EnemyManager {

//    SetMessageType() {
//        return MessageType.Type_Enemy;
//    }
//}

MessageCenter类

import { _decorator, Component, Node } from 'cc';
import { ComponentBase } from './ComponentBase';
import { Message } from './Message';
const { ccclass, property } = _decorator;

//单独存在,不需要继承于类,功能就是发送消息
export class MessageCenter  {

    //管理类列表
    static Managers: ComponentBase[] = [];

    //发送消息类
    static SendMessage(msg: Message) {
        //给本数组内的所有成员转发消息,遍历static Managers: ComponentBase[] = [],
        for (let manager of this.Managers) {
            manager.ReceiveMessage(msg);
            
        }
    }

    //简化发送消息

    static SendCustomMessage(type: number, commade: number, content: any) {
        //组装成一个消息对象
        let msg = new Message(type, commade, content);
        //调用上面的SendMessage(){}方法
        this.SendMessage(msg);

        console.log("thismessagecenter");

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我救我自己

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值