私はゲームを開発する学校のプロジェクトに取り組んでいます。私たちは、私たちが持っているチームの1つによって作られたエンジンを使用しています. エンジンのビルドアップは私には不明確であり、アンチパターンのようです. しかし、誰も私にデザインの選択を明確にすることはできないようです. エンジンは「コンポーネントベース」の設計を使用することになっていますが、私にはわかりません。以下は、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 で、複合型とエンティティの両方をハイブリッド型のオブジェクトとして使用しています (ロジックがよくわかりません)。
Player.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 データのデータ構造を保持する「Rigidbody」と呼ばれるコンポーネントと、完全に仮想的なコンポーネントの基本クラスをオーバーライドするいくつかの関数のように?