C++ 实体组件体系结构:要拆分大实体->;难以重构
在开发的第一步,我将C++ 实体组件体系结构:要拆分大实体->;难以重构,c++,design-patterns,maintainability,entity-system,entity-component-system,C++,Design Patterns,Maintainability,Entity System,Entity Component System,在开发的第一步,我将Car和AI设计为一个实体。 它工作得很好(伪代码):- for(每个实体都是“赛车”){ //^通过使用标志了解类型 //或迭代特殊组件(例如“RacingCarComponent”) 实体=。。。 AI*AI=get 上述代码将更新为:- for(every entity that is "racing car"){ Entity entity=... AttachAI* aiAttach=get<AttachAI>(entity); /
Car
和AI
设计为一个实体。它工作得很好(伪代码):-
for(每个实体都是“赛车”){
//^通过使用标志了解类型
//或迭代特殊组件(例如“RacingCarComponent”)
实体=。。。
AI*AI=get
上述代码将更新为:-
for(every entity that is "racing car"){
Entity entity=...
AttachAI* aiAttach=get<AttachAI>(entity); //<-- edit
aiAttach->ai->setInformation(...) //<-- edit
}
for(every entity that is "bicycle"){
Entity entity=...
AttachAI* aiAttach=get<AttachAI>(entity); //<-- edit
aiAttach->ai->setInformation(...) //<-- edit
}
for(每个实体都是“赛车”){
实体=。。。
AtthiA*AAtAd= = GET(实体);/AI> SETIVATION(…)/SETIVATIONE(…)//< P>可能是错误的,但看起来好像您可能多次循环遍历所有实体,检查一个条件。我对C++语法不太确定,请容忍我:
for (entities as entity) {
info = null;
//Check type to get specific info
if (type is a "racing car"){
info = "fast car";
}
elseif (type is a "bicycle") {
info = "rad spokes";
}
//If we found info, we know we had a valid type
if (info isnt null) {
aiAttach = get(entity);
aiAttach->ai->setInformation(info);
}
}
我不确定get函数是否需要特定于每种类型的任何内容。在我的伪代码示例中,我假设我们只发送实体,而不是特定于类型的内容。如果是,则可以使用其他变量。如果只有少数类型(赛车+自行车),将所有内容填充到1个循环中就可以了。但是,当执行部分喜欢在许多文件(10+)中生存时,这将不适用。不过,这个答案为我打开了一个新的思维方式。我将尝试用其他奇怪的方式解决它。谢谢。
for (entities as entity) {
info = null;
//Check type to get specific info
if (type is a "racing car"){
info = "fast car";
}
elseif (type is a "bicycle") {
info = "rad spokes";
}
//If we found info, we know we had a valid type
if (info isnt null) {
aiAttach = get(entity);
aiAttach->ai->setInformation(info);
}
}