/* * Copyright (C) 2012 Alec Thomas * All rights reserved. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. * * Author: Alec Thomas */ #define CATCH_CONFIG_MAIN #include #include #include "entityx/3rdparty/catch.hpp" #include "entityx/System.h" #include "entityx/quick.h" // using namespace std; using namespace entityx; using std::string; struct Position : Component { explicit Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} float x, y; }; struct Direction : Component { explicit Direction(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} float x, y; }; struct Counter : Component { explicit Counter(int counter = 0) : counter(counter) {} int counter; }; class MovementSystem : public System { public: explicit MovementSystem(string label = "") : label(label) {} void update(EntityManager &es, EventManager &events, TimeDelta) override { EntityManager::View entities = es.entities_with_components(); ComponentHandle position; ComponentHandle direction; for (auto entity : entities) { entity.unpack(position, direction); position->x += direction->x; position->y += direction->y; } } string label; }; class CounterSystem : public System { public: void update(EntityManager &es, EventManager &events, TimeDelta) override { EntityManager::View entities = es.entities_with_components(); Counter::Handle counter; for (auto entity : entities) { entity.unpack(counter); counter->counter++; } } }; class EntitiesFixture : public EntityX { public: std::vector created_entities; EntitiesFixture() { for (int i = 0; i < 150; ++i) { Entity e = entities.create(); created_entities.push_back(e); if (i % 2 == 0) e.assign(1, 2); if (i % 3 == 0) e.assign(1, 1); e.assign(0); } } }; TEST_CASE_METHOD(EntitiesFixture, "TestConstructSystemWithArgs") { systems.add("movement"); systems.configure(); REQUIRE("movement" == systems.system()->label); } TEST_CASE_METHOD(EntitiesFixture, "TestApplySystem") { systems.add(); systems.configure(); systems.update(0.0); ComponentHandle position; ComponentHandle direction; for (auto entity : created_entities) { entity.unpack(position, direction); if (position && direction) { REQUIRE(2.0 == Approx(position->x)); REQUIRE(3.0 == Approx(position->y)); } else if (position) { REQUIRE(1.0 == Approx(position->x)); REQUIRE(2.0 == Approx(position->y)); } } } TEST_CASE_METHOD(EntitiesFixture, "TestApplyAllSystems") { systems.add(); systems.add(); systems.configure(); systems.update_all(0.0); Position::Handle position; Direction::Handle direction; Counter::Handle counter; for (auto entity : created_entities) { entity.unpack(position, direction, counter); if (position && direction) { REQUIRE(2.0 == Approx(position->x)); REQUIRE(3.0 == Approx(position->y)); } else if (position) { REQUIRE(1.0 == Approx(position->x)); REQUIRE(2.0 == Approx(position->y)); } REQUIRE(1 == counter->counter); } }