mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-02 10:15:32 +00:00
Adding tests and fixing has components
This commit is contained in:
@@ -11,7 +11,7 @@ namespace WAL::Components
|
||||
_position()
|
||||
{}
|
||||
|
||||
PositionComponent::PositionComponent(float x, float y, float z, Entity &entity)
|
||||
PositionComponent::PositionComponent(Entity &entity, float x, float y, float z)
|
||||
: Component(entity),
|
||||
_position(x, y, z)
|
||||
{}
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace WAL::Components
|
||||
//! @brief Create a new PositionComponent linked to a specific entity
|
||||
explicit PositionComponent(Entity &entity);
|
||||
//! @brief Create a new PositionComponent at a certain position
|
||||
PositionComponent(float x, float y, float z, Entity &entity);
|
||||
PositionComponent(Entity &entity, float x, float y, float z);
|
||||
//! @brief A position component is copy constructable
|
||||
PositionComponent(const PositionComponent &) = default;
|
||||
//! @brief A default destructor
|
||||
|
||||
@@ -41,22 +41,22 @@ namespace WAL
|
||||
//! @brief Get a component of a specific type
|
||||
//! @throw NotFoundError if the component could not be found
|
||||
template<typename T>
|
||||
T getComponent()
|
||||
T &getComponent()
|
||||
{
|
||||
const std::type_info &type = typeid(T);
|
||||
auto &existing = std::find(this->_components.begin(), this->_components.end(), [&type] (auto &cmp) {
|
||||
auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (auto &cmp) {
|
||||
return typeid(*cmp) == type;
|
||||
});
|
||||
if (existing == this->_components.end())
|
||||
throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\".");
|
||||
return *existing;
|
||||
return *reinterpret_cast<T *>(existing->get());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool hasComponent() const
|
||||
{
|
||||
const std::type_info &type = typeid(T);
|
||||
auto existing = std::find(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) {
|
||||
auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) {
|
||||
return typeid(*cmp) == type;
|
||||
});
|
||||
return existing != this->_components.end();
|
||||
@@ -70,7 +70,7 @@ namespace WAL
|
||||
{
|
||||
if (this->hasComponent<T>())
|
||||
throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists.");
|
||||
this->_components.push_back(std::make_unique<T>(params...));
|
||||
this->_components.push_back(std::make_unique<T>(*this, params...));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,16 @@ namespace WAL
|
||||
//! @brief A default destructor
|
||||
~Vector3() = default;
|
||||
|
||||
bool operator==(const Vector3<T> &other) const
|
||||
{
|
||||
return this->x == other.x && this->y == other.y && this->z == other.z;
|
||||
}
|
||||
|
||||
bool operator!=(const Vector3<T> &other) const
|
||||
{
|
||||
return !this->operator==(other);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> &operator+=(const Vector3<T2> &vec)
|
||||
{
|
||||
|
||||
@@ -9,8 +9,23 @@
|
||||
using namespace WAL;
|
||||
using namespace WAL::Components;
|
||||
|
||||
TEST_CASE("Get component", "[Entity/getComponent]")
|
||||
TEST_CASE("Component", "[Entity]")
|
||||
{
|
||||
Entity entity("Bob");
|
||||
entity.addComponent<PositionComponent>();
|
||||
entity.addComponent<PositionComponent>(2, 3, 4);
|
||||
|
||||
SECTION("Check value") {
|
||||
auto &pos = entity.getComponent<PositionComponent>();
|
||||
REQUIRE(entity.hasComponent<PositionComponent>());
|
||||
REQUIRE(pos.getPosition() == Vector3f(2, 3, 4));
|
||||
}
|
||||
SECTION("Prevent duplicates") {
|
||||
REQUIRE_THROWS_AS(entity.addComponent<PositionComponent>(), DuplicateError);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("ComponentNotFound", "[Entity]")
|
||||
{
|
||||
Entity entity("Bob");
|
||||
REQUIRE_THROWS_AS(entity.getComponent<PositionComponent>(), NotFoundError);
|
||||
}
|
||||
Reference in New Issue
Block a user