From d17a410a656b7932c05209921d89d094983435e6 Mon Sep 17 00:00:00 2001 From: Askou Date: Wed, 9 Jun 2021 17:23:55 +0200 Subject: [PATCH] basic gravity --- CMakeLists.txt | 2 ++ .../Component/Gravity/GravityComponent.cpp | 17 ++++++++++ .../Component/Gravity/GravityComponent.hpp | 32 +++++++++++++++++++ sources/Runner/Runner.cpp | 4 +++ sources/System/Gravity/GravitySystem.cpp | 21 ++++++++++++ sources/System/Gravity/GravitySystem.hpp | 30 +++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 sources/Component/Gravity/GravityComponent.cpp create mode 100644 sources/Component/Gravity/GravityComponent.hpp create mode 100644 sources/System/Gravity/GravitySystem.cpp create mode 100644 sources/System/Gravity/GravitySystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d7dc8112..8ae95107 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,8 @@ set(SOURCES sources/System/Sound/PlayerSoundManagerSystem.hpp sources/System/Music/MusicSystem.hpp sources/System/Music/MusicSystem.cpp + sources/Component/Gravity/GravityComponent.cpp + sources/System/Gravity/GravitySystem.cpp ) add_executable(bomberman diff --git a/sources/Component/Gravity/GravityComponent.cpp b/sources/Component/Gravity/GravityComponent.cpp new file mode 100644 index 00000000..a6ee1db4 --- /dev/null +++ b/sources/Component/Gravity/GravityComponent.cpp @@ -0,0 +1,17 @@ +// +// Created by Tom Augier on 2021-05-20. +// + +#include "GravityComponent.hpp" + +namespace BBM +{ + GravityComponent::GravityComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + + WAL::Component *GravityComponent::clone(WAL::Entity &entity) const + { + return new GravityComponent(entity); + } +} \ No newline at end of file diff --git a/sources/Component/Gravity/GravityComponent.hpp b/sources/Component/Gravity/GravityComponent.hpp new file mode 100644 index 00000000..f2b6a368 --- /dev/null +++ b/sources/Component/Gravity/GravityComponent.hpp @@ -0,0 +1,32 @@ +// +// Created by Tom Augier on 2021-05-20. +// + +#pragma once + +#include +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" + +namespace BBM +{ + class GravityComponent : public WAL::Component + { + public: + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief Constructor + GravityComponent(WAL::Entity &entity); + + //! @brief A Gravity component can't be instantiated, it should be derived. + GravityComponent(const GravityComponent &) = default; + + //! @brief default destructor + ~GravityComponent() override = default; + + //! @brief A Gravity component can't be assigned + GravityComponent &operator=(const GravityComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 2ee46062..de4c821f 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -39,6 +39,8 @@ #include "Component/Sound/SoundComponent.hpp" #include "System/Sound/PlayerSoundManagerSystem.hpp" #include "System/Music/MusicSystem.hpp" +#include "Component/Gravity/GravityComponent.hpp" +#include "System/Gravity/GravitySystem.hpp" namespace RAY3D = RAY::Drawables::Drawables3D; namespace RAY2D = RAY::Drawables::Drawables2D; @@ -66,6 +68,7 @@ namespace BBM .addSystem() .addSystem() .addSystem() + .addSystem() .addSystem() .addSystem(); } @@ -101,6 +104,7 @@ namespace BBM .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() .addComponent(soundPath) + .addComponent() .addComponent() .addComponent(1, [](WAL::Entity &entity) { auto &animation = entity.getComponent(); diff --git a/sources/System/Gravity/GravitySystem.cpp b/sources/System/Gravity/GravitySystem.cpp new file mode 100644 index 00000000..8e0801e1 --- /dev/null +++ b/sources/System/Gravity/GravitySystem.cpp @@ -0,0 +1,21 @@ +// +// Created by Tom Augier on 2021-06-09. +// + +#include "GravitySystem.hpp" + +namespace BBM +{ + GravitySystem::GravitySystem(WAL::Wal &wal) + : System(wal) + {} + + void GravitySystem::onFixedUpdate(WAL::ViewEntity &entity) + { + auto &movable = entity.get(); + auto &position = entity.get(); + + if (position.getY() > 0) + movable.addForce(Vector3f(0, -0.5, 0)); + } +} \ No newline at end of file diff --git a/sources/System/Gravity/GravitySystem.hpp b/sources/System/Gravity/GravitySystem.hpp new file mode 100644 index 00000000..59963ac7 --- /dev/null +++ b/sources/System/Gravity/GravitySystem.hpp @@ -0,0 +1,30 @@ +// +// Created by Tom Augier on 2021-06-09. +// + +#pragma once + +#include "Component/Movable/MovableComponent.hpp" +#include "Component/Position/PositionComponent.hpp" +#include "Component/Gravity/GravityComponent.hpp" +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Gravity entities. + class GravitySystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::ViewEntity &entity) override; + + //! @brief A default constructor + explicit GravitySystem(WAL::Wal &wal); + //! @brief A Gravity system is copy constructable + GravitySystem(const GravitySystem &) = default; + //! @brief A default destructor + ~GravitySystem() override = default; + //! @brief A system is not assignable. + GravitySystem &operator=(const GravitySystem &) = delete; + }; +} \ No newline at end of file