diff --git a/source/client/scene/AbstractController.hpp b/source/client/scene/AbstractController.hpp new file mode 100644 index 00000000..077833cc --- /dev/null +++ b/source/client/scene/AbstractController.hpp @@ -0,0 +1,41 @@ +/* + * ===================================================================================== + * + * OpenMiner + * + * Copyright (C) 2018-2020 Unarelith, Quentin Bazin + * Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) + * + * This file is part of OpenMiner. + * + * OpenMiner is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * OpenMiner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenMiner; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * ===================================================================================== + */ +#ifndef ABSTRACTCONTROLLER_HPP_ +#define ABSTRACTCONTROLLER_HPP_ + +#include +#include + +#include + +class AbstractController { + public: + virtual void update(entt::DefaultRegistry &) {} + virtual void draw(entt::DefaultRegistry &, gk::RenderTarget &, gk::RenderStates) {} +}; + +#endif // ABSTRACTCONTROLLER_HPP_ diff --git a/source/client/scene/AnimationController.hpp b/source/client/scene/AnimationController.hpp index 8154fe02..1870f0df 100644 --- a/source/client/scene/AnimationController.hpp +++ b/source/client/scene/AnimationController.hpp @@ -27,11 +27,11 @@ #ifndef ANIMATIONCONTROLLER_HPP_ #define ANIMATIONCONTROLLER_HPP_ -#include +#include "AbstractController.hpp" -class AnimationController { +class AnimationController : public AbstractController { public: - static void update(entt::DefaultRegistry ®istry); + void update(entt::DefaultRegistry ®istry) override; }; #endif // ANIMATIONCONTROLLER_HPP_ diff --git a/source/client/scene/CollisionController.cpp b/source/client/scene/CollisionController.cpp index 5cb0bccb..95cff89b 100644 --- a/source/client/scene/CollisionController.cpp +++ b/source/client/scene/CollisionController.cpp @@ -29,13 +29,13 @@ #include "InventoryCube.hpp" #include "ItemStack.hpp" -void CollisionController::update(entt::DefaultRegistry ®istry, ClientPlayer &player) { +void CollisionController::update(entt::DefaultRegistry ®istry) { // FIXME: This shouldn't use InventoryCube, but instead a callback stored in a CollisionComponent registry.view().each([&](auto entity, auto &cube, auto &box, auto &itemStack) { gk::DoubleBox hitbox = box + cube.getPosition(); - gk::DoubleBox playerHitbox = player.hitbox() + gk::Vector3d{player.x(), player.y(), player.z()}; + gk::DoubleBox playerHitbox = m_player.hitbox() + gk::Vector3d{m_player.x(), m_player.y(), m_player.z()}; if (hitbox.intersects(playerHitbox)) { - player.inventory().addStack(itemStack.item().stringID(), itemStack.amount()); + m_player.inventory().addStack(itemStack.item().stringID(), itemStack.amount()); registry.destroy(entity); } }); diff --git a/source/client/scene/CollisionController.hpp b/source/client/scene/CollisionController.hpp index 24e123ac..9f7741b8 100644 --- a/source/client/scene/CollisionController.hpp +++ b/source/client/scene/CollisionController.hpp @@ -27,13 +27,18 @@ #ifndef COLLISIONCONTROLLER_HPP_ #define COLLISIONCONTROLLER_HPP_ -#include +#include "AbstractController.hpp" class ClientPlayer; -class CollisionController { +class CollisionController : public AbstractController { public: - static void update(entt::DefaultRegistry ®istry, ClientPlayer &player); + CollisionController(ClientPlayer &player) : m_player(player) {} + + void update(entt::DefaultRegistry ®istry); + + private: + ClientPlayer &m_player; }; #endif // COLLISIONCONTROLLER_HPP_ diff --git a/source/client/scene/RenderingController.hpp b/source/client/scene/RenderingController.hpp index d50077a6..75af6d10 100644 --- a/source/client/scene/RenderingController.hpp +++ b/source/client/scene/RenderingController.hpp @@ -27,14 +27,11 @@ #ifndef RENDERINGCONTROLLER_HPP_ #define RENDERINGCONTROLLER_HPP_ -#include -#include +#include "AbstractController.hpp" -#include - -class RenderingController { +class RenderingController : public AbstractController { public: - static void draw(entt::DefaultRegistry ®istry, gk::RenderTarget &target, gk::RenderStates states); + void draw(entt::DefaultRegistry ®istry, gk::RenderTarget &target, gk::RenderStates states) override; }; #endif // RENDERINGCONTROLLER_HPP_ diff --git a/source/client/scene/Scene.cpp b/source/client/scene/Scene.cpp index a9d28c15..c6227d2a 100644 --- a/source/client/scene/Scene.cpp +++ b/source/client/scene/Scene.cpp @@ -31,11 +31,14 @@ #include "RenderingController.hpp" Scene::Scene(ClientPlayer &player) : m_player(player) { + m_controllers.emplace_back(new AnimationController); + m_controllers.emplace_back(new CollisionController(player)); + m_controllers.emplace_back(new RenderingController); } void Scene::update() { - AnimationController::update(m_registry); - CollisionController::update(m_registry, m_player); + for (auto &controller : m_controllers) + controller->update(m_registry); } void Scene::draw(gk::RenderTarget &target, gk::RenderStates states) const { @@ -45,6 +48,7 @@ void Scene::draw(gk::RenderTarget &target, gk::RenderStates states) const { gk::Vector3d cameraPosition = m_camera->getDPosition(); states.transform.translate(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z); - RenderingController::draw(m_registry, target, states); + for (auto &controller : m_controllers) + controller->draw(m_registry, target, states); } diff --git a/source/client/scene/Scene.hpp b/source/client/scene/Scene.hpp index 3628d262..ddbf7d32 100644 --- a/source/client/scene/Scene.hpp +++ b/source/client/scene/Scene.hpp @@ -27,12 +27,16 @@ #ifndef SCENE_HPP_ #define SCENE_HPP_ +#include +#include + #include #include -#include #include +#include "AbstractController.hpp" + class ClientPlayer; class Scene : public gk::Drawable { @@ -52,9 +56,9 @@ class Scene : public gk::Drawable { gk::Camera *m_camera = nullptr; - gk::BoxShape m_testBox; - mutable entt::DefaultRegistry m_registry; + + std::deque> m_controllers; }; #endif // SCENE_HPP_