Added librarys and test application
This commit is contained in:
54
include/entityx/deps/Dependencies.h
Normal file
54
include/entityx/deps/Dependencies.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Alec Thomas <alec@swapoff.org>
|
||||
* 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 <alec@swapoff.org>
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "entityx/System.h"
|
||||
#include "entityx/Event.h"
|
||||
#include "entityx/Entity.h"
|
||||
|
||||
namespace entityx {
|
||||
namespace deps {
|
||||
|
||||
/**
|
||||
* An entityx::System for declaring component dependencies.
|
||||
*
|
||||
* eg. To declare that a `Physics` component must always be paired with `Position`
|
||||
* and `Direction` components:
|
||||
*
|
||||
* system_manager->add<Dependency<Physics, Position, Direction>>();
|
||||
*/
|
||||
template <typename C, typename ... Deps>
|
||||
class Dependency : public System<Dependency<C, Deps...>>, public Receiver<Dependency<C, Deps...>> {
|
||||
public:
|
||||
void receive(const ComponentAddedEvent<C> &event) {
|
||||
assign<Deps...>(event.entity);
|
||||
}
|
||||
|
||||
void configure(EventManager &events) override {
|
||||
events.subscribe<ComponentAddedEvent<C>>(*this);
|
||||
}
|
||||
|
||||
void update(EntityManager &entities, EventManager &events, TimeDelta dt) override {}
|
||||
|
||||
private:
|
||||
template <typename D>
|
||||
void assign(Entity entity) {
|
||||
if (!entity.component<D>()) entity.assign<D>();
|
||||
}
|
||||
|
||||
template <typename D, typename D1, typename ... Ds>
|
||||
void assign(Entity entity) {
|
||||
assign<D>(entity);
|
||||
assign<D1, Ds...>(entity);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace deps
|
||||
} // namespace entityx
|
||||
64
include/entityx/deps/Dependencies_test.cc
Normal file
64
include/entityx/deps/Dependencies_test.cc
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Alec Thomas <alec@swapoff.org>
|
||||
* 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 <alec@swapoff.org>
|
||||
*/
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
|
||||
#include "entityx/3rdparty/catch.hpp"
|
||||
#include "entityx/deps/Dependencies.h"
|
||||
#include "entityx/quick.h"
|
||||
|
||||
|
||||
namespace deps = entityx::deps;
|
||||
|
||||
|
||||
struct A : public entityx::Component<A> {};
|
||||
struct B : public entityx::Component<B> {
|
||||
explicit B(bool b = false) : b(b) {}
|
||||
|
||||
bool b;
|
||||
};
|
||||
struct C : public entityx::Component<C> {};
|
||||
|
||||
TEST_CASE_METHOD(entityx::EntityX, "TestSingleDependency") {
|
||||
systems.add<deps::Dependency<A, B>>();
|
||||
systems.configure();
|
||||
|
||||
entityx::Entity e = entities.create();
|
||||
REQUIRE(!static_cast<bool>(e.component<A>()));
|
||||
REQUIRE(!static_cast<bool>(e.component<B>()));
|
||||
e.assign<A>();
|
||||
REQUIRE(static_cast<bool>(e.component<A>()));
|
||||
REQUIRE(static_cast<bool>(e.component<B>()));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(entityx::EntityX, "TestMultipleDependencies") {
|
||||
systems.add<deps::Dependency<A, B, C>>();
|
||||
systems.configure();
|
||||
|
||||
entityx::Entity e = entities.create();
|
||||
REQUIRE(!static_cast<bool>(e.component<A>()));
|
||||
REQUIRE(!static_cast<bool>(e.component<B>()));
|
||||
REQUIRE(!static_cast<bool>(e.component<C>()));
|
||||
e.assign<A>();
|
||||
REQUIRE(static_cast<bool>(e.component<A>()));
|
||||
REQUIRE(static_cast<bool>(e.component<B>()));
|
||||
REQUIRE(static_cast<bool>(e.component<C>()));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(entityx::EntityX, "TestDependencyDoesNotRecreateComponent") {
|
||||
systems.add<deps::Dependency<A, B>>();
|
||||
systems.configure();
|
||||
|
||||
entityx::Entity e = entities.create();
|
||||
e.assign<B>(true);
|
||||
REQUIRE(e.component<B>()->b);
|
||||
e.assign<A>();
|
||||
REQUIRE(e.component<B>()->b);
|
||||
}
|
||||
Reference in New Issue
Block a user