135 lines
3.4 KiB
C++
135 lines
3.4 KiB
C++
#include <doctest/doctest.h>
|
|
#include "Components/Configs/WorldConfig.hpp"
|
|
#include "Core/WorldInstance.h"
|
|
#include "Components/Resource.hpp"
|
|
#include "Components/Inventory.hpp"
|
|
|
|
TEST_SUITE("Resource") {
|
|
TEST_CASE("basic resource gathering produces item after one tick") {
|
|
WorldConfig config{};
|
|
uint16_t stoneID = config.RegisterItem("Stone");
|
|
|
|
WorldInstance world{ config };
|
|
|
|
// Create a resource entity with gatherTicks = 1
|
|
auto entity = world.GetEcsWorld().entity();
|
|
Resource_Ore_Helper(entity, stoneID, 1);
|
|
|
|
world.ProcessFrame();
|
|
|
|
auto& inv = world.GetEcsWorld().ensure<WorldInventory>();
|
|
CHECK(inv.GetItemsAmount(stoneID) >= 1);
|
|
}
|
|
|
|
TEST_CASE("resource with 20 tick gather time produces item after 20 ticks") {
|
|
WorldConfig config{};
|
|
uint16_t stoneID = config.RegisterItem("Stone");
|
|
|
|
WorldInstance world{ config };
|
|
|
|
auto entity = world.GetEcsWorld().entity();
|
|
Resource_Ore_Helper(entity, stoneID, 20);
|
|
|
|
for (int i = 0; i < 19; ++i)
|
|
world.ProcessFrame();
|
|
|
|
auto& inv = world.GetEcsWorld().ensure<WorldInventory>();
|
|
CHECK(inv.GetItemsAmount(stoneID) == 0);
|
|
|
|
world.ProcessFrame();
|
|
|
|
CHECK(inv.GetItemsAmount(stoneID) >= 1);
|
|
}
|
|
}
|
|
|
|
TEST_SUITE("Resource - Health & Renewing") {
|
|
TEST_CASE("tree resource loses health each gather cycle") {
|
|
WorldConfig config{};
|
|
uint16_t woodID = config.RegisterItem("Wood");
|
|
|
|
WorldInstance world{ config };
|
|
|
|
auto entity = world.GetEcsWorld().entity();
|
|
Resource_Tree_Helper(entity, woodID, 1, 3, 5);
|
|
|
|
world.ProcessFrame();
|
|
|
|
auto& inv = world.GetEcsWorld().ensure<WorldInventory>();
|
|
CHECK(inv.GetItemsAmount(woodID) == 1);
|
|
|
|
auto health = entity.get<ResourceHealth>();
|
|
CHECK(health.Health == 2);
|
|
}
|
|
|
|
TEST_CASE("tree enters renewing state when health reaches 0") {
|
|
WorldConfig config{};
|
|
uint16_t woodID = config.RegisterItem("Wood");
|
|
|
|
WorldInstance world{ config };
|
|
|
|
auto entity = world.GetEcsWorld().entity();
|
|
Resource_Tree_Helper(entity, woodID, 1, 2, 5);
|
|
|
|
// 2 gather cycles to deplete health
|
|
world.ProcessFrame();
|
|
world.ProcessFrame();
|
|
|
|
auto& inv = world.GetEcsWorld().ensure<WorldInventory>();
|
|
CHECK(inv.GetItemsAmount(woodID) == 2);
|
|
CHECK(entity.has<Renewing>());
|
|
|
|
// one more tick should not produce items while renewing
|
|
world.ProcessFrame();
|
|
CHECK(inv.GetItemsAmount(woodID) == 2);
|
|
}
|
|
|
|
TEST_CASE("tree restores health after renewal period") {
|
|
WorldConfig config{};
|
|
uint16_t woodID = config.RegisterItem("Wood");
|
|
|
|
WorldInstance world{ config };
|
|
|
|
auto entity = world.GetEcsWorld().entity();
|
|
Resource_Tree_Helper(entity, woodID, 1, 1, 5);
|
|
|
|
// 1 gather cycle depletes health
|
|
world.ProcessFrame();
|
|
CHECK(entity.has<Renewing>());
|
|
|
|
// 5 ticks for renewal
|
|
for (int i = 0; i < 5; ++i)
|
|
world.ProcessFrame();
|
|
|
|
CHECK_FALSE(entity.has<Renewing>());
|
|
CHECK(entity.has<FullyGrown>());
|
|
|
|
auto health = entity.get<ResourceHealth>();
|
|
CHECK(health.Health == health.MaxHealth);
|
|
}
|
|
|
|
TEST_CASE("tree resumes gathering after renewal") {
|
|
WorldConfig config{};
|
|
uint16_t woodID = config.RegisterItem("Wood");
|
|
|
|
WorldInstance world{ config };
|
|
|
|
auto entity = world.GetEcsWorld().entity();
|
|
Resource_Tree_Helper(entity, woodID, 1, 1, 5);
|
|
|
|
// deplete
|
|
world.ProcessFrame();
|
|
auto& inv = world.GetEcsWorld().ensure<WorldInventory>();
|
|
CHECK(inv.GetItemsAmount(woodID) == 1);
|
|
|
|
// renew (5 ticks)
|
|
for (int i = 0; i < 5; ++i)
|
|
world.ProcessFrame();
|
|
|
|
CHECK_FALSE(entity.has<Renewing>());
|
|
|
|
// should gather again
|
|
world.ProcessFrame();
|
|
CHECK(inv.GetItemsAmount(woodID) == 2);
|
|
}
|
|
}
|