【问题标题】:Allocation of object manager? for MMORPG对象管理器的分配?用于大型多人在线角色扮演游戏
【发布时间】:2013-02-02 09:17:22
【问题描述】:

谁能提出一个干净的方法来做到这一点。我想要一个包含游戏中所有实例化对象的世界。但我不确定如何正确投射。

所有对象都应该在 1 个伞形对象和 1 个字典中,以便于保存/搜索。

//all connected players
private Dictionary<IOClient, string> IOClients = new Dictionary<IOClient, string>();

player = new MObject(new object[]{result.Remove(0,1)}, 1);



public class MObject
{
    private Serial m_Serial;
    public Serial Serial { get { return m_Serial; } }

    public Player player = null;
    public Item item = null;

    public MObject(object[] obj, int type)
    {
      //  m_Serial = Serial.NewMobile;
        if (obj.GetType() == typeof(Player) || type == 1)
        {
            player = new Player((string)obj[0]);
        }
        if (obj.GetType() == typeof(Item) || type == 2)
        {
         //   item = new Item();
        }
    }
}


//Also a World
public static class World
{
    private static bool m_Loading;
    private static bool m_Loaded;

    private static Dictionary<Serial, MObject> m_MObjects;

    public static Dictionary<Serial, MObject> MObjects
    { get { return m_MObjects; } }
}

【问题讨论】:

  • 请将代码内联到问题中。
  • @AlexeiLevenkov 添加到 OP :)
  • 已更新。应该不错。
  • 也许你可以真正解释你的问题?
  • 一个类型化字典的字典怎么样(每个类型化字典只包含特定(基本)类型的对象)?

标签: c# oop object


【解决方案1】:

听起来这里的核心问题是弱类型与强类型之一。

一种可能的解决方案是为您的对象引入接口,并为每个接口提供一个或多个字典。

例如

您有所有 IDrawable 对象的字典,所有 ICanShoot 对象的字典,所有 ICanBeShot 对象的字典等等。

这样,每次您使用其中一个字典时,您都不需要强制转换......并且您可以获得松散耦合等额外的好处。

如果您想进一步扩展这个想法,可以将字典包装到 IOC 容器中......但这本身就是一个完整的主题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2017-06-12
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多