[Scene] Now using AbstractController instead of static classes.
This commit is contained in:
parent
d090e6903d
commit
53460f2340
41
source/client/scene/AbstractController.hpp
Normal file
41
source/client/scene/AbstractController.hpp
Normal 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_
|
@ -27,11 +27,11 @@
|
|||||||
#ifndef ANIMATIONCONTROLLER_HPP_
|
#ifndef ANIMATIONCONTROLLER_HPP_
|
||||||
#define ANIMATIONCONTROLLER_HPP_
|
#define ANIMATIONCONTROLLER_HPP_
|
||||||
|
|
||||||
#include <entt/entt.hpp>
|
#include "AbstractController.hpp"
|
||||||
|
|
||||||
class AnimationController {
|
class AnimationController : public AbstractController {
|
||||||
public:
|
public:
|
||||||
static void update(entt::DefaultRegistry ®istry);
|
void update(entt::DefaultRegistry ®istry) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ANIMATIONCONTROLLER_HPP_
|
#endif // ANIMATIONCONTROLLER_HPP_
|
||||||
|
@ -29,13 +29,13 @@
|
|||||||
#include "InventoryCube.hpp"
|
#include "InventoryCube.hpp"
|
||||||
#include "ItemStack.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
|
// 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) {
|
registry.view<InventoryCube, gk::DoubleBox, ItemStack>().each([&](auto entity, auto &cube, auto &box, auto &itemStack) {
|
||||||
gk::DoubleBox hitbox = box + cube.getPosition();
|
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)) {
|
if (hitbox.intersects(playerHitbox)) {
|
||||||
player.inventory().addStack(itemStack.item().stringID(), itemStack.amount());
|
m_player.inventory().addStack(itemStack.item().stringID(), itemStack.amount());
|
||||||
registry.destroy(entity);
|
registry.destroy(entity);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -27,13 +27,18 @@
|
|||||||
#ifndef COLLISIONCONTROLLER_HPP_
|
#ifndef COLLISIONCONTROLLER_HPP_
|
||||||
#define COLLISIONCONTROLLER_HPP_
|
#define COLLISIONCONTROLLER_HPP_
|
||||||
|
|
||||||
#include <entt/entt.hpp>
|
#include "AbstractController.hpp"
|
||||||
|
|
||||||
class ClientPlayer;
|
class ClientPlayer;
|
||||||
|
|
||||||
class CollisionController {
|
class CollisionController : public AbstractController {
|
||||||
public:
|
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_
|
#endif // COLLISIONCONTROLLER_HPP_
|
||||||
|
@ -27,14 +27,11 @@
|
|||||||
#ifndef RENDERINGCONTROLLER_HPP_
|
#ifndef RENDERINGCONTROLLER_HPP_
|
||||||
#define RENDERINGCONTROLLER_HPP_
|
#define RENDERINGCONTROLLER_HPP_
|
||||||
|
|
||||||
#include <gk/gl/RenderStates.hpp>
|
#include "AbstractController.hpp"
|
||||||
#include <gk/gl/RenderTarget.hpp>
|
|
||||||
|
|
||||||
#include <entt/entt.hpp>
|
class RenderingController : public AbstractController {
|
||||||
|
|
||||||
class RenderingController {
|
|
||||||
public:
|
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_
|
#endif // RENDERINGCONTROLLER_HPP_
|
||||||
|
@ -31,11 +31,14 @@
|
|||||||
#include "RenderingController.hpp"
|
#include "RenderingController.hpp"
|
||||||
|
|
||||||
Scene::Scene(ClientPlayer &player) : m_player(player) {
|
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() {
|
void Scene::update() {
|
||||||
AnimationController::update(m_registry);
|
for (auto &controller : m_controllers)
|
||||||
CollisionController::update(m_registry, m_player);
|
controller->update(m_registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
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();
|
gk::Vector3d cameraPosition = m_camera->getDPosition();
|
||||||
states.transform.translate(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,12 +27,16 @@
|
|||||||
#ifndef SCENE_HPP_
|
#ifndef SCENE_HPP_
|
||||||
#define SCENE_HPP_
|
#define SCENE_HPP_
|
||||||
|
|
||||||
|
#include <deque>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <gk/gl/Camera.hpp>
|
#include <gk/gl/Camera.hpp>
|
||||||
#include <gk/gl/Drawable.hpp>
|
#include <gk/gl/Drawable.hpp>
|
||||||
#include <gk/graphics/BoxShape.hpp>
|
|
||||||
|
|
||||||
#include <entt/entt.hpp>
|
#include <entt/entt.hpp>
|
||||||
|
|
||||||
|
#include "AbstractController.hpp"
|
||||||
|
|
||||||
class ClientPlayer;
|
class ClientPlayer;
|
||||||
|
|
||||||
class Scene : public gk::Drawable {
|
class Scene : public gk::Drawable {
|
||||||
@ -52,9 +56,9 @@ class Scene : public gk::Drawable {
|
|||||||
|
|
||||||
gk::Camera *m_camera = nullptr;
|
gk::Camera *m_camera = nullptr;
|
||||||
|
|
||||||
gk::BoxShape m_testBox;
|
|
||||||
|
|
||||||
mutable entt::DefaultRegistry m_registry;
|
mutable entt::DefaultRegistry m_registry;
|
||||||
|
|
||||||
|
std::deque<std::unique_ptr<AbstractController>> m_controllers;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SCENE_HPP_
|
#endif // SCENE_HPP_
|
||||||
|
Loading…
x
Reference in New Issue
Block a user