我正在开发一个游戏的学校项目。我们使用由我们拥有的一个团队制造的引擎。引擎的构建对我来说不清楚,似乎是一种反模式。然而,似乎没有人能让我清楚地选择设计。该引擎应该使用“基于组件”的设计,但我没有看到它。下面是 Component、Composite 和 Entity 类的代码。简而言之,我的问题是:这段代码是使用有效的设计模式还是仅仅为了“实现设计模式”而过于复杂,从而导致反模式?
组件.cpp:
#include "Engine\Component.h"
#include "Engine\Composite.h"
Component::Component(Composite* parent)
{
this->parent = parent;
}
Component::~Component()
{
}
实体.cpp
#include "Engine\Entity.h"
#include "Engine\Game.h"
Entity::Entity(Composite* parent):Composite(parent)
{
this->mass = 1;
node = NULL;
}
void Entity::update()
{
Composite::update();
this->angularVelocity += this->angularAccelaration;
this->orientation += this->angularVelocity;
this->accelaration = (1 / this->mass) * this->force;
this->velocity += this->accelaration;
this->position += this->velocity;
if (node != NULL)
{
this->node->setPosition(this->position);
this->node->setRotation(this->orientation);
}
}
void Entity::draw()
{
Composite::draw();
if (node == NULL) return;
if (!this->visible)
{
this->node->setVisible(false);
return;
}
this->node->setVisible(true);
this->node->render();
}
void Entity::createNode(std::string modelPath)
{
// Get the mesh
irr::scene::IAnimatedMesh* mesh = Game::getSceneManager()->getMesh(modelPath.c_str());
// Create model entity
this->node = Game::getSceneManager()->addMeshSceneNode( mesh );
this->node->setMaterialFlag(EMF_FOG_ENABLE, true);
}
Entity::~Entity()
{
Composite::~Composite();
if (node != NULL)
{
node->drop();
}
}
复合材料.cpp
#include "Engine\Composite.h"
Composite::Composite(Composite* parent):Component(parent)
{
}
Composite::~Composite()
{
for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
{
delete (*i);
}
components.clear();
}
void Composite::handleMessage(unsigned int message, void* data)
{
for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
{
(*i)->handleMessage(message, data);
}
}
void Composite::update()
{
for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
{
(*i)->update();
}
}
void Composite::draw()
{
for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
{
(*i)->draw();
}
}
void Composite::addComponent(Component* component)
{
components.push_back(component);
}
void Composite::removeComponent(Component* component)
{
components.remove(component);
delete component;
}
下一段代码是 Player.cpp,它使用复合和实体作为混合类型的对象(我真的不明白逻辑)。
播放器.cpp
#include "Player.h"
#include "Messages.h"
#include <iostream>
Player::Player(Composite* parent) : Entity(parent)
{
createNode("../assets/sydney.md2");
//i = 0;
//v3f = vector3df(0,0,0);
/*networker = new NetworkComponent();
addComponent(networker);
networker->registerVar(&i);
networker->registerVar(&v3f);*/
}
void Player::update() {
Composite::update();
//std::cout<<i<<std::endl;
//std::cout<<"vectorx="<<v3f.X<<"\n";
}
void Player::handleMessage(unsigned int message, void* data) {
switch(message) {
case DAMAGE: /* Do something */;
}
delete data;
}
Player::~Player()
{
Entity::~Entity();
}
我根本不相信这是基于组件的设计。不应该删除实体,只使用复合和组件。组件基类不应该是空的,从不直接使用吗?就像一个名为“Rigidbody”的组件,它拥有一个用于刚体数据的数据结构和一些覆盖完全虚拟组件基类的函数?