目标:
我正在尝试实现一个实体组件系统,但我坚持使用我的组件实现。我打算有一个通用组件类型,其中包含每个组件的静态位,例如位置类型的组件有位 = 0,精灵类型的组件有位 = 1,等等。一个组件应该只包含它持有的值因此我从结构开始。以下是我对此的尝试(不工作的代码):
struct Position : Component<Position> //this does not work in c#
{
public int x, y;
}
internal struct ComponentBit
{
public static int bitCounter = 0;
};
public struct Component<T>
{
public static readonly int bit;
static Component()
{
bit = ComponentBit.bitCounter++;
}
public int GetBit()
{
return bit;
}
}
之后我发现无法继承结构,因此我尝试将结构更改为有效的类。
问题:
有没有办法像在 C++(模板)中那样使用结构来实现这些功能?出于以后的实现/性能原因,我想将它们保留为值类型而不是引用类型。
编辑1:
我想要的预期用途是:
Position pos = new Position(x, y);
int bit1 = pos.GetBit(); // or
int bit2 = Position.bit; // both should be possible