Ejemplo n.º 1
0
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);
}
Ejemplo n.º 2
0
TEST(EntityFilter, Single) {
    EntityManager entityManager;
    // Set up filter
    EntityFilter<TestComponent<0>> filter;
    filter.setEntityManager(&entityManager);
    // Add component
    EntityId entityId = entityManager.generateNewId();
    entityManager.addComponent(
        entityId,
        make_unique<TestComponent<0>>()
    );
    // Check filter
    auto filteredEntities = filter.entities();
    EXPECT_EQ(1, filteredEntities.count(entityId));
    EXPECT_EQ(1, filteredEntities.size());
    // Remove component
    entityManager.removeComponent(
        entityId,
        TestComponent<0>::TYPE_ID
    );
    entityManager.processRemovals();
    // Check filter
    filteredEntities = filter.entities();
    EXPECT_EQ(0, filteredEntities.count(entityId));
    EXPECT_EQ(0, filteredEntities.size());
}
Ejemplo n.º 3
0
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));
}
Ejemplo n.º 4
0
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;
}
Ejemplo n.º 5
0
//--------------------------------------------------------------
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);
}
Ejemplo n.º 6
0
TEST(EntityFilter, Initialization) {
    EntityManager entityManager;
    // Add component
    EntityId entityId = entityManager.generateNewId();
    entityManager.addComponent(
        entityId,
        make_unique<TestComponent<0>>()
    );
    EXPECT_TRUE(nullptr != entityManager.getComponent(entityId, TestComponent<0>::TYPE_ID));
    // Set up filter
    EntityFilter<TestComponent<0>> filter;
    filter.setEntityManager(&entityManager);
    // Check filter
    auto filteredEntities = filter.entities();
    EXPECT_EQ(1, filteredEntities.count(entityId));
    EXPECT_EQ(1, filteredEntities.size());
}
Ejemplo n.º 7
0
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);
    }
}