エンティティに明示的な「タイプ」(例:プレーヤー)がなく、単にコンポーネントのコレクションである場合、システムで作業する必要があるエンティティとそうでないエンティティを識別するにはどうすればよいですか?たとえば、ポンのゲームでは、パドルとボールの両方がウィンドウの境界に衝突します。ただし、それぞれの衝突処理システムは異なるため、システムは間違ったタイプのエンティティを処理するべきではありません。
void PlayerCollisionSystem::update(std::vector<Entity *> entities) {
typedef std::vector<Entity *>::iterator EIter;
for (EIter i = entities.begin(); i != entities.end(); ++i) {
Entity *player = *i; // How do I verify that the entity is a player?
// Get relevant components.
PositionComponent *position = player->getComponent<PositionComponent>();
VelocityComponent *velocity = player->getComponent<VelocityComponent>();
SpriteComponent *sprite = player->getComponent<SpriteComponent>();
// Detect and handle player collisions using the components.
}
}
プレーヤーとボールの両方が衝突処理に関連する同じコンポーネントタイプを共有していますが、システムの実装は異なります。
すべてのゲームエンティティのコンテナがある場合、Entity
などのメンバー変数を継承または含めずに特定のタイプのエンティティを識別するにはどうすればよいですか。std::string type
この場合、エンティティは単なるコンポーネントのコレクションではなくなりますか?