Documentation for module 'NDK'
Former-commit-id: a6c2075cfbfd0eccf2b77def71c0d42684bed590 [formerly 36ece2bc6a148bde6cacf45084821d20edcd115e] [formerly 4a6988792ec026e65be6850c46dfe8ddda92a885 [formerly fd3f4f975de5c427f3adc98b220446fd255be396]] Former-commit-id: c87fdc9483202842267c60eff3d619f0df2963bf [formerly ee35202f1b2df7ca20da5b6d8b13147f2b92c933] Former-commit-id: dad5de1b00bb4413f7aa191ca06b7d43b659f32a
This commit is contained in:
22
tests/SDK/NDK/Application.cpp
Normal file
22
tests/SDK/NDK/Application.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <NDK/Application.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("Application", "[NDK][APPLICATION]")
|
||||
{
|
||||
GIVEN("An application")
|
||||
{
|
||||
Ndk::Application application;
|
||||
application.AddWorld();
|
||||
Nz::Window& window = application.AddWindow<Nz::Window>();
|
||||
|
||||
WHEN("We close the open window")
|
||||
{
|
||||
window.Close();
|
||||
|
||||
THEN("Application should close")
|
||||
{
|
||||
REQUIRE(!application.Run());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
64
tests/SDK/NDK/BaseSystem.cpp
Normal file
64
tests/SDK/NDK/BaseSystem.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <NDK/System.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/VelocityComponent.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
class TestSystem : public Ndk::System<TestSystem>
|
||||
{
|
||||
public:
|
||||
TestSystem()
|
||||
{
|
||||
Requires<Ndk::VelocityComponent>();
|
||||
Excludes<Ndk::NodeComponent>();
|
||||
}
|
||||
|
||||
~TestSystem() = default;
|
||||
|
||||
static Ndk::SystemIndex systemIndex;
|
||||
|
||||
private:
|
||||
void OnUpdate(float elapsedTime) override
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
Ndk::SystemIndex TestSystem::systemIndex;
|
||||
}
|
||||
|
||||
SCENARIO("BaseSystem", "[NDK][BASESYSTEM]")
|
||||
{
|
||||
GIVEN("Our TestSystem")
|
||||
{
|
||||
Ndk::World world;
|
||||
Ndk::BaseSystem& system = world.AddSystem<TestSystem>();
|
||||
REQUIRE(&system.GetWorld() == &world);
|
||||
|
||||
WHEN("We add an entity")
|
||||
{
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
entity->AddComponent<Ndk::VelocityComponent>();
|
||||
|
||||
THEN("System should have it")
|
||||
{
|
||||
world.Update(1.f);
|
||||
REQUIRE(system.HasEntity(entity));
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("We add an entity with excluded component")
|
||||
{
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
entity->AddComponent<Ndk::VelocityComponent>();
|
||||
entity->AddComponent<Ndk::NodeComponent>();
|
||||
|
||||
THEN("System should not have it")
|
||||
{
|
||||
world.Update(1.f);
|
||||
REQUIRE(!system.HasEntity(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
tests/SDK/NDK/Component.cpp
Normal file
31
tests/SDK/NDK/Component.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <NDK/Component.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
class TestComponent : public Ndk::Component<TestComponent>
|
||||
{
|
||||
public:
|
||||
static Ndk::ComponentIndex componentIndex;
|
||||
};
|
||||
|
||||
Ndk::ComponentIndex TestComponent::componentIndex;
|
||||
}
|
||||
|
||||
SCENARIO("Component", "[NDK][COMPONENT]")
|
||||
{
|
||||
GIVEN("Our TestComponent")
|
||||
{
|
||||
TestComponent testComponent;
|
||||
|
||||
WHEN("We clone it")
|
||||
{
|
||||
std::unique_ptr<Ndk::BaseComponent> clone = testComponent.Clone();
|
||||
|
||||
THEN("We should get a copy")
|
||||
{
|
||||
REQUIRE(dynamic_cast<TestComponent*>(clone.get()) != nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
tests/SDK/NDK/Entity.cpp
Normal file
101
tests/SDK/NDK/Entity.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Component.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
class UpdatableComponent : public Ndk::Component<UpdatableComponent>
|
||||
{
|
||||
public:
|
||||
bool IsUpdated()
|
||||
{
|
||||
return m_updated;
|
||||
}
|
||||
|
||||
void SetUpdated()
|
||||
{
|
||||
m_updated = true;
|
||||
}
|
||||
|
||||
static Ndk::ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
bool m_updated = false;
|
||||
};
|
||||
|
||||
Ndk::ComponentIndex UpdatableComponent::componentIndex;
|
||||
|
||||
class UpdateSystem : public Ndk::System<UpdateSystem>
|
||||
{
|
||||
public:
|
||||
UpdateSystem()
|
||||
{
|
||||
Requires<UpdatableComponent>();
|
||||
}
|
||||
|
||||
~UpdateSystem() = default;
|
||||
|
||||
static Ndk::SystemIndex systemIndex;
|
||||
|
||||
private:
|
||||
void OnUpdate(float elapsedTime) override
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
||||
{
|
||||
UpdatableComponent& updatable = entity->GetComponent<UpdatableComponent>();
|
||||
updatable.SetUpdated();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ndk::SystemIndex UpdateSystem::systemIndex;
|
||||
}
|
||||
|
||||
SCENARIO("Entity", "[NDK][ENTITY]")
|
||||
{
|
||||
GIVEN("A world & an entity")
|
||||
{
|
||||
Ndk::World world;
|
||||
Ndk::BaseSystem& system = world.AddSystem<UpdateSystem>();
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
|
||||
WHEN("We add our UpdateComponent")
|
||||
{
|
||||
UpdatableComponent& updatableComponent = entity->AddComponent<UpdatableComponent>();
|
||||
REQUIRE(!updatableComponent.IsUpdated());
|
||||
|
||||
THEN("Update the world should update the entity's component")
|
||||
{
|
||||
world.Update(1.f);
|
||||
UpdatableComponent& updatableComponentGet = entity->GetComponent<UpdatableComponent>();
|
||||
REQUIRE(updatableComponentGet.IsUpdated());
|
||||
}
|
||||
|
||||
THEN("Update the world should not update the entity's component if it's disabled")
|
||||
{
|
||||
entity->Enable(false);
|
||||
world.Update(1.f);
|
||||
UpdatableComponent& updatableComponentGet = entity->GetComponent<UpdatableComponent>();
|
||||
REQUIRE(!updatableComponentGet.IsUpdated());
|
||||
}
|
||||
|
||||
THEN("We can remove its component")
|
||||
{
|
||||
entity->RemoveComponent(Ndk::GetComponentIndex<UpdatableComponent>());
|
||||
world.Update(1.f);
|
||||
REQUIRE(!entity->HasComponent<UpdatableComponent>());
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("We kill our entity")
|
||||
{
|
||||
entity->Kill();
|
||||
world.Update(1.f);
|
||||
|
||||
THEN("It's no more valid")
|
||||
{
|
||||
REQUIRE(!world.IsEntityValid(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
41
tests/SDK/NDK/EntityList.cpp
Normal file
41
tests/SDK/NDK/EntityList.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <NDK/EntityList.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("EntityList", "[NDK][ENTITYLIST]")
|
||||
{
|
||||
GIVEN("A world & a set of entities")
|
||||
{
|
||||
Ndk::World world;
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
Ndk::EntityList entityList;
|
||||
entityList.Insert(entity);
|
||||
|
||||
WHEN("We ask if entity is in there")
|
||||
{
|
||||
THEN("These results are expected")
|
||||
{
|
||||
REQUIRE(entityList.Has(entity->GetId()));
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
REQUIRE(!entityList.Has(entity->GetId()));
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("We remove then insert")
|
||||
{
|
||||
entityList.Remove(*entityList.begin());
|
||||
|
||||
THEN("Set should be empty")
|
||||
{
|
||||
REQUIRE(entityList.empty());
|
||||
}
|
||||
|
||||
entityList.Insert(entity);
|
||||
|
||||
THEN("With one element")
|
||||
{
|
||||
REQUIRE(!entityList.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
29
tests/SDK/NDK/EntityOwner.cpp
Normal file
29
tests/SDK/NDK/EntityOwner.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <NDK/EntityOwner.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("EntityOwner", "[NDK][ENTITYOWNER]")
|
||||
{
|
||||
GIVEN("A world & an entity")
|
||||
{
|
||||
Ndk::World world;
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
|
||||
WHEN("We set the ownership of the entity to our owner")
|
||||
{
|
||||
Ndk::EntityOwner entityOwner(entity);
|
||||
|
||||
THEN("Entity is still valid")
|
||||
{
|
||||
REQUIRE(entity.IsValid());
|
||||
}
|
||||
|
||||
THEN("Resetting or getting out of scope is no more valid")
|
||||
{
|
||||
entityOwner.Reset();
|
||||
world.Update(1.f);
|
||||
REQUIRE(!entity.IsValid());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
48
tests/SDK/NDK/StateMachine.cpp
Normal file
48
tests/SDK/NDK/StateMachine.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <NDK/StateMachine.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
class TestState : public Ndk::State
|
||||
{
|
||||
public:
|
||||
void Enter(Ndk::StateMachine& fsm) override
|
||||
{
|
||||
m_isUpdated = false;
|
||||
}
|
||||
|
||||
bool IsUpdated() const
|
||||
{
|
||||
return m_isUpdated;
|
||||
}
|
||||
|
||||
void Leave(Ndk::StateMachine& fsm) override
|
||||
{
|
||||
}
|
||||
|
||||
bool Update(Ndk::StateMachine& fsm, float elapsedTime) override
|
||||
{
|
||||
m_isUpdated = true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_isUpdated;
|
||||
};
|
||||
|
||||
SCENARIO("State & StateMachine", "[NDK][STATE]")
|
||||
{
|
||||
GIVEN("A statemachine with our TestState")
|
||||
{
|
||||
std::shared_ptr<TestState> testState = std::make_shared<TestState>();
|
||||
Ndk::StateMachine stateMachine(testState);
|
||||
REQUIRE(!testState->IsUpdated());
|
||||
|
||||
WHEN("We update our machine")
|
||||
{
|
||||
stateMachine.Update(1.f);
|
||||
|
||||
THEN("Our state has been updated")
|
||||
{
|
||||
REQUIRE(testState->IsUpdated());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
42
tests/SDK/NDK/System.cpp
Normal file
42
tests/SDK/NDK/System.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <NDK/System.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
class TestSystem : public Ndk::System<TestSystem>
|
||||
{
|
||||
public:
|
||||
TestSystem()
|
||||
{
|
||||
}
|
||||
|
||||
~TestSystem() = default;
|
||||
|
||||
static Ndk::SystemIndex systemIndex;
|
||||
|
||||
private:
|
||||
void OnUpdate(float elapsedTime) override
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
Ndk::SystemIndex TestSystem::systemIndex;
|
||||
}
|
||||
|
||||
SCENARIO("System", "[NDK][SYSTEM]")
|
||||
{
|
||||
GIVEN("Our TestSystem")
|
||||
{
|
||||
TestSystem testSystem;
|
||||
|
||||
WHEN("We clone it")
|
||||
{
|
||||
std::unique_ptr<Ndk::BaseSystem> clone = testSystem.Clone();
|
||||
|
||||
THEN("We should get a copy")
|
||||
{
|
||||
REQUIRE(dynamic_cast<TestSystem*>(clone.get()) != nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
43
tests/SDK/NDK/Systems/ListenerSystem.cpp
Normal file
43
tests/SDK/NDK/Systems/ListenerSystem.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <NDK/Systems/ListenerSystem.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/ListenerComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/VelocityComponent.hpp>
|
||||
#include <Nazara/Audio/Audio.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("ListenerSystem", "[NDK][LISTENERSYSTEM]")
|
||||
{
|
||||
GIVEN("A world and an entity with listener & node components")
|
||||
{
|
||||
Ndk::World world;
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
Ndk::ListenerComponent& listenerComponent = entity->AddComponent<Ndk::ListenerComponent>();
|
||||
Ndk::NodeComponent& nodeComponent = entity->AddComponent<Ndk::NodeComponent>();
|
||||
|
||||
WHEN("We move our entity")
|
||||
{
|
||||
Nz::Vector3f position = Nz::Vector3f::Unit() * 3.f;
|
||||
nodeComponent.SetPosition(position);
|
||||
Nz::Quaternionf rotation = Nz::Quaternionf::RotationBetween(Nz::Vector3f::Forward(), Nz::Vector3f::Up());
|
||||
nodeComponent.SetRotation(rotation);
|
||||
world.Update(1.f);
|
||||
|
||||
THEN("Our listener should have moved")
|
||||
{
|
||||
REQUIRE(Nz::Audio::GetListenerPosition() == position);
|
||||
REQUIRE(Nz::Audio::GetListenerRotation() == rotation);
|
||||
}
|
||||
|
||||
THEN("With a component of velocity")
|
||||
{
|
||||
Ndk::VelocityComponent& velocityComponent = entity->AddComponent<Ndk::VelocityComponent>();
|
||||
Nz::Vector3f velocity = Nz::Vector3f::Unit() * 2.f;
|
||||
velocityComponent.linearVelocity = velocity;
|
||||
|
||||
world.Update(1.f);
|
||||
REQUIRE(Nz::Audio::GetListenerVelocity() == velocity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
tests/SDK/NDK/Systems/PhysicsSystem.cpp
Normal file
34
tests/SDK/NDK/Systems/PhysicsSystem.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <NDK/Systems/PhysicsSystem.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/CollisionComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/PhysicsComponent.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("PhysicsSystem", "[NDK][PHYSICSSYSTEM]")
|
||||
{
|
||||
GIVEN("A world and a static entity & a dynamic entity")
|
||||
{
|
||||
Ndk::World world;
|
||||
const Ndk::EntityHandle& staticEntity = world.CreateEntity();
|
||||
Ndk::CollisionComponent& collisionComponentStatic = staticEntity->AddComponent<Ndk::CollisionComponent>();
|
||||
Ndk::NodeComponent& nodeComponentStatic = staticEntity->AddComponent<Ndk::NodeComponent>();
|
||||
|
||||
const Ndk::EntityHandle& dynamicEntity = world.CreateEntity();
|
||||
Ndk::NodeComponent& nodeComponentDynamic = dynamicEntity->AddComponent<Ndk::NodeComponent>();
|
||||
Ndk::PhysicsComponent& physicsComponentDynamic = dynamicEntity->AddComponent<Ndk::PhysicsComponent>();
|
||||
|
||||
WHEN("We make collide these two entities")
|
||||
{
|
||||
nodeComponentDynamic.SetPosition(-Nz::Vector3f::UnitZ());
|
||||
physicsComponentDynamic.AddForce(Nz::Vector3f::UnitZ());
|
||||
|
||||
THEN("The dynamic entity should have hit the static one")
|
||||
{
|
||||
world.Update(1.f); // On origin
|
||||
world.Update(1.f); // On origin due to collision
|
||||
REQUIRE(nodeComponentStatic.GetPosition().SquaredDistance(nodeComponentDynamic.GetPosition()) < 0.2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
44
tests/SDK/NDK/Systems/RenderSystem.cpp
Normal file
44
tests/SDK/NDK/Systems/RenderSystem.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <NDK/Systems/RenderSystem.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/CameraComponent.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/LightComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/ParticleGroupComponent.hpp>
|
||||
#include <Nazara/Graphics/Sprite.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("RenderSystem", "[NDK][RenderSystem]")
|
||||
{
|
||||
GIVEN("A world with a camera, a drawable, a light and some particles")
|
||||
{
|
||||
Ndk::World world;
|
||||
const Ndk::EntityHandle& cameraEntity = world.CreateEntity();
|
||||
Ndk::CameraComponent& cameraComponentCamera = cameraEntity->AddComponent<Ndk::CameraComponent>();
|
||||
Ndk::NodeComponent& nodeComponentCamera = cameraEntity->AddComponent<Ndk::NodeComponent>();
|
||||
|
||||
const Ndk::EntityHandle& drawableEntity = world.CreateEntity();
|
||||
Ndk::GraphicsComponent& graphicsComponentDrawable = drawableEntity->AddComponent<Ndk::GraphicsComponent>();
|
||||
Nz::SpriteRef sprite = Nz::Sprite::New();
|
||||
graphicsComponentDrawable.Attach(sprite);
|
||||
Ndk::NodeComponent& nodeComponentDrawable = drawableEntity->AddComponent<Ndk::NodeComponent>();
|
||||
|
||||
const Ndk::EntityHandle& lightEntity = world.CreateEntity();
|
||||
Ndk::LightComponent& lightComponentLight = lightEntity->AddComponent<Ndk::LightComponent>();
|
||||
Ndk::NodeComponent& nodeComponentLight = lightEntity->AddComponent<Ndk::NodeComponent>();
|
||||
|
||||
const Ndk::EntityHandle& particlesEntity = world.CreateEntity();
|
||||
Ndk::ParticleGroupComponent& particleGroupComponentParticles = particlesEntity->AddComponent<Ndk::ParticleGroupComponent>(1, Nz::ParticleLayout_Sprite);
|
||||
|
||||
WHEN("We change the render technique to ForwardRenderTechnique")
|
||||
{
|
||||
Ndk::RenderSystem& renderSystem = world.GetSystem<Ndk::RenderSystem>();
|
||||
renderSystem.ChangeRenderTechnique<Nz::ForwardRenderTechnique>();
|
||||
|
||||
THEN("The render system should be ForwardRenderTechnique")
|
||||
{
|
||||
REQUIRE(renderSystem.GetRenderTechnique().GetType() == Nz::RenderTechniqueType_BasicForward);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
28
tests/SDK/NDK/Systems/VelocitySystem.cpp
Normal file
28
tests/SDK/NDK/Systems/VelocitySystem.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <NDK/Systems/VelocitySystem.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/VelocityComponent.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("VelocitySystem", "[NDK][VELOCITYSYSTEM]")
|
||||
{
|
||||
GIVEN("A world and an entity with velocity & node components")
|
||||
{
|
||||
Ndk::World world;
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
Ndk::VelocityComponent& velocityComponent = entity->AddComponent<Ndk::VelocityComponent>();
|
||||
Ndk::NodeComponent& nodeComponent = entity->AddComponent<Ndk::NodeComponent>();
|
||||
|
||||
WHEN("We give a speed to our entity")
|
||||
{
|
||||
Nz::Vector3f velocity = Nz::Vector3f::Unit() * 2.f;
|
||||
velocityComponent.linearVelocity = velocity;
|
||||
world.Update(1.f);
|
||||
|
||||
THEN("Our entity should have moved")
|
||||
{
|
||||
REQUIRE(nodeComponent.GetPosition().SquaredDistance(velocity) < 0.2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
103
tests/SDK/NDK/World.cpp
Normal file
103
tests/SDK/NDK/World.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Component.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
class UpdatableComponent : public Ndk::Component<UpdatableComponent>
|
||||
{
|
||||
public:
|
||||
bool IsUpdated()
|
||||
{
|
||||
return m_updated;
|
||||
}
|
||||
|
||||
void SetUpdated()
|
||||
{
|
||||
m_updated = true;
|
||||
}
|
||||
|
||||
static Ndk::ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
bool m_updated = false;
|
||||
};
|
||||
|
||||
Ndk::ComponentIndex UpdatableComponent::componentIndex;
|
||||
|
||||
class UpdateSystem : public Ndk::System<UpdateSystem>
|
||||
{
|
||||
public:
|
||||
UpdateSystem()
|
||||
{
|
||||
Requires<UpdatableComponent>();
|
||||
}
|
||||
|
||||
~UpdateSystem() = default;
|
||||
|
||||
static Ndk::SystemIndex systemIndex;
|
||||
|
||||
private:
|
||||
void OnUpdate(float elapsedTime) override
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
||||
{
|
||||
UpdatableComponent& updatable = entity->GetComponent<UpdatableComponent>();
|
||||
updatable.SetUpdated();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ndk::SystemIndex UpdateSystem::systemIndex;
|
||||
}
|
||||
|
||||
SCENARIO("World", "[NDK][WORLD]")
|
||||
{
|
||||
GIVEN("A brave new world and the update system")
|
||||
{
|
||||
Ndk::World world;
|
||||
Ndk::BaseSystem& system = world.AddSystem<UpdateSystem>();
|
||||
|
||||
WHEN("We had a new entity with an updatable component and a system")
|
||||
{
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
UpdatableComponent& component = entity->AddComponent<UpdatableComponent>();
|
||||
|
||||
THEN("We can get our entity and our system")
|
||||
{
|
||||
const Ndk::EntityHandle& fetchedEntity = world.GetEntity(entity->GetId());
|
||||
REQUIRE(fetchedEntity->GetWorld() == &world);
|
||||
}
|
||||
|
||||
THEN("We can clone it")
|
||||
{
|
||||
const Ndk::EntityHandle& clone = world.CloneEntity(entity->GetId());
|
||||
REQUIRE(world.IsEntityValid(clone));
|
||||
}
|
||||
}
|
||||
|
||||
AND_WHEN("We update our world with our entity")
|
||||
{
|
||||
REQUIRE(&world.GetSystem(UpdateSystem::systemIndex) == &world.GetSystem<UpdateSystem>());
|
||||
const Ndk::EntityHandle& entity = world.CreateEntity();
|
||||
UpdatableComponent& component = entity->AddComponent<UpdatableComponent>();
|
||||
|
||||
THEN("Our entity component must be updated")
|
||||
{
|
||||
world.Update(1.f);
|
||||
|
||||
REQUIRE(component.IsUpdated());
|
||||
}
|
||||
|
||||
THEN("We kill our entity")
|
||||
{
|
||||
REQUIRE(entity->IsValid());
|
||||
|
||||
world.KillEntity(entity);
|
||||
world.Update(1.f);
|
||||
|
||||
REQUIRE(!world.IsEntityValid(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user