From 15d9acac61887516418fcb93509e5dec0a55fa88 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Thu, 25 Jun 2020 00:22:03 +0200 Subject: [PATCH] [Registry] Now storing entity callbacks here instead of using a component. --- source/common/core/Registry.cpp | 9 +++++++++ source/common/core/Registry.hpp | 5 +++++ ...mponent.hpp => EntityCallbackContainer.hpp} | 18 ++++-------------- source/common/scene/Scene.cpp | 17 ++++++++--------- .../common/scene/component/ComponentType.hpp | 2 +- source/server/lua/loader/LuaEntityLoader.cpp | 11 ++++++----- source/server/lua/loader/LuaEntityLoader.hpp | 2 +- .../scene/controller/CollisionController.cpp | 10 ++++++---- .../world/save/WorldSaveBasicBackend.cpp | 16 +++++++++------- 9 files changed, 49 insertions(+), 41 deletions(-) rename source/common/scene/{component/LuaCallbackComponent.hpp => EntityCallbackContainer.hpp} (77%) diff --git a/source/common/core/Registry.cpp b/source/common/core/Registry.cpp index aa1a3159c..aea732d6e 100644 --- a/source/common/core/Registry.cpp +++ b/source/common/core/Registry.cpp @@ -224,6 +224,14 @@ const Recipe *Registry::getRecipe(const Inventory &inventory) const { return nullptr; } +EntityCallbackContainer &Registry::addEntityCallbackContainer(const std::string &stringID) { + return m_entityCallbacks.emplace(stringID, EntityCallbackContainer{}).first->second; +} + +EntityCallbackContainer &Registry::getEntityCallbackContainer(const std::string &stringID) { + return m_entityCallbacks.at(stringID); +} + void Registry::serialize(sf::Packet &packet) const { for (auto &it : m_items) { packet << u8(DataType::Item) << it; @@ -312,6 +320,7 @@ void Registry::clear() { m_keysID.clear(); m_entities.clear(); + m_entityCallbacks.clear(); m_entityRegistry.clear(); } diff --git a/source/common/core/Registry.hpp b/source/common/core/Registry.hpp index a0a7b41d9..1ca81e16b 100644 --- a/source/common/core/Registry.hpp +++ b/source/common/core/Registry.hpp @@ -36,6 +36,7 @@ #include "Biome.hpp" #include "Block.hpp" #include "Dimension.hpp" +#include "EntityCallbackContainer.hpp" #include "Item.hpp" #include "Key.hpp" #include "Network.hpp" @@ -108,6 +109,9 @@ class Registry : public gk::ISerializable { const Recipe *getRecipe(const Inventory &inventory) const; + EntityCallbackContainer &addEntityCallbackContainer(const std::string &stringID); + EntityCallbackContainer &getEntityCallbackContainer(const std::string &stringID); + void serialize(sf::Packet &packet) const override; void deserialize(sf::Packet &packet) override; @@ -146,6 +150,7 @@ class Registry : public gk::ISerializable { std::unordered_map m_keysID; std::unordered_map m_entities; + std::unordered_map m_entityCallbacks; entt::registry m_entityRegistry; diff --git a/source/common/scene/component/LuaCallbackComponent.hpp b/source/common/scene/EntityCallbackContainer.hpp similarity index 77% rename from source/common/scene/component/LuaCallbackComponent.hpp rename to source/common/scene/EntityCallbackContainer.hpp index 800d4f25b..9f4300c15 100644 --- a/source/common/scene/component/LuaCallbackComponent.hpp +++ b/source/common/scene/EntityCallbackContainer.hpp @@ -24,23 +24,13 @@ * * ===================================================================================== */ -#ifndef LUACALLBACKCOMPONENT_HPP_ -#define LUACALLBACKCOMPONENT_HPP_ - -#include +#ifndef ENTITYCALLBACKCONTAINER_HPP_ +#define ENTITYCALLBACKCONTAINER_HPP_ #include -#include "Network.hpp" - -struct LuaCallbackComponent : public gk::ISerializable { +struct EntityCallbackContainer { sol::unsafe_function collisionCallback; - - // FIXME - void serialize(sf::Packet &) const {} - void deserialize(sf::Packet &) {} - bool isUpdated = false; - Network::Command packetType; }; -#endif // LUACALLBACKCOMPONENT_HPP_ +#endif // ENTITYCALLBACKCONTAINER_HPP_ diff --git a/source/common/scene/Scene.cpp b/source/common/scene/Scene.cpp index 89f798778..173155bd2 100644 --- a/source/common/scene/Scene.cpp +++ b/source/common/scene/Scene.cpp @@ -112,19 +112,18 @@ void extend_meta_type(const std::string &name, ComponentType type, bool isSerial #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", ComponentType::Hitbox, false, true); - extend_meta_type ("AnimationComponent", ComponentType::Animation, true, true); - extend_meta_type ("DrawableDef", ComponentType::Drawable, true, true); - extend_meta_type ("ItemStack", ComponentType::ItemStack, false, true); - extend_meta_type("LuaCallbackComponent", ComponentType::LuaCallback, false, false); - extend_meta_type ("NetworkComponent", ComponentType::Network, false, false); - extend_meta_type ("PositionComponent", ComponentType::Position, true, true); - extend_meta_type ("RotationComponent", ComponentType::Rotation, true, true); + extend_meta_type ("gk::DoubleBox", ComponentType::Hitbox, false, true); + extend_meta_type ("AnimationComponent", ComponentType::Animation, true, true); + extend_meta_type ("DrawableDef", ComponentType::Drawable, true, true); + extend_meta_type ("ItemStack", ComponentType::ItemStack, false, true); + extend_meta_type ("NetworkComponent", ComponentType::Network, false, false); + extend_meta_type ("PositionComponent", ComponentType::Position, true, true); + extend_meta_type ("RotationComponent", ComponentType::Rotation, true, true); + extend_meta_type ("EntityID", ComponentType::EntityID, false, true); } diff --git a/source/common/scene/component/ComponentType.hpp b/source/common/scene/component/ComponentType.hpp index cbaab318d..3970e987d 100644 --- a/source/common/scene/component/ComponentType.hpp +++ b/source/common/scene/component/ComponentType.hpp @@ -36,7 +36,7 @@ enum class ComponentType { Drawable = 3, Network = 4, - LuaCallback = 5, // FIXME + EntityID = 5, Hitbox = 6, ItemStack = 7, }; diff --git a/source/server/lua/loader/LuaEntityLoader.cpp b/source/server/lua/loader/LuaEntityLoader.cpp index 8a75bdf24..99c56d79c 100644 --- a/source/server/lua/loader/LuaEntityLoader.cpp +++ b/source/server/lua/loader/LuaEntityLoader.cpp @@ -28,7 +28,6 @@ #include "AnimationComponent.hpp" #include "DrawableDef.hpp" -#include "LuaCallbackComponent.hpp" #include "LuaEntityLoader.hpp" #include "LuaMod.hpp" #include "NetworkComponent.hpp" @@ -43,7 +42,7 @@ void LuaEntityLoader::loadEntity(const sol::table &table) const { entt::registry ®istry = Registry::getInstance().entityRegistry(); entt::entity entity = Registry::getInstance().registerEntity(m_stringID); - tryLoadCallbacks(table, registry, entity); + tryLoadCallbacks(table); sol::optional properties = table["properties"]; if (properties != sol::nullopt) { @@ -79,6 +78,7 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table registry.emplace(entity, pos.x, pos.y, pos.z, dim); registry.emplace(entity, entity); + registry.emplace(entity, entityID); // FIXME: This code is too specific to the item drop entity ItemStack *itemStack = tryLoadItemStack(table, registry, entity); @@ -92,11 +92,12 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table gkError() << "In mod '" + m_mod.id() + "': Cannot find entity with id '" + entityID + "'"; } -void LuaEntityLoader::tryLoadCallbacks(const sol::table &table, entt::registry ®istry, entt::entity entity) const { +void LuaEntityLoader::tryLoadCallbacks(const sol::table &table) const { + auto &entityCallbackContainer = Registry::getInstance().addEntityCallbackContainer(m_stringID); + sol::optional onCollision = table["on_collision"]; if (onCollision != sol::nullopt) { - auto &luaCallbackComponent = registry.emplace(entity); - luaCallbackComponent.collisionCallback = onCollision.value(); + entityCallbackContainer.collisionCallback = onCollision.value(); } } diff --git a/source/server/lua/loader/LuaEntityLoader.hpp b/source/server/lua/loader/LuaEntityLoader.hpp index 4f873bff1..c8139b29e 100644 --- a/source/server/lua/loader/LuaEntityLoader.hpp +++ b/source/server/lua/loader/LuaEntityLoader.hpp @@ -45,7 +45,7 @@ class LuaEntityLoader { void spawnEntity(const std::string &entityID, const sol::table &table) const; private: - void tryLoadCallbacks(const sol::table &table, entt::registry ®istry, entt::entity entity) const; + void tryLoadCallbacks(const sol::table &table) const; void tryLoadVisual(const sol::table &table, entt::registry ®istry, entt::entity entity) const; void tryLoadRotation(const sol::table &table, entt::registry ®istry, entt::entity entity) const; void tryLoadAnimation(const sol::table &table, entt::registry ®istry, entt::entity entity) const; diff --git a/source/server/scene/controller/CollisionController.cpp b/source/server/scene/controller/CollisionController.cpp index 93102f754..2d89c2a67 100644 --- a/source/server/scene/controller/CollisionController.cpp +++ b/source/server/scene/controller/CollisionController.cpp @@ -29,21 +29,23 @@ #include "CollisionController.hpp" #include "EntityWrapper.hpp" #include "ItemStack.hpp" -#include "LuaCallbackComponent.hpp" #include "NetworkComponent.hpp" #include "PlayerList.hpp" #include "PositionComponent.hpp" +#include "Registry.hpp" #include "ServerCommandHandler.hpp" void CollisionController::update(entt::registry ®istry) { - registry.view().each([&](auto id, auto &position, auto &box, auto &luaCallbackComponent) { + registry.view().each([&](auto entity, auto &position, auto &box, auto &id) { for (auto &it : m_players) { if (it.second.dimension() == position.dimension) { gk::DoubleBox hitbox = box + gk::Vector3d{position.x, position.y, position.z}; gk::DoubleBox playerHitbox = it.second.hitbox() + gk::Vector3d{it.second.x(), it.second.y(), it.second.z()}; if (hitbox.intersects(playerHitbox)) { - EntityWrapper entity{id, registry}; - luaCallbackComponent.collisionCallback(entity, it.second, *m_server); + EntityWrapper entityWrapper{entity, registry}; + + auto &entityCallbackContainer = Registry::getInstance().getEntityCallbackContainer(id); + entityCallbackContainer.collisionCallback(entityWrapper, it.second, *m_server); } } } diff --git a/source/server/world/save/WorldSaveBasicBackend.cpp b/source/server/world/save/WorldSaveBasicBackend.cpp index 6d14abfde..f7d4a35ee 100644 --- a/source/server/world/save/WorldSaveBasicBackend.cpp +++ b/source/server/world/save/WorldSaveBasicBackend.cpp @@ -156,6 +156,7 @@ void WorldSaveBasicBackend::save(const std::string &name) { } #include "AnimationComponent.hpp" +#include "ComponentType.hpp" #include "DrawableDef.hpp" #include "PositionComponent.hpp" #include "RotationComponent.hpp" @@ -177,7 +178,7 @@ void WorldSaveBasicBackend::loadEntities(sf::Packet &save, ServerWorld &world) { m_entityMap.emplace(entityID, entity); registry.emplace(entity, entity); - gkDebug() << "Creating entity" << std::underlying_type_t(entityID); + // gkDebug() << "Creating entity" << std::underlying_type_t(entityID); u32 componentCount; save >> componentCount; @@ -186,7 +187,7 @@ void WorldSaveBasicBackend::loadEntities(sf::Packet &save, ServerWorld &world) { ComponentType type; save >> type; - gkDebug() << "Loading component" << (u16)type << "for entity" << std::underlying_type_t(entityID); + // gkDebug() << "Loading component" << (u16)type << "for entity" << std::underlying_type_t(entityID); if (type == ComponentType::Position) { save >> registry.emplace(entity); @@ -206,14 +207,15 @@ void WorldSaveBasicBackend::loadEntities(sf::Packet &save, ServerWorld &world) { else if (type == ComponentType::Hitbox) { save >> registry.emplace(entity); } + else if (type == ComponentType::EntityID) { + save >> registry.emplace(entity); + } else gkWarning() << "Unknown component with type" << (int)type; } } } -#include "ComponentType.hpp" - void WorldSaveBasicBackend::saveEntities(sf::Packet &save, ServerWorld &world) { entt::registry ®istry = world.scene().registry(); @@ -232,18 +234,18 @@ void WorldSaveBasicBackend::saveEntities(sf::Packet &save, ServerWorld &world) { Network::Packet packet = type.func("save"_hs).invoke({}, component).template cast(); entityPacket.append(packet.getData(), packet.getDataSize()); - gkDebug() << "Serializing component" << type.prop("name"_hs).value().template cast() << "for entity" << std::underlying_type_t(entity) << "of size" << packet.getDataSize(); + // gkDebug() << "Serializing component" << type.prop("name"_hs).value().template cast() << "for entity" << std::underlying_type_t(entity) << "of size" << packet.getDataSize(); ++componentCount; } }); - gkDebug() << "Saving" << componentCount << "components for entity" << std::underlying_type_t(entity); + // gkDebug() << "Saving" << componentCount << "components for entity" << std::underlying_type_t(entity); save << network.entityID << componentCount; save.append(entityPacket.getData(), entityPacket.getDataSize()); }); - gkDebug() << "Saving" << view.size() << "entities in dimension" << world.dimension().id(); + // gkDebug() << "Saving" << view.size() << "entities in dimension" << world.dimension().id(); }