【发布时间】:2019-07-21 02:10:27
【问题描述】:
我一直在使用monogame 用C# 进行一些简单的游戏编程,目前我正在尝试从头开始创建一个基本的ECS。我意识到已经存在制作精良的 ECS 库,这更像是个人学习练习。
我现在的代码是这样的;
public class Entity{
private bool active;
IComponent[] Components;
public Entity(){
active = true;
}
public AddComponent(IComponent component){
}
public bool Active { get => active; set => active = value; }
}
由于 IComponent 是组件的接口,我想知道是否有一种方法可以在特定组件类型在运行时首次定义时为其定义全局数组索引?
例如;如果你有一个 Transform 组件和一个 CollisionComponent,那么当在运行时定义第一个 Transform 组件时,将为之后定义的每个 Transform 组件提供一个全局索引,碰撞器组件的行为方式相似,但索引号明显不同。
我正在寻找的 c++ 等价物是这样的:
inline ComponentID getNewComponentTypeID()
{
static ComponentID lastID = 0u;
return lastID++;
}
template <typename T>
inline ComponentID getComponentTypeID() noexcept
{
static_assert(std::is_base_of<Component, T>::value, "");
static ComponentID typeID = getNewComponentTypeID();
return typeID;
}
【问题讨论】:
标签: c# .net static components