mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-15 11:53:51 +07:00
198 lines
6.1 KiB
C++
198 lines
6.1 KiB
C++
/*******************************************************************************
|
|
* This file is part of the "https://github.com/blackmatov/ecs.hpp"
|
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
|
* Copyright (C) 2018 Matvey Cherevko
|
|
******************************************************************************/
|
|
|
|
#define CATCH_CONFIG_FAST_COMPILE
|
|
#include "catch.hpp"
|
|
|
|
#include "ecs.hpp"
|
|
namespace ecs = ecs_hpp;
|
|
|
|
namespace
|
|
{
|
|
struct position_c {
|
|
int x{0};
|
|
int y{0};
|
|
|
|
position_c() = default;
|
|
position_c(int nx, int ny) : x(nx), y(ny) {}
|
|
};
|
|
|
|
struct velocity_c {
|
|
int dx{0};
|
|
int dy{0};
|
|
|
|
velocity_c() = default;
|
|
velocity_c(int ndx, int ndy) : dx(ndx), dy(ndy) {}
|
|
};
|
|
}
|
|
|
|
TEST_CASE("detail") {
|
|
SECTION("get_type_id") {
|
|
REQUIRE(ecs::detail::type_family<position_c>::id() == 1u);
|
|
REQUIRE(ecs::detail::type_family<position_c>::id() == 1u);
|
|
|
|
REQUIRE(ecs::detail::type_family<velocity_c>::id() == 2u);
|
|
REQUIRE(ecs::detail::type_family<velocity_c>::id() == 2u);
|
|
|
|
REQUIRE(ecs::detail::type_family<position_c>::id() == 1u);
|
|
REQUIRE(ecs::detail::type_family<velocity_c>::id() == 2u);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("world") {
|
|
SECTION("entities") {
|
|
{
|
|
ecs::world w;
|
|
|
|
ecs::entity e1{w};
|
|
ecs::entity e2{w};
|
|
|
|
REQUIRE(e1 == e2);
|
|
REQUIRE_FALSE(w.is_entity_alive(e1));
|
|
REQUIRE_FALSE(w.is_entity_alive(e2));
|
|
|
|
REQUIRE_FALSE(w.destroy_entity(e1));
|
|
REQUIRE_FALSE(w.destroy_entity(e2));
|
|
}
|
|
{
|
|
ecs::world w;
|
|
|
|
auto e1 = w.create_entity();
|
|
auto e2 = w.create_entity();
|
|
|
|
REQUIRE(e1 != e2);
|
|
REQUIRE(w.is_entity_alive(e1));
|
|
REQUIRE(w.is_entity_alive(e2));
|
|
|
|
REQUIRE(w.destroy_entity(e1));
|
|
REQUIRE_FALSE(w.is_entity_alive(e1));
|
|
REQUIRE(w.is_entity_alive(e2));
|
|
|
|
REQUIRE(w.destroy_entity(e2));
|
|
REQUIRE_FALSE(w.is_entity_alive(e1));
|
|
REQUIRE_FALSE(w.is_entity_alive(e2));
|
|
|
|
REQUIRE_FALSE(w.destroy_entity(e1));
|
|
REQUIRE_FALSE(w.destroy_entity(e2));
|
|
}
|
|
}
|
|
SECTION("component_assigning") {
|
|
{
|
|
ecs::world w;
|
|
ecs::entity e1 = w.create_entity();
|
|
|
|
{
|
|
REQUIRE_FALSE(w.exists_component<position_c>(e1));
|
|
REQUIRE_FALSE(w.exists_component<velocity_c>(e1));
|
|
|
|
REQUIRE(w.assign_component<position_c>(e1));
|
|
|
|
REQUIRE(w.exists_component<position_c>(e1));
|
|
REQUIRE_FALSE(w.exists_component<velocity_c>(e1));
|
|
|
|
REQUIRE(w.assign_component<velocity_c>(e1));
|
|
|
|
REQUIRE(w.exists_component<position_c>(e1));
|
|
REQUIRE(w.exists_component<velocity_c>(e1));
|
|
|
|
REQUIRE(w.remove_all_components(e1) == 2u);
|
|
|
|
REQUIRE_FALSE(w.exists_component<position_c>(e1));
|
|
REQUIRE_FALSE(w.exists_component<velocity_c>(e1));
|
|
}
|
|
|
|
{
|
|
REQUIRE_FALSE(e1.exists_component<position_c>());
|
|
REQUIRE_FALSE(e1.exists_component<velocity_c>());
|
|
|
|
REQUIRE(e1.assign_component<position_c>());
|
|
|
|
REQUIRE(e1.exists_component<position_c>());
|
|
REQUIRE_FALSE(e1.exists_component<velocity_c>());
|
|
|
|
REQUIRE(e1.assign_component<velocity_c>());
|
|
|
|
REQUIRE(e1.exists_component<position_c>());
|
|
REQUIRE(e1.exists_component<velocity_c>());
|
|
}
|
|
}
|
|
{
|
|
ecs::world w;
|
|
|
|
auto e1 = w.create_entity();
|
|
auto e2 = w.create_entity();
|
|
|
|
REQUIRE(w.assign_component<position_c>(e1));
|
|
REQUIRE(w.assign_component<velocity_c>(e1));
|
|
|
|
REQUIRE(w.assign_component<position_c>(e2));
|
|
REQUIRE(w.assign_component<velocity_c>(e2));
|
|
|
|
REQUIRE(w.destroy_entity(e1));
|
|
|
|
REQUIRE_FALSE(w.exists_component<position_c>(e1));
|
|
REQUIRE_FALSE(w.exists_component<velocity_c>(e1));
|
|
|
|
REQUIRE(w.exists_component<position_c>(e2));
|
|
REQUIRE(w.exists_component<velocity_c>(e2));
|
|
}
|
|
{
|
|
ecs::world w;
|
|
auto e1 = w.create_entity();
|
|
REQUIRE(e1.destroy());
|
|
REQUIRE_FALSE(e1.assign_component<position_c>());
|
|
REQUIRE_FALSE(w.exists_component<position_c>(e1));
|
|
}
|
|
}
|
|
SECTION("component_accessing") {
|
|
{
|
|
ecs::world w;
|
|
|
|
auto e1 = w.create_entity();
|
|
auto e2 = w.create_entity();
|
|
|
|
REQUIRE_FALSE(e1.find_component<position_c>());
|
|
REQUIRE_FALSE(e2.find_component<velocity_c>());
|
|
|
|
e1.assign_component<position_c>(1, 2);
|
|
e2.assign_component<velocity_c>(3, 4);
|
|
|
|
REQUIRE(e1.get_component<position_c>().x == 1);
|
|
REQUIRE(e1.get_component<position_c>().y == 2);
|
|
|
|
REQUIRE(e2.get_component<velocity_c>().dx == 3);
|
|
REQUIRE(e2.get_component<velocity_c>().dy == 4);
|
|
|
|
REQUIRE_THROWS_AS(e1.get_component<velocity_c>(), ecs::basic_exception);
|
|
REQUIRE_THROWS_AS(e2.get_component<position_c>(), ecs::basic_exception);
|
|
}
|
|
{
|
|
ecs::world w;
|
|
|
|
const auto e1 = w.create_entity();
|
|
const auto e2 = w.create_entity();
|
|
|
|
REQUIRE_FALSE(e1.find_component<position_c>());
|
|
REQUIRE_FALSE(e2.find_component<velocity_c>());
|
|
|
|
w.assign_component<position_c>(e1, 1, 2);
|
|
w.assign_component<velocity_c>(e2, 3, 4);
|
|
|
|
{
|
|
const ecs::world& ww = w;
|
|
REQUIRE(ww.get_component<position_c>(e1).x == 1);
|
|
REQUIRE(ww.get_component<position_c>(e1).y == 2);
|
|
|
|
REQUIRE(ww.get_component<velocity_c>(e2).dx == 3);
|
|
REQUIRE(ww.get_component<velocity_c>(e2).dy == 4);
|
|
|
|
REQUIRE_THROWS_AS(ww.get_component<velocity_c>(e1), ecs::basic_exception);
|
|
REQUIRE_THROWS_AS(ww.get_component<position_c>(e2), ecs::basic_exception);
|
|
}
|
|
}
|
|
}
|
|
}
|