保存相关常量的最佳实践?

发布于 2024-10-18 06:11:51 字数 1222 浏览 1 评论 0 原文

在我的游戏中,我有一个具有不同 GameEntityType 的枚举(大约 200 个条目长)。

保存游戏时,仅将 GameEntityType 数组写入保存文件。

重建游戏世界,并显示有关 GameEntityTypes 的各种信息 我需要一个包含更多额外细节(常量值)的列表。 这意味着每个 GameEntityType 都有与其关联的其他值。 这些值每帧都会被频繁访问。 我的目标是有一种简单的方法来从 GameEntityTypeID (枚举)中获取所有额外信息。 例如,当我从保存文件中读取 0x1 时,我可以简单地访问名称和所有其他 数组中的数字/枚举隐含的信息如下:“Constants.TextureList[GameEntityType]”,将返回字符串 “Stone_Texture.DDS”

附加示例/第一个枚举条目的关联信息:

类型: 字符串,字符串,Flag-Enum:“视觉分类”布尔布尔 价值观: "StoneBlock""Stone_Texture.DDS"0x0102falsetrue

我的第一个方法是创建一个静态 GameEntityTypeInfo 类 对于每个附加信息类型都有一个如下所示的成员:

public static const string[] = {"StoneBlock", ...[more Entrys]};

当然,这是一个糟糕的解决方案,因为我不能只添加 GameEntityType 任何我想要的地方,而不必也更新所有其他列表。 我还必须将逻辑单元分离为数据类型单元(这是一个问题!因为当我决定不再需要特定的 EntityType 时,我必须遍历 6 个以上的列表!)

接下来,我尝试解决这个问题通过从这些数据集中创建一个结构 并将它们添加到静态数组中。 但是在构造函数时(游戏开始时)创建一个包含附加信息的列表 似乎仍然不是最好的解决方案。

问题:如何创建对这些常量值的快速(名称/分类查找将在每帧中至少使用 5000 次)并轻松访问(最好使用索引器)?

与此类似的查找最好是“Constants.TextureList[GameEntityType]”

In my game I have a enum (about 200 entrys long) with different GameEntityTypes.

When the game is saved, only an array of GameEntityType is written to the savefile.

To reconstruct the gameworld, and to display various information about the GameEntityTypes
I need to have a list of quite a few more extra details (constant values).
This means that each GameEntityType has other values associated with it.
These values are accessed very often every frame.
My goal is to have a simple way to get all the extra information out of the GameEntityTypeID (the enum).
For example, when I read a 0x1 out of the savefile, I can simply access the Name and all other
information that is implied with the number/enum from an array like this: "Constants.TextureList[GameEntityType]", Would return the string "Stone_Texture.DDS"

Example for additional/associated information of the first enum entry:

Types:
string, string, Flag-Enum: "Visual-Classification", bool, bool
Values:
"StoneBlock", "Stone_Texture.DDS", 0x0102, false, true

My first approach to this was to create a static GameEntityTypeInfo class that
had a member like the following for every additional info type:

public static const string[] = {"StoneBlock", ...[more entrys]};

Of course this is a terrible solution, because I can't just add GameEntityType's
wherever I want without having to update all other lists too.
I also have to separate logical units into datatype units (which is a problem! because when I decide that I don't need an specific EntityType anymore, I have to go trough more than 6 lists!)

Next, I tried to tackle this problem by making a struct out of these datasets
and adding them to a static array.
But creating a List with the additional info while the constructor (while the game is starting)
still doesn't seem like the best solution.

Question: How can I create a fast(Name/Classification lookups will be used every frame at least 5000 times in total) and easy access (preferably with indexers) to these constant values?

A lookup similar to this would be best "Constants.TextureList[GameEntityType]"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-10-25 06:11:51

通过子类化:

public abstract class Enumeration : IComparable
{
private readonly int _value;
private readonly string _displayName;

protected Enumeration()
{
}

protected Enumeration(int value, string displayName)
{
    _value = value;
    _displayName = displayName;
}

public int Value
{
    get { return _value; }
}

public string DisplayName
{
    get { return _displayName; }
}

public override string ToString()
{
    return DisplayName;
}

public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
{
    var type = typeof(T);
    var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);

    foreach (var info in fields)
    {
        var instance = new T();
        var locatedValue = info.GetValue(instance) as T;

        if (locatedValue != null)
        {
            yield return locatedValue;
        }
    }
}

public override bool Equals(object obj)
{
    var otherValue = obj as Enumeration;

    if (otherValue == null)
    {
        return false;
    }

    var typeMatches = GetType().Equals(obj.GetType());
    var valueMatches = _value.Equals(otherValue.Value);

    return typeMatches && valueMatches;
}

public override int GetHashCode()
{
    return _value.GetHashCode();
}

public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
{
    var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value);
    return absoluteDifference;
}

public static T FromValue<T>(int value) where T : Enumeration, new()
{
    var matchingItem = parse<T, int>(value, "value", item => item.Value == value);
    return matchingItem;
}

public static T FromDisplayName<T>(string displayName) where T : Enumeration, new()
{
    var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == displayName);
    return matchingItem;
}

private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
{
    var matchingItem = GetAll<T>().FirstOrDefault(predicate);

    if (matchingItem == null)
    {
        var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
        throw new ApplicationException(message);
    }

    return matchingItem;
}

public int CompareTo(object other)
{
    return Value.CompareTo(((Enumeration)other).Value);
}
}

这里有一篇文章: http:// www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/12/enumeration-classes.aspx

By subclassing:

public abstract class Enumeration : IComparable
{
private readonly int _value;
private readonly string _displayName;

protected Enumeration()
{
}

protected Enumeration(int value, string displayName)
{
    _value = value;
    _displayName = displayName;
}

public int Value
{
    get { return _value; }
}

public string DisplayName
{
    get { return _displayName; }
}

public override string ToString()
{
    return DisplayName;
}

public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
{
    var type = typeof(T);
    var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);

    foreach (var info in fields)
    {
        var instance = new T();
        var locatedValue = info.GetValue(instance) as T;

        if (locatedValue != null)
        {
            yield return locatedValue;
        }
    }
}

public override bool Equals(object obj)
{
    var otherValue = obj as Enumeration;

    if (otherValue == null)
    {
        return false;
    }

    var typeMatches = GetType().Equals(obj.GetType());
    var valueMatches = _value.Equals(otherValue.Value);

    return typeMatches && valueMatches;
}

public override int GetHashCode()
{
    return _value.GetHashCode();
}

public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
{
    var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value);
    return absoluteDifference;
}

public static T FromValue<T>(int value) where T : Enumeration, new()
{
    var matchingItem = parse<T, int>(value, "value", item => item.Value == value);
    return matchingItem;
}

public static T FromDisplayName<T>(string displayName) where T : Enumeration, new()
{
    var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == displayName);
    return matchingItem;
}

private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
{
    var matchingItem = GetAll<T>().FirstOrDefault(predicate);

    if (matchingItem == null)
    {
        var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
        throw new ApplicationException(message);
    }

    return matchingItem;
}

public int CompareTo(object other)
{
    return Value.CompareTo(((Enumeration)other).Value);
}
}

An article here: http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/12/enumeration-classes.aspx

瑕疵 2024-10-25 06:11:51

不确定我是否完全理解您想要了解的内容,但如果您需要快速查找,那么听起来您正在寻找的是 Dictionary>

Not sure if I understand exactly what you're trying to get at but it sounds like what you're looking for is a Dictionary<string, List<string>> if you're after fast lookups.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文