[Scene] Now using AbstractController instead of static classes.

This commit is contained in:
Quentin Bazin 2020-04-05 01:28:06 +02:00
parent d090e6903d
commit 53460f2340
7 changed files with 72 additions and 21 deletions

View File

@ -0,0 +1,41 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* 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 <gk/gl/RenderStates.hpp>
#include <gk/gl/RenderTarget.hpp>
#include <entt/entt.hpp>
class AbstractController {
public:
virtual void update(entt::DefaultRegistry &) {}
virtual void draw(entt::DefaultRegistry &, gk::RenderTarget &, gk::RenderStates) {}
};
#endif // ABSTRACTCONTROLLER_HPP_

View File

@ -27,11 +27,11 @@
#ifndef ANIMATIONCONTROLLER_HPP_
#define ANIMATIONCONTROLLER_HPP_
#include <entt/entt.hpp>
#include "AbstractController.hpp"
class AnimationController {
class AnimationController : public AbstractController {
public:
static void update(entt::DefaultRegistry &registry);
void update(entt::DefaultRegistry &registry) override;
};
#endif // ANIMATIONCONTROLLER_HPP_

View File

@ -29,13 +29,13 @@
#include "InventoryCube.hpp"
#include "ItemStack.hpp"
void CollisionController::update(entt::DefaultRegistry &registry, ClientPlayer &player) {
void CollisionController::update(entt::DefaultRegistry &registry) {
// FIXME: This shouldn't use InventoryCube, but instead a callback stored in a CollisionComponent
registry.view<InventoryCube, gk::DoubleBox, ItemStack>().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);
}
});

View File

@ -27,13 +27,18 @@
#ifndef COLLISIONCONTROLLER_HPP_
#define COLLISIONCONTROLLER_HPP_
#include <entt/entt.hpp>
#include "AbstractController.hpp"
class ClientPlayer;
class CollisionController {
class CollisionController : public AbstractController {
public:
static void update(entt::DefaultRegistry &registry, ClientPlayer &player);
CollisionController(ClientPlayer &player) : m_player(player) {}
void update(entt::DefaultRegistry &registry);
private:
ClientPlayer &m_player;
};
#endif // COLLISIONCONTROLLER_HPP_

View File

@ -27,14 +27,11 @@
#ifndef RENDERINGCONTROLLER_HPP_
#define RENDERINGCONTROLLER_HPP_
#include <gk/gl/RenderStates.hpp>
#include <gk/gl/RenderTarget.hpp>
#include "AbstractController.hpp"
#include <entt/entt.hpp>
class RenderingController {
class RenderingController : public AbstractController {
public:
static void draw(entt::DefaultRegistry &registry, gk::RenderTarget &target, gk::RenderStates states);
void draw(entt::DefaultRegistry &registry, gk::RenderTarget &target, gk::RenderStates states) override;
};
#endif // RENDERINGCONTROLLER_HPP_

View File

@ -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);
}

View File

@ -27,12 +27,16 @@
#ifndef SCENE_HPP_
#define SCENE_HPP_
#include <deque>
#include <memory>
#include <gk/gl/Camera.hpp>
#include <gk/gl/Drawable.hpp>
#include <gk/graphics/BoxShape.hpp>
#include <entt/entt.hpp>
#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<std::unique_ptr<AbstractController>> m_controllers;
};
#endif // SCENE_HPP_