我正在用 ECS 和面向数据的模型编写引擎。我试图避免继承和动态调度,以避免每次update()
调用都破坏缓存。我想出的是:
struct transformComponent {
const unsigned short id;
vecShort p;
transformComponent(unsigned short id, short x, short y): p(x, y), id(id) {}
transformComponent(unsigned short id): p(0, 0), id(id) {}
};
struct physicsComponent {
const unsigned short id;
unsigned short mass;
double invmass;
vecShort v, a, f;
physicsComponent(unsigned short id, unsigned short mass):
id(id), mass(mass), invmass(1/mass) {}
physicsComponent(unsigned short id): id(id) {}
};
//...
struct component {
enum compType {transform, physics, behavior, collision, rendering};
union {
transformComponent tra;
physicsComponent phy;
//...
};
template<typename ...argtypes>
component(compType type, unsigned short id, argtypes... args) {
switch(type) {
case transform:
tra(id, std::forward<argtypes>(args)...);
break;
case physics:
phy(id, std::forward<argtypes>(args)...);
break;
//...
}
}
};
然后我有一个room
通过内存池保存所有系统和组件的类(管理器)。问题是
template<typename ...argtypes>
component(compType type, unsigned short id, argtypes... args) {
switch(type) {
case transform:
tra(id, std::forward<argtypes>(args)...);
break;
编译器抱怨的部分:
src\inc/components.h: In instantiation of 'component::component(component::compType, short unsigned
int, argtypes ...) [with argtypes = {}]':
src\inc/components.h:94:71: required from here
src\inc/components.h:57:5: error: no match for call to '(transformComponent) (short unsigned int&)'
57 | tra(id, std::forward<argtypes>(args)...);
| ^~~
我尝试移动椭圆,但没有效果。我实际上想要做的是避免在每一帧上同时使用模板和动态调度,但我需要模板进行初始化。我有一些基本语法错误吗?我完全错过了什么吗?我是面向数据的范式的新手,所以我很乐意接受任何建议。最小的可重现示例:https ://godbolt.org/z/zGAfXS