【发布时间】:2015-01-31 21:57:40
【问题描述】:
我正在尝试编写我自己的实体组件系统,并且我想要一种快速(可能的时间恒定)而且内存高效的方式来访问实体的组件。
使用全局映射 (std::unordered_map) 将每个实体映射到包含其组件的排序向量:
Key Value
--- -----
EntityA -> Sorted vector of components
EntityB -> Sorted vector of components
[..] -> [..]
每个实体都有一个位集,指示实体具有哪些组件,即。 :
|C0 |C1 |C2 |C3 |C4 |C5
0 0 1 0 1 1
and on the map:
Key Value
--- -----
EntityA -> [C2,C4,C5]
由于组件很少添加,我可以负担排序插入的成本,但我绝对想要快速访问。
现在我从 bitset 知道 C4 是第二个元素集(从左数),所以它应该在第二个向量索引处。
如何将它写入一个方法中,该方法将返回给定组件类型 ID 的实体的组件? 例如。
Component* getComponent(ComponentID id){ // component id of C5 should be 6 since it occupies the 6th position of the bitset
return [..];
}
【问题讨论】:
标签: c++