switch to ECS architecture
This commit is contained in:
@@ -2,25 +2,23 @@
|
||||
|
||||
#include <QVector2D>
|
||||
|
||||
#include "EntityId.h"
|
||||
#include "Scrap.h"
|
||||
#include "EntityAdmin.h"
|
||||
#include "ScrapSystem.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Spawn
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("ScrapSystem: spawn returns a findable scrap with correct fields", "[scrap]")
|
||||
TEST_CASE("ScrapSystem: spawn returns a valid entity with correct scrap data", "[scrap]")
|
||||
{
|
||||
EntityId nextId = 1;
|
||||
ScrapSystem ss([&nextId]() { return nextId++; });
|
||||
EntityAdmin admin;
|
||||
ScrapSystem ss(admin);
|
||||
|
||||
const EntityId id = ss.spawn(QVector2D(3.0f, 4.0f), 5, 100);
|
||||
const Scrap* s = ss.findScrap(id);
|
||||
const entt::entity e = ss.spawn(QVector2D(3.0f, 4.0f), 5, 100);
|
||||
|
||||
REQUIRE(s != nullptr);
|
||||
REQUIRE(s->amount == 5);
|
||||
REQUIRE(s->despawnAt == 100);
|
||||
REQUIRE(admin.isValid(e));
|
||||
REQUIRE(admin.get<ScrapData>(e).amount == 5);
|
||||
REQUIRE(admin.get<DespawnAt>(e).tick == 100);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -29,24 +27,24 @@ TEST_CASE("ScrapSystem: spawn returns a findable scrap with correct fields", "[s
|
||||
|
||||
TEST_CASE("ScrapSystem: scrap still present one tick before despawnAt", "[scrap]")
|
||||
{
|
||||
EntityId nextId = 1;
|
||||
ScrapSystem ss([&nextId]() { return nextId++; });
|
||||
EntityAdmin admin;
|
||||
ScrapSystem ss(admin);
|
||||
|
||||
const EntityId id = ss.spawn(QVector2D(0.0f, 0.0f), 1, 50);
|
||||
const entt::entity e = ss.spawn(QVector2D(0.0f, 0.0f), 1, 50);
|
||||
|
||||
ss.tickDespawn(49);
|
||||
REQUIRE(ss.findScrap(id) != nullptr);
|
||||
REQUIRE(admin.isValid(e));
|
||||
}
|
||||
|
||||
TEST_CASE("ScrapSystem: scrap removed at despawnAt tick", "[scrap]")
|
||||
{
|
||||
EntityId nextId = 1;
|
||||
ScrapSystem ss([&nextId]() { return nextId++; });
|
||||
EntityAdmin admin;
|
||||
ScrapSystem ss(admin);
|
||||
|
||||
const EntityId id = ss.spawn(QVector2D(0.0f, 0.0f), 1, 50);
|
||||
const entt::entity e = ss.spawn(QVector2D(0.0f, 0.0f), 1, 50);
|
||||
|
||||
ss.tickDespawn(50);
|
||||
REQUIRE(ss.findScrap(id) == nullptr);
|
||||
REQUIRE_FALSE(admin.isValid(e));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -55,29 +53,56 @@ TEST_CASE("ScrapSystem: scrap removed at despawnAt tick", "[scrap]")
|
||||
|
||||
TEST_CASE("ScrapSystem: tickDespawn removes only expired scraps", "[scrap]")
|
||||
{
|
||||
EntityId nextId = 1;
|
||||
ScrapSystem ss([&nextId]() { return nextId++; });
|
||||
EntityAdmin admin;
|
||||
ScrapSystem ss(admin);
|
||||
|
||||
const EntityId earlyId = ss.spawn(QVector2D(0.0f, 0.0f), 1, 30);
|
||||
const EntityId lateId = ss.spawn(QVector2D(1.0f, 0.0f), 2, 60);
|
||||
const entt::entity earlyE = ss.spawn(QVector2D(0.0f, 0.0f), 1, 30);
|
||||
const entt::entity lateE = ss.spawn(QVector2D(1.0f, 0.0f), 2, 60);
|
||||
|
||||
ss.tickDespawn(30);
|
||||
|
||||
REQUIRE(ss.findScrap(earlyId) == nullptr);
|
||||
REQUIRE(ss.findScrap(lateId) != nullptr);
|
||||
REQUIRE_FALSE(admin.isValid(earlyE));
|
||||
REQUIRE(admin.isValid(lateE));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Entity ids
|
||||
// Consume
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("ScrapSystem: spawned scraps receive strictly increasing entity ids", "[scrap]")
|
||||
TEST_CASE("ScrapSystem: consume returns amount and destroys entity", "[scrap]")
|
||||
{
|
||||
EntityId nextId = 1;
|
||||
ScrapSystem ss([&nextId]() { return nextId++; });
|
||||
EntityAdmin admin;
|
||||
ScrapSystem ss(admin);
|
||||
|
||||
const EntityId id1 = ss.spawn(QVector2D(0.0f, 0.0f), 1, 100);
|
||||
const EntityId id2 = ss.spawn(QVector2D(1.0f, 0.0f), 2, 200);
|
||||
const entt::entity e = ss.spawn(QVector2D(0.0f, 0.0f), 7, 100);
|
||||
|
||||
REQUIRE(id2 > id1);
|
||||
const std::optional<int> amount = ss.consume(e);
|
||||
REQUIRE(amount.has_value());
|
||||
REQUIRE(*amount == 7);
|
||||
REQUIRE_FALSE(admin.isValid(e));
|
||||
}
|
||||
|
||||
TEST_CASE("ScrapSystem: consume returns nullopt for invalid entity", "[scrap]")
|
||||
{
|
||||
EntityAdmin admin;
|
||||
ScrapSystem ss(admin);
|
||||
|
||||
const std::optional<int> amount = ss.consume(entt::null);
|
||||
REQUIRE_FALSE(amount.has_value());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// allScrapInfo
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("ScrapSystem: allScrapInfo returns all spawned scrap", "[scrap]")
|
||||
{
|
||||
EntityAdmin admin;
|
||||
ScrapSystem ss(admin);
|
||||
|
||||
ss.spawn(QVector2D(1.0f, 2.0f), 3, 100);
|
||||
ss.spawn(QVector2D(4.0f, 5.0f), 6, 200);
|
||||
|
||||
const std::vector<ScrapInfo> info = ss.allScrapInfo();
|
||||
REQUIRE(info.size() == 2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user