本文整理汇总了C++中EntityManager::addComponent方法的典型用法代码示例。如果您正苦于以下问题:C++ EntityManager::addComponent方法的具体用法?C++ EntityManager::addComponent怎么用?C++ EntityManager::addComponent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EntityManager
的用法示例。
在下文中一共展示了EntityManager::addComponent方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(EntityFilter, Optional) {
EntityManager entityManager;
using TestFilter = EntityFilter<
TestComponent<0>,
Optional<TestComponent<1>>
>;
// Set up filter
TestFilter filter;
filter.setEntityManager(&entityManager);
TestFilter::EntityMap filteredEntities = filter.entities();
// Add first component
EntityId entityId = entityManager.generateNewId();
entityManager.addComponent(
entityId,
make_unique<TestComponent<0>>()
);
// Check filter
filteredEntities = filter.entities();
EXPECT_EQ(1, filteredEntities.count(entityId));
EXPECT_EQ(1, filteredEntities.size());
// Check group
TestFilter::ComponentGroup group = filteredEntities[entityId];
EXPECT_TRUE(std::get<0>(group) != nullptr);
EXPECT_TRUE(std::get<1>(group) == nullptr);
// Add second component
entityManager.addComponent(
entityId,
make_unique<TestComponent<1>>()
);
// Check filter
filteredEntities = filter.entities();
EXPECT_EQ(1, filteredEntities.count(entityId));
EXPECT_EQ(1, filteredEntities.size());
// Check group
group = filteredEntities[entityId];
EXPECT_TRUE(std::get<0>(group) != nullptr);
EXPECT_TRUE(std::get<1>(group) != nullptr);
// Remove component
entityManager.removeComponent(
entityId,
TestComponent<1>::TYPE_ID
);
entityManager.processRemovals();
// Check filter
filteredEntities = filter.entities();
EXPECT_EQ(1, filteredEntities.count(entityId));
EXPECT_EQ(1, filteredEntities.size());
// Check group
group = filteredEntities[entityId];
EXPECT_TRUE(std::get<0>(group) != nullptr);
}
示例2: filter
TEST(EntityFilter, Record) {
EntityManager entityManager;
using TestFilter = EntityFilter<
TestComponent<0>
>;
// Set up filter
TestFilter filter(true);
filter.setEntityManager(&entityManager);
TestFilter::EntityMap filteredEntities = filter.entities();
// Add first component
EntityId entityId = entityManager.generateNewId();
entityManager.addComponent(
entityId,
make_unique<TestComponent<0>>()
);
// Check added entities
EXPECT_EQ(1, filter.addedEntities().count(entityId));
// Remove component
entityManager.removeComponent(
entityId,
TestComponent<0>::TYPE_ID
);
entityManager.processRemovals();
// Check removed entities
EXPECT_EQ(1, filter.removedEntities().count(entityId));
}
示例3: createEntity
int createEntity(EntityManager& entityManager) {
int id = entityManager.createEntity();
std::cout << "Entity ID: " << id << std::endl;
// std::unique_ptr<PositionComponent> position{new PositionComponent{sf::Vector2f{0, 0}}};
// entityManager.addComponent(id, std::move(position));
entityManager.addComponent(id, new PositionComponent{sf::Vector2f{(float) id, 4432}});
return id;
}
示例4: setup
//--------------------------------------------------------------
void testApp::setup() {
world = new World();
EntityManager * em = world->getEntityManager();
world->getSystemManager()->addSystem(new ecs::MotionSystem(world));
world->getSystemManager()->addSystem(new ecs::RenderSystem(world));
for (int i = 0; i < 100; i++) {
Entity * e = em->createEntity();
em->addComponent(e->id, new TransformComponent(rand() % 1024, rand() % 768));
em->addComponent(e->id, new MotionComponent(randPlusMinus(100), randPlusMinus(100)));
em->addComponent(e->id, new RenderComponent(new CircleRenderer(rand() % 5, rand() % 255, rand() % 255, rand() % 255)));
}
ofBackground(0, 0, 0);
}
示例5:
void
EntityManager::transferEntity(
EntityId oldEntityId,
EntityId newEntityId,
EntityManager& newEntityManager,
const ComponentFactory& componentFactory
){
for (const auto& pair : m_impl->m_collections) {
Component* component = pair.second->get(oldEntityId);
if (component != nullptr){
if (not component->isVolatile() and not
(m_impl->m_volatileEntities.count(oldEntityId) > 0)
) {
auto newComponent = componentFactory.load(componentFactory.getTypeName(pair.first), component->storage());
newComponent->setOwner(newEntityId);
newEntityManager.addComponent(newEntityId, std::move(newComponent));
}
}
m_impl->m_entitiesToRemove.push_back(oldEntityId);
}
}