From e98a73f2eae0f8625cfd0dd51982bb918343d1df Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 17 May 2021 12:31:02 +0200 Subject: [PATCH] Adding a position componentn and a vector3 --- lib/wal/CMakeLists.txt | 18 +- .../Component/Basics/PositionComponent.cpp | 48 ++++++ .../Component/Basics/PositionComponent.hpp | 45 +++++ lib/wal/sources/Component/Component.cpp | 10 +- lib/wal/sources/Component/Component.hpp | 7 +- lib/wal/sources/Entity/Entity.hpp | 2 +- lib/wal/sources/Models/Vector3.hpp | 157 ++++++++++++++++++ lib/wal/tests/EntityTests.cpp | 16 ++ lib/wal/tests/MainTest.cpp | 6 + 9 files changed, 292 insertions(+), 17 deletions(-) create mode 100644 lib/wal/sources/Component/Basics/PositionComponent.cpp create mode 100644 lib/wal/sources/Component/Basics/PositionComponent.hpp create mode 100644 lib/wal/sources/Models/Vector3.hpp create mode 100644 lib/wal/tests/EntityTests.cpp create mode 100644 lib/wal/tests/MainTest.cpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index 02613603..26357827 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -19,6 +19,20 @@ add_library(wal sources/Events/EventManager.hpp sources/Exception/WalError.cpp sources/Exception/WalError.hpp - sources/Entity/Entity.cpp sources/Component/Component.cpp) + sources/Entity/Entity.cpp + sources/Component/Component.cpp + sources/Component/Basics/PositionComponent.cpp + sources/Component/Basics/PositionComponent.hpp + sources/Models/Vector2.hpp + sources/Models/Vector3.hpp +) -target_include_directories(wal PUBLIC sources) \ No newline at end of file +target_include_directories(wal PUBLIC sources) + +add_executable(wal_tests + tests/EntityTests.cpp + tests/MainTest.cpp) + +target_link_libraries(wal_tests PRIVATE wal) +find_package(Catch2 REQUIRED) +target_link_libraries(wal_tests PRIVATE Catch2::Catch2) \ No newline at end of file diff --git a/lib/wal/sources/Component/Basics/PositionComponent.cpp b/lib/wal/sources/Component/Basics/PositionComponent.cpp new file mode 100644 index 00000000..572bacaf --- /dev/null +++ b/lib/wal/sources/Component/Basics/PositionComponent.cpp @@ -0,0 +1,48 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#include "PositionComponent.hpp" + +namespace WAL::Components +{ + PositionComponent::PositionComponent(Entity &entity) + : Component(entity), + _position() + {} + + PositionComponent::PositionComponent(float x, float y, float z, Entity &entity) + : Component(entity), + _position(x, y, z) + {} + + Component *PositionComponent::clone(WAL::Entity &entity) const + { + return new PositionComponent(entity); + } + + Vector3f &PositionComponent::getPosition() + { + return this->_position; + } + + const Vector3f &PositionComponent::getPosition() const + { + return this->_position; + } + + float PositionComponent::getX() const + { + return this->_position.x; + } + + float PositionComponent::getY() const + { + return this->_position.y; + } + + float PositionComponent::getZ() const + { + return this->_position.z; + } +} \ No newline at end of file diff --git a/lib/wal/sources/Component/Basics/PositionComponent.hpp b/lib/wal/sources/Component/Basics/PositionComponent.hpp new file mode 100644 index 00000000..b1f30fbd --- /dev/null +++ b/lib/wal/sources/Component/Basics/PositionComponent.hpp @@ -0,0 +1,45 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#pragma once + +#include "Models/Vector3.hpp" +#include "Component/Component.hpp" + +namespace WAL::Components +{ + //! @brief A basic position component + class PositionComponent : public Component + { + private: + //! @brief The position of the entity as a vector3. + Vector3f _position; + public: + //! @brief Get the editable position of this entity + Vector3f &getPosition(); + //! @brief Get the const position of this entity + const Vector3f &getPosition() const; + + //! @brief Get the X position of this entity. + float getX() const; + //! @brief Get the Y position of this entity. + float getY() const; + //! @brief Get the Z position of this entity. + float getZ() const; + + //! @inherit + Component *clone(Entity &entity) const override; + + //! @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); + //! @brief A position component is copy constructable + PositionComponent(const PositionComponent &) = default; + //! @brief A default destructor + ~PositionComponent() override = default; + //! @brief A position component is not assignable + PositionComponent &operator=(const PositionComponent &) = delete; + }; +} diff --git a/lib/wal/sources/Component/Component.cpp b/lib/wal/sources/Component/Component.cpp index 5d2948c7..f891352b 100644 --- a/lib/wal/sources/Component/Component.cpp +++ b/lib/wal/sources/Component/Component.cpp @@ -6,16 +6,10 @@ namespace WAL { - Component::Component(std::string name, Entity &entity) - : _name(std::move(name)), - _entity(entity) + Component::Component(Entity &entity) + : _entity(entity) { } - std::string Component::getName() const - { - return this->_name; - } - bool Component::isDisabled() const { return this->_disabled; diff --git a/lib/wal/sources/Component/Component.hpp b/lib/wal/sources/Component/Component.hpp index 64d325a9..c05fd507 100644 --- a/lib/wal/sources/Component/Component.hpp +++ b/lib/wal/sources/Component/Component.hpp @@ -16,8 +16,6 @@ namespace WAL class Component { private: - //! @brief The name of this component - std::string _name; //! @brief Is this component disabled? bool _disabled = false; protected: @@ -27,7 +25,7 @@ namespace WAL std::vector _dependencies; //! @brief A component can't be instantiated, it should be derived. - explicit Component(std::string name, Entity &entity); + explicit Component(Entity &entity); //! @brief A component can't be instantiated, it should be derived. Component(const Component &) = default; public: @@ -40,9 +38,6 @@ namespace WAL //! @param entity The entity that owns the ne component. virtual Component *clone(Entity &entity) const = 0; - //! @brief Get the name of this component - std::string getName() const; - //! @brief Used if the component is disabled bool isDisabled() const; //! @brief Disable this component. diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 7ad68586..cc4c3422 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -70,7 +70,7 @@ namespace WAL { if (this->hasComponent()) throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists."); - this->_components.push_back(std::make_unique(params...)); + this->_components.push_back(std::make_unique(params...)); return *this; } diff --git a/lib/wal/sources/Models/Vector3.hpp b/lib/wal/sources/Models/Vector3.hpp new file mode 100644 index 00000000..fa3b23fa --- /dev/null +++ b/lib/wal/sources/Models/Vector3.hpp @@ -0,0 +1,157 @@ +// +// Created by Zoe Roux on 5/17/21. +// + + +#pragma once + +#include +#include + +namespace WAL +{ + //! @brief A Vector3 data type. (templated to allow any kind of vector3) + template + class Vector3 + { + public: + //! @brief The x value of the vector + T x; + //! @brief The y value of the vector + T y; + //! @brief The y value of the vector + T z; + + //! @brief Create a new nil vector3. + Vector3() + : x(0), y(0), z(0) + {} + + //! @brief Create a new vector3 representing a specific coordinate. + Vector3(T x, T y, T z) + : x(x), y(y), z(z) + {} + + //! @brief A default destructor + ~Vector3() = default; + + template + Vector3 &operator+=(const Vector3 &vec) + { + this->x += vec.x; + this->y += vec.y; + this->z += vec.z; + return *this; + } + + template + Vector3 operator+(const Vector3 &vec) const + { + return Vector3(this->x + vec.x, this->y + vec.y, this->z + vec.z); + } + + template + Vector3 &operator-=(const Vector3 &vec) + { + this->x -= vec.x; + this->y -= vec.y; + this->z -= vec.z; + return *this; + } + + template + Vector3 &operator*=(T2 d) + { + this->x *= d; + this->y *= d; + this->z *= d; + return *this; + } + + template + Vector3 operator*(T2 d) const + { + return Vector2(this->x * d, this->y * d, this->z * d); + } + + template + Vector3 operator*(Vector3 &b) const + { + return Vector3(this->x * b.x, this->y * b.y, this->z * b.z); + } + + template + Vector3 operator/=(Vector3 &b) + { + this->x /= b.x; + this->y /= b.y; + this->z /= b.z; + return this; + } + + template + Vector3 operator/(Vector3 &b) const + { + return Vector3(this->x / b.x, this->y / b.y, this->z / b.z); + } + + template + Vector3 operator/=(T2 b) + { + this->x /= b; + this->y /= b; + this->z /= b; + return this; + } + + template + Vector3 operator/(T2 b) const + { + return Vector3(this->x / b, this->y / b, this->z / b); + } + + template + double distance(const Vector3 &o) const + { + return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2) + std::pow(this->z - o.z, 2)); + } + + double magnitude() const + { + return (std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2), std::pow(this->z, 2))); + } + + Vector3 normalize() + { + double mag = this->magnitude(); + + this->x /= mag; + this->y /= mag; + this->z /= mag; + return *this; + } + + Vector3 normalized() const + { + T mag = this->magnitude(); + + return Vector3(this->x / mag, this->y / mag, this->z / mag); + } + + Vector3 projection(const Vector3 &point) const + { + return (point * this) / std::pow(this->magnitude(), 2) * this; + } + }; + + typedef Vector3 Vector3f; + typedef Vector3 Vector3u; + typedef Vector3 Vector3i; +} + +template +std::ostream &operator<<(std::ostream &s, const WAL::Vector3 &v) +{ + s << v.x << " " << v.y << " " << v.z; + return s; +} \ No newline at end of file diff --git a/lib/wal/tests/EntityTests.cpp b/lib/wal/tests/EntityTests.cpp new file mode 100644 index 00000000..58363495 --- /dev/null +++ b/lib/wal/tests/EntityTests.cpp @@ -0,0 +1,16 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#include +#include "Entity/Entity.hpp" +#include "Component/Basics/PositionComponent.hpp" + +using namespace WAL; +using namespace WAL::Components; + +TEST_CASE("Get component", "[Entity/getComponent]") +{ + Entity entity("Bob"); + entity.addComponent(); +} \ No newline at end of file diff --git a/lib/wal/tests/MainTest.cpp b/lib/wal/tests/MainTest.cpp new file mode 100644 index 00000000..6f2c3690 --- /dev/null +++ b/lib/wal/tests/MainTest.cpp @@ -0,0 +1,6 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#define CATCH_CONFIG_MAIN +#include \ No newline at end of file