[Scene] Added 'createEntityFromModel()'.

This commit is contained in:
Quentin Bazin 2020-05-20 04:47:26 +02:00
parent 17af1861b5
commit 0653b802b8
5 changed files with 119 additions and 35 deletions

2
external/entt vendored

@ -1 +1 @@
Subproject commit 0adee56d764686c16fbccfa1eee2446efdce737c Subproject commit 39baa59625ff59cda4425721a0c52819fd715476

View File

@ -0,0 +1,95 @@
/*
* =====================================================================================
*
* 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
*
* =====================================================================================
*/
#include <gk/core/Debug.hpp>
#include <entt/entt.hpp>
#include "Scene.hpp"
Scene::Scene() {
registerComponents();
}
void Scene::update() {
for (auto &controller : m_controllers)
controller->update(m_registry);
}
// Opaque entity stamper: https://github.com/skypjack/entt/issues/481#issuecomment-627614706
entt::entity Scene::createEntityFromModel(entt::registry &modelRegistry, entt::entity modelEntity) {
if (!modelRegistry.valid(modelEntity)) {
gkError() << "Failed to create entity from model: Model entity with ID" << (u32)modelEntity << "doesn't exist";
return entt::null;
}
auto other = m_registry.create();
modelRegistry.visit(modelEntity, [&](const auto component) {
const auto type = entt::resolve_type(component);
const auto any = type.func("get"_hs).invoke({}, std::ref(modelRegistry), modelEntity);
type.func("set"_hs).invoke({}, std::ref(m_registry), other, any);
});
return other;
}
template<typename Type>
Type &get(entt::registry &registry, entt::entity entity) {
return registry.get_or_emplace<Type>(entity);
}
template<typename Type>
Type &set(entt::registry &registry, entt::entity entity, const Type &instance) {
return registry.emplace_or_replace<Type>(entity, instance);
}
template<typename Type>
void extend_meta_type() {
entt::meta<Type>().type()
.template func<&get<Type>, entt::as_ref_t>("get"_hs)
.template func<&set<Type>>("set"_hs);
}
#include <gk/core/Box.hpp>
#include "AnimationComponent.hpp"
#include "DrawableDef.hpp"
#include "ItemStack.hpp"
#include "LuaCallbackComponent.hpp"
#include "NetworkComponent.hpp"
#include "PositionComponent.hpp"
#include "RotationComponent.hpp"
void Scene::registerComponents() {
extend_meta_type<gk::DoubleBox>();
extend_meta_type<AnimationComponent>();
extend_meta_type<DrawableDef>();
extend_meta_type<ItemStack>();
extend_meta_type<LuaCallbackComponent>();
extend_meta_type<NetworkComponent>();
extend_meta_type<PositionComponent>();
extend_meta_type<RotationComponent>();
}

View File

@ -34,12 +34,18 @@
class Scene { class Scene {
public: public:
virtual void update() { for (auto &controller : m_controllers) controller->update(m_registry); } Scene();
virtual void update();
entt::entity createEntityFromModel(entt::registry &modelRegistry, entt::entity modelEntity);
const entt::registry &registry() const { return m_registry; } const entt::registry &registry() const { return m_registry; }
entt::registry &registry() { return m_registry; } entt::registry &registry() { return m_registry; }
protected: protected:
void registerComponents();
mutable entt::registry m_registry; mutable entt::registry m_registry;
std::deque<std::unique_ptr<AbstractController>> m_controllers; std::deque<std::unique_ptr<AbstractController>> m_controllers;

View File

@ -35,6 +35,7 @@
#include "PositionComponent.hpp" #include "PositionComponent.hpp"
#include "Registry.hpp" #include "Registry.hpp"
#include "RotationComponent.hpp" #include "RotationComponent.hpp"
#include "WorldController.hpp"
void LuaEntityLoader::loadEntity(const sol::table &table) const { void LuaEntityLoader::loadEntity(const sol::table &table) const {
std::string stringID = m_mod.id() + ":" + table["id"].get<std::string>(); std::string stringID = m_mod.id() + ":" + table["id"].get<std::string>();
@ -101,8 +102,6 @@ void LuaEntityLoader::loadEntity(const sol::table &table) const {
} }
} }
#include "WorldController.hpp"
void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table &table) const { void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table &table) const {
gk::Vector3f pos; gk::Vector3f pos;
u16 dim; u16 dim;
@ -125,43 +124,27 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table
stack.setAmount(itemStack.value()[2].get_or(1)); stack.setAmount(itemStack.value()[2].get_or(1));
} }
// Create entity copy here entt::registry &modelRegistry = Registry::getInstance().entityRegistry();
entt::registry &registry = Registry::getInstance().entityRegistry(); entt::entity modelEntity = Registry::getInstance().getEntityFromStringID(entityID);
entt::entity entityModel = Registry::getInstance().getEntityFromStringID(entityID); if (modelEntity != entt::null) {
if (entityModel != entt::null) {
ServerWorld &world = m_worldController.getWorld(dim); ServerWorld &world = m_worldController.getWorld(dim);
entt::registry &sceneRegistry = world.scene().registry(); Scene &scene = world.scene();
entt::entity entity = sceneRegistry.create(); entt::registry &registry = scene.registry();
entt::entity entity = scene.createEntityFromModel(modelRegistry, modelEntity);
sceneRegistry.emplace<PositionComponent>(entity, pos.x, pos.y, pos.z, dim); registry.emplace<PositionComponent>(entity, pos.x, pos.y, pos.z, dim);
sceneRegistry.emplace<NetworkComponent>(entity, entity); registry.emplace<NetworkComponent>(entity, entity);
if (registry.has<RotationComponent>(entityModel)) // FIXME: This code is too specific to the item drop entity
sceneRegistry.emplace<RotationComponent>(entity);
auto *luaCallbackComponent = registry.try_get<LuaCallbackComponent>(entityModel);
if (luaCallbackComponent)
sceneRegistry.emplace<LuaCallbackComponent>(entity, *luaCallbackComponent);
auto *drawable = registry.try_get<DrawableDef>(entityModel);
if (drawable) {
auto &drawableDef = sceneRegistry.emplace<DrawableDef>(entity, *drawable);
if (stack.amount()) { if (stack.amount()) {
auto &cube = drawableDef.getInventoryCubeDef(); auto *drawable = registry.try_get<DrawableDef>(entity);
if (drawable) {
auto &cube = drawable->getInventoryCubeDef();
cube.blockID = stack.item().stringID(); cube.blockID = stack.item().stringID();
} }
registry.emplace<ItemStack>(entity, stack);
} }
auto *animation = registry.try_get<AnimationComponent>(entityModel);
if (animation)
sceneRegistry.emplace<AnimationComponent>(entity, *animation);
auto *hitbox = registry.try_get<gk::DoubleBox>(entityModel);
if (hitbox)
sceneRegistry.emplace<gk::DoubleBox>(entity, *hitbox);
if (stack.amount())
sceneRegistry.emplace<ItemStack>(entity, stack);
} }
else else
gkError() << "In mod '" + m_mod.id() + "': Cannot find entity with id '" + entityID + "'"; gkError() << "In mod '" + m_mod.id() + "': Cannot find entity with id '" + entityID + "'";