From 76ccd80df738ede9eff6ca4f89bc82aead0e7fc1 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 17 May 2021 17:03:15 +0200 Subject: [PATCH] Adding a movable system/component --- lib/wal/CMakeLists.txt | 6 +-- .../Component/Movable/MovableComponent.cpp | 22 +++++++++++ .../Component/Movable/MovableComponent.hpp | 39 +++++++++++++++++++ .../PositionComponent.cpp | 26 ++++--------- .../PositionComponent.hpp | 9 +---- lib/wal/sources/Models/Vector3.hpp | 2 +- .../sources/System/Movable/MovableSystem.cpp | 26 +++++++++++++ .../sources/System/Movable/MovableSystem.hpp | 30 ++++++++++++++ lib/wal/sources/System/System.cpp | 15 +++++++ lib/wal/sources/System/System.hpp | 6 +-- lib/wal/sources/Wal.cpp | 8 ++-- lib/wal/sources/Wal.hpp | 6 +-- lib/wal/tests/EntityTests.cpp | 3 +- 13 files changed, 157 insertions(+), 41 deletions(-) create mode 100644 lib/wal/sources/Component/Movable/MovableComponent.cpp create mode 100644 lib/wal/sources/Component/Movable/MovableComponent.hpp rename lib/wal/sources/Component/{Basics => Position}/PositionComponent.cpp (59%) rename lib/wal/sources/Component/{Basics => Position}/PositionComponent.hpp (84%) create mode 100644 lib/wal/sources/System/Movable/MovableSystem.cpp create mode 100644 lib/wal/sources/System/Movable/MovableSystem.hpp create mode 100644 lib/wal/sources/System/System.cpp diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index abac0828..4552fd54 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -21,10 +21,10 @@ add_library(wal sources/Exception/WalError.hpp sources/Entity/Entity.cpp sources/Component/Component.cpp - sources/Component/Basics/PositionComponent.cpp - sources/Component/Basics/PositionComponent.hpp + sources/Component/Position/PositionComponent.cpp + sources/Component/Position/PositionComponent.hpp sources/Models/Vector3.hpp -) + sources/Component/Movable/MovableComponent.cpp sources/Component/Movable/MovableComponent.hpp sources/System/Movable/MovableSystem.cpp sources/System/Movable/MovableSystem.hpp sources/System/System.cpp) target_include_directories(wal PUBLIC sources) diff --git a/lib/wal/sources/Component/Movable/MovableComponent.cpp b/lib/wal/sources/Component/Movable/MovableComponent.cpp new file mode 100644 index 00000000..b989de28 --- /dev/null +++ b/lib/wal/sources/Component/Movable/MovableComponent.cpp @@ -0,0 +1,22 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#include "MovableComponent.hpp" + +namespace WAL::Movable +{ + MovableComponent::MovableComponent(Entity &entity) + : Component(entity) + {} + + Component *MovableComponent::clone(Entity &entity) const + { + return new MovableComponent(entity); + } + + void MovableComponent::addForce(Vector3f force) + { + this->_acceleration += force; + } +} \ No newline at end of file diff --git a/lib/wal/sources/Component/Movable/MovableComponent.hpp b/lib/wal/sources/Component/Movable/MovableComponent.hpp new file mode 100644 index 00000000..0693121a --- /dev/null +++ b/lib/wal/sources/Component/Movable/MovableComponent.hpp @@ -0,0 +1,39 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#pragma once + +#include "Models/Vector3.hpp" +#include "Entity/Entity.hpp" + +namespace WAL +{ + //! @brief A component to place on entities that can move or be moved. + class MovableComponent : public Component + { + private: + //! @brief The acceleration of this entity. + Vector3f _acceleration; + //! @brief The velocity of the entity. + Vector3f _velocity; + public: + //! @brief Add an instant force to this entity. + //! @param force The force to add to this entity's acceleration. The force is added instantly and in one go. + void addForce(Vector3f force); + + //! @inherit + Component *clone(Entity &entity) const override; + + //! @brief Create a new movable component. + explicit MovableComponent(Entity &entity); + //! @brief A movable component is copy constructable. + MovableComponent(const MovableComponent &) = default; + //! @brief A default destructor + ~MovableComponent() override = default; + //! @brief A movable component is not assignable. + MovableComponent &operator=(const MovableComponent &) = delete; + + friend class MovableSystem; + }; +} \ No newline at end of file diff --git a/lib/wal/sources/Component/Basics/PositionComponent.cpp b/lib/wal/sources/Component/Position/PositionComponent.cpp similarity index 59% rename from lib/wal/sources/Component/Basics/PositionComponent.cpp rename to lib/wal/sources/Component/Position/PositionComponent.cpp index 3e5b736c..1e470254 100644 --- a/lib/wal/sources/Component/Basics/PositionComponent.cpp +++ b/lib/wal/sources/Component/Position/PositionComponent.cpp @@ -4,50 +4,40 @@ #include "PositionComponent.hpp" -namespace WAL::Components +namespace WAL { PositionComponent::PositionComponent(Entity &entity) : Component(entity), - _position() + position() {} PositionComponent::PositionComponent(Entity &entity, Vector3f pos) : Component(entity), - _position(pos) + position(pos) {} PositionComponent::PositionComponent(Entity &entity, float x, float y, float z) : Component(entity), - _position(x, y, z) + position(x, y, z) {} Component *PositionComponent::clone(WAL::Entity &entity) const { - return new PositionComponent(entity, this->_position); - } - - Vector3f &PositionComponent::getPosition() - { - return this->_position; - } - - const Vector3f &PositionComponent::getPosition() const - { - return this->_position; + return new PositionComponent(entity, this->position); } float PositionComponent::getX() const { - return this->_position.x; + return this->position.x; } float PositionComponent::getY() const { - return this->_position.y; + return this->position.y; } float PositionComponent::getZ() const { - return this->_position.z; + 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/Position/PositionComponent.hpp similarity index 84% rename from lib/wal/sources/Component/Basics/PositionComponent.hpp rename to lib/wal/sources/Component/Position/PositionComponent.hpp index 6c557f01..8e11a2e3 100644 --- a/lib/wal/sources/Component/Basics/PositionComponent.hpp +++ b/lib/wal/sources/Component/Position/PositionComponent.hpp @@ -7,19 +7,14 @@ #include "Models/Vector3.hpp" #include "Component/Component.hpp" -namespace WAL::Components +namespace WAL { //! @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; + Vector3f position; //! @brief Get the X position of this entity. float getX() const; diff --git a/lib/wal/sources/Models/Vector3.hpp b/lib/wal/sources/Models/Vector3.hpp index 3ea30504..05391c25 100644 --- a/lib/wal/sources/Models/Vector3.hpp +++ b/lib/wal/sources/Models/Vector3.hpp @@ -81,7 +81,7 @@ namespace WAL template Vector3 operator*(T2 d) const { - return Vector2(this->x * d, this->y * d, this->z * d); + return Vector3(this->x * d, this->y * d, this->z * d); } template diff --git a/lib/wal/sources/System/Movable/MovableSystem.cpp b/lib/wal/sources/System/Movable/MovableSystem.cpp new file mode 100644 index 00000000..108b4c93 --- /dev/null +++ b/lib/wal/sources/System/Movable/MovableSystem.cpp @@ -0,0 +1,26 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#include "Component/Position/PositionComponent.hpp" +#include "System/Movable/MovableSystem.hpp" +#include "Component/Movable/MovableComponent.hpp" +#include "Wal.hpp" + +namespace WAL +{ + const std::type_info &MovableSystem::getComponent() const + { + return typeid(MovableComponent); + } + + void MovableSystem::onFixedUpdate(Entity &entity) + { + auto &movable = entity.getComponent(); + auto &position = entity.getComponent(); + + position.position += movable._velocity * WAL::timestep.count(); + movable._velocity = movable._acceleration * WAL::timestep.count(); + movable._acceleration = Vector3f(); + } +} \ No newline at end of file diff --git a/lib/wal/sources/System/Movable/MovableSystem.hpp b/lib/wal/sources/System/Movable/MovableSystem.hpp new file mode 100644 index 00000000..400c7830 --- /dev/null +++ b/lib/wal/sources/System/Movable/MovableSystem.hpp @@ -0,0 +1,30 @@ + +// +// Created by Zoe Roux on 5/17/21. +// + +#pragma once + +#include "System/System.hpp" + +namespace WAL +{ + //! @brief A system to handle movable entities. This system update velocity based on accelerations and positions based on velocity. + class MovableSystem : public System + { + public: + //! @inherit + const std::type_info &getComponent() const override; + //! @inherit + void onFixedUpdate(Entity &entity) override; + + //! @brief A default constructor + MovableSystem() = default; + //! @brief A movable system is copy constructable + MovableSystem(const MovableSystem &) = default; + //! @brief A default destructor + ~MovableSystem() override = default; + //! @brief A movable system is assignable. + MovableSystem &operator=(const MovableSystem &) = default; + }; +} diff --git a/lib/wal/sources/System/System.cpp b/lib/wal/sources/System/System.cpp new file mode 100644 index 00000000..e1cc2ec0 --- /dev/null +++ b/lib/wal/sources/System/System.cpp @@ -0,0 +1,15 @@ +// +// Created by Zoe Roux on 5/17/21. +// + +#include "System.hpp" + +namespace WAL +{ + + void System::onUpdate(Entity &entity, std::chrono::nanoseconds dtime) + {} + + void System::onFixedUpdate(Entity &entity) + {} +} \ No newline at end of file diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index b0cc02d4..998da6b9 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -17,17 +17,17 @@ namespace WAL System(System &&) = default; //! @brief Get the name of the component corresponding to this system. - virtual std::type_info &getComponent() const = 0; + virtual const std::type_info &getComponent() const = 0; //! @brief Update the corresponding component of the given entity //! @param entity The entity to update. //! @param dtime The delta time. - virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) = 0; + virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime); //! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up. //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. //! @param entity The entity to update. - virtual void onFixedUpdate(Entity &entity) = 0; + virtual void onFixedUpdate(Entity &entity); protected: //! @brief A system can't be instantiated, it should be derived. System() = default; diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index 0e4921ff..de82cddc 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -9,7 +9,7 @@ using namespace std::chrono_literals; namespace WAL { - std::chrono::nanoseconds WAL::_timestep = 8ms; + std::chrono::nanoseconds WAL::timestep = 8ms; SceneManager &WAL::getSceneManger() { @@ -27,8 +27,8 @@ namespace WAL lastTick = now; this->_update(dtime); - while (dtime > WAL::_timestep) { - dtime -= WAL::_timestep; + while (dtime > WAL::timestep) { + dtime -= WAL::timestep; this->_fixedUpdate(); } this->_renderer->render(); @@ -41,7 +41,7 @@ namespace WAL for (auto &system : this->_systems) { for (auto &entity : entities) { - auto &cmp = system->getComponent(); + const auto &cmp = system->getComponent(); if (!entity.hasComponent(cmp)) continue; // TODO handle dependencies. diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index e4c9f291..d5e9d797 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -32,15 +32,15 @@ namespace WAL //! @brief True if the engine should close after the end of the current tick. bool _shouldClose = false; - //! @brief The time between each fixed update. - static std::chrono::nanoseconds _timestep; - //! @brief Call the onUpdate of every system with every component void _update(std::chrono::nanoseconds dtime); //! @brief Call the onFixedUpdate of every system with every component void _fixedUpdate(); public: + //! @brief The time between each fixed update. + static std::chrono::nanoseconds timestep; + //! @brief Create a new system in place. //! @return The wal instance used to call this function is returned. This allow method chaining. template diff --git a/lib/wal/tests/EntityTests.cpp b/lib/wal/tests/EntityTests.cpp index aafb8b94..0ae20e85 100644 --- a/lib/wal/tests/EntityTests.cpp +++ b/lib/wal/tests/EntityTests.cpp @@ -4,11 +4,10 @@ #include "tests.hpp" #include "Entity/Entity.hpp" -#include "Component/Basics/PositionComponent.hpp" +#include "Component/Position/PositionComponent.hpp" #include using namespace WAL; -using namespace WAL::Components; TEST_CASE("Component", "[Entity]") {