技术笔记(2)MMORPG架构

技术笔记(2)MMORPG架构

  • 希望实现的功能或目标:

    • 一个功能完整的接近商业案例的MMORPG游戏项目
    • 搭建起该游戏项目的基本架构

  • 学习笔记:

    • IOCContainer类

      • 用以保存所有层级以及各个模块的实例

      • 实例字典

        • private Dictionary<Type,object> instancesDict = new Dictionary<Type,object>();​​
      • 注册方法

        •     public void Register<T>(T instance)
              {
                  var key = typeof(T);
                  if (instancesDict.ContainsKey(key))
                  {
                      instancesDict[key] = instance;
                  }
                  else
                  {
                      instancesDict.Add(key, instance);
                  }
              }
          
      • 获取实例方法

        • public T Get<T>() where T : class
          {
              var key = typeof(T);
              object obj = null;
              if(instancesDict.TryGetValue(key,out obj))
              {
                  return obj as T;
              }
              else
              {
                  Debug.Log("想要获取的对象为空");
              }
              return null;
          }
          
      • 调用容器所有实例初始化方法

        • public void InitAllModules()
          {
          
          }
          
    • EventRegistration类

      • 委托:public Action<object> OnEvent = obj => { };​​
    • GameEventSystem类

      • 事件字典:private Dictionary<Type, IEventRegistration> eventRegistrationsDict = new Dictionary<Type, IEventRegistration>();​​

      • 事件注册方法:

        • public void Regist<T>(Action<object> onEvent)
          {
              var type = typeof(T);
              IEventRegistration eventRegistration;
              if(eventRegistrationsDict.TryGetValue(type,out eventRegistration))
              {
                  (eventRegistration as EventRegistration).OnEvent += onEvent;
              }
              else
              {
                  eventRegistration = new EventRegistration() 
                  { 
                      OnEvent = onEvent
                  };
                  eventRegistrationsDict.Add(type, eventRegistration);
              }
          }
          
      • 事件发送方法:

        • public void Send<T>(object obj) where T : new()
          {
              var type = typeof(T);
              IEventRegistration eventRegistration;
              if (eventRegistrationsDict.TryGetValue(type, out eventRegistration))
              {
                  (eventRegistration as EventRegistration).OnEvent.Invoke(obj);
              }
          }
          
      • 事件注销方法:

        • public void UnRegist<T>(Action<object> onEvent)
          {
              var type = typeof(T);
              IEventRegistration eventRegistration;
              if (eventRegistrationsDict.TryGetValue(type, out eventRegistration))
              {
                  (eventRegistration as EventRegistration).OnEvent -= onEvent;
              }
          }
          
    • 架构抽象基类 abstract class Architecture

      • 实例化IOC容器:private IOCContainer IOCContainer = new IOCContainer();​​

      • 实例化游戏事件系统:private GameEventSystem gameEventSystem = new GameEventSystem();​​

      • 抽象初始化方法

        • protected abstract void Init();
          
      • 无参构造函数

        • public Architecture() { Init(); }
          
      • 初始化所有模块方法

        • public void InitAllModules()
          {
              IOCContainer.InitAllModules();
          }
          
      • 注册模型方法

        • public void RegistModel<U>(U instance) where U : IModel
          {
              IOCContainer.Register<U>(instance);
          }
          
      • 注册系统方法(同上)

      • 注册工具方法(同上)

      • 获取模型层对象方法

        • U IArchitecture.GetModel<U>()
          {
              return IOCContainer.Get<U>();
          }
          
      • 获取系统层对象方法(同上)

      • 获取工具层对象方法(同上)

      • 发送事件方法

        • public void SendEvent<U>(object dataObj) where U : new()
          {
              gameEventSystem.Send<U>(dataObj);
          }
          
      • 注册事件方法

        • public void RegistEvent<U>(Action<object> onEvent) where U : new()
          {
              gameEventSystem.Regist<U>(onEvent);
          }
          
      • 注销事件方法

        • public void UnRegistEvent<U>(Action<object> onEvent) where U : new()
          {
              gameEventSystem.UnRegist<U>(onEvent);
          }
          
      • 发送命令方法

        • public void SendCommand<U>(object dataObj) where U : ICommand, new()
          {
              var command = new U();
              command.Execute(dataObj);
          }
          

  • 实现过程中产生的疑惑:

    • public Action OnEvent = obj => { };
    • 为什么Architecture里面也要写事件的注册注销发送方法?
    • SendCommand(object dataObj)
    • Architecture的泛型T和IArchitecture的泛型U是什么关系
    • 为什么Architecture要做成抽象类?
      • 对疑惑的解答:

        • Action表示这个变量是一个Action类型的泛型委托,他可以指向一个接受一个object类型的参数,没有返回值的方法。
          obj=>{}表示这个变量的值是一个匿名方法,obj代表参数名称,{}表示方法体内容。

        • Architecture的事件相关方法,调用的其实还是事件系统里的方法,我个人的理解是在事件系统之上再套一层框架,用以和IOC联动,即统一由Architecture来管理和提供方法,避免我们直接访问事件系统类。

        • U表示这个方法的的泛型参数,它可以是任何实现了ICommand接口的类,比如MoveCommand、RotateCommand等。
          object dataObj表示这个方法的参数,它可以是任何类型的对象,它会作为命令的数据传递的命令的执行者。

        • 此处T和U的关系:

          • T表示继承自Architecture类的子类的类型,比如GameArchitecture、UIArchitecture等。
          • U表示不同的游戏模块或功能的类型,比如IModel、ISystem、IUtility、ICommand等。
        • 因为此处的Architecture类只是一个基类,还需要去实现继承自它的其他框架子类。

        • 日期:3.4

posted @   静候霜白  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示