TEST(NAME, DestroyingEntitiesDispatchesReallocationEvent) { World w; MockEntityManagerListener mock; EntityManager* em = new EntityManager(&w); em->event.addListener(&mock, "mock"); // uninteresting calls EXPECT_CALL(mock, onCreateEntityHelper(testing::_)) .Times(testing::AtLeast(0)); EXPECT_CALL(mock, onDestroyEntityHelper(testing::_)) .Times(testing::AtLeast(0)); EXPECT_CALL(mock, onEntitiesReallocatedHelper(testing::_)).Times(testing::AtLeast(0)); // ignore this call during entity creation - not important em->createEntity("entity1"); // will be destroyed via destroyEntities() em->createEntity("entity2"); // will be via destroyAllEntities // expect 4 calls, since 4 entities are being destroyed EXPECT_CALL(mock, onEntitiesReallocatedHelper(testing::_)) .Times(4); // 4 entities were created... em->destroyEntities("entity1"); em->destroyAllEntities(); Entity& e = em->createEntity("entity3"); em->destroyEntity(e); em->createEntity("entity4"); // destroyed by destructor delete em; }
int main() { ResourceManager resourceManager = ResourceManager(); resourceManager.addResourceDirectory("res/"); sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "ECS Test"); EntityManager entityManager = EntityManager(&window, 100); for (int i = 1; i < 10000; ++i) { int temp = entityManager.createEntity(sf::Vector2f(WINDOW_X * float(rand()) / RAND_MAX, WINDOW_Y * float(rand()) / RAND_MAX)); entityManager.attachComponent(temp, RenderComponent(resourceManager.getTexture("player.png"))); entityManager.attachComponent(temp, PhysicsComponent(sf::Vector2f(100 * (float(rand()) / RAND_MAX - 0.5), 100 * (float(rand()) / RAND_MAX - 0.5)))); } // Remove some entities. for (int i = 1; i < 3; ++i) { entityManager.destroyEntity(i); } // Remove some components. for (int i = 3; i < 10; ++i) { entityManager.detachComponent(i, RENDER); } sf::Clock clock; while (window.isOpen()) { // Handle events. sf::Event event; while (window.pollEvent(event)) { if(event.type == sf::Event::Closed) { window.close(); } } float frametime = clock.restart().asSeconds(); std::cout << (1 / frametime) << std::endl; window.clear(sf::Color::White); entityManager.update(frametime); window.display(); } }
TEST(NAME, DispatchesCreateAndDestroyEntityEvents) { World w; MockEntityManagerListener mock; EntityManager* em = new EntityManager(&w); em->event.addListener(&mock, "mock"); // uninteresting calls EXPECT_CALL(mock, onEntitiesReallocatedHelper(testing::_)) .Times(testing::AtLeast(0)); // interesting calls EXPECT_CALL(mock, onCreateEntityHelper(testing::_)) .Times(1); EXPECT_CALL(mock, onDestroyEntityHelper(testing::_)) .Times(1); Entity& e = em->createEntity("entity"); ASSERT_EQ(1, em->getEntityList().size()); em->destroyEntity(e); em->event.removeListener("mock"); delete em; }