Skip to content

Commit

Permalink
[Registry] Now storing entity callbacks here instead of using a compo…
Browse files Browse the repository at this point in the history
…nent.
  • Loading branch information
Unarelith committed Jun 24, 2020
1 parent f32cdf8 commit 15d9aca
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 41 deletions.
9 changes: 9 additions & 0 deletions source/common/core/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -312,6 +320,7 @@ void Registry::clear() {
m_keysID.clear();

m_entities.clear();
m_entityCallbacks.clear();
m_entityRegistry.clear();
}

Expand Down
5 changes: 5 additions & 0 deletions source/common/core/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -146,6 +150,7 @@ class Registry : public gk::ISerializable {
std::unordered_map<std::string, u16> m_keysID;

std::unordered_map<std::string, entt::entity> m_entities;
std::unordered_map<std::string, EntityCallbackContainer> m_entityCallbacks;

entt::registry m_entityRegistry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,13 @@
*
* =====================================================================================
*/
#ifndef LUACALLBACKCOMPONENT_HPP_
#define LUACALLBACKCOMPONENT_HPP_

#include <gk/core/ISerializable.hpp>
#ifndef ENTITYCALLBACKCONTAINER_HPP_
#define ENTITYCALLBACKCONTAINER_HPP_

#include <sol/sol.hpp>

#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_
17 changes: 8 additions & 9 deletions source/common/scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> ("gk::DoubleBox", ComponentType::Hitbox, false, true);
extend_meta_type<AnimationComponent> ("AnimationComponent", ComponentType::Animation, true, true);
extend_meta_type<DrawableDef> ("DrawableDef", ComponentType::Drawable, true, true);
extend_meta_type<ItemStack> ("ItemStack", ComponentType::ItemStack, false, true);
extend_meta_type<LuaCallbackComponent>("LuaCallbackComponent", ComponentType::LuaCallback, false, false);
extend_meta_type<NetworkComponent> ("NetworkComponent", ComponentType::Network, false, false);
extend_meta_type<PositionComponent> ("PositionComponent", ComponentType::Position, true, true);
extend_meta_type<RotationComponent> ("RotationComponent", ComponentType::Rotation, true, true);
extend_meta_type<gk::DoubleBox> ("gk::DoubleBox", ComponentType::Hitbox, false, true);
extend_meta_type<AnimationComponent> ("AnimationComponent", ComponentType::Animation, true, true);
extend_meta_type<DrawableDef> ("DrawableDef", ComponentType::Drawable, true, true);
extend_meta_type<ItemStack> ("ItemStack", ComponentType::ItemStack, false, true);
extend_meta_type<NetworkComponent> ("NetworkComponent", ComponentType::Network, false, false);
extend_meta_type<PositionComponent> ("PositionComponent", ComponentType::Position, true, true);
extend_meta_type<RotationComponent> ("RotationComponent", ComponentType::Rotation, true, true);
extend_meta_type<std::string> ("EntityID", ComponentType::EntityID, false, true);
}

2 changes: 1 addition & 1 deletion source/common/scene/component/ComponentType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum class ComponentType {
Drawable = 3,
Network = 4,

LuaCallback = 5, // FIXME
EntityID = 5,
Hitbox = 6,
ItemStack = 7,
};
Expand Down
11 changes: 6 additions & 5 deletions source/server/lua/loader/LuaEntityLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include "AnimationComponent.hpp"
#include "DrawableDef.hpp"
#include "LuaCallbackComponent.hpp"
#include "LuaEntityLoader.hpp"
#include "LuaMod.hpp"
#include "NetworkComponent.hpp"
Expand All @@ -43,7 +42,7 @@ void LuaEntityLoader::loadEntity(const sol::table &table) const {
entt::registry &registry = Registry::getInstance().entityRegistry();
entt::entity entity = Registry::getInstance().registerEntity(m_stringID);

tryLoadCallbacks(table, registry, entity);
tryLoadCallbacks(table);

sol::optional<sol::table> properties = table["properties"];
if (properties != sol::nullopt) {
Expand Down Expand Up @@ -79,6 +78,7 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table

registry.emplace<PositionComponent>(entity, pos.x, pos.y, pos.z, dim);
registry.emplace<NetworkComponent>(entity, entity);
registry.emplace<std::string>(entity, entityID);

// FIXME: This code is too specific to the item drop entity
ItemStack *itemStack = tryLoadItemStack(table, registry, entity);
Expand All @@ -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 &registry, entt::entity entity) const {
void LuaEntityLoader::tryLoadCallbacks(const sol::table &table) const {
auto &entityCallbackContainer = Registry::getInstance().addEntityCallbackContainer(m_stringID);

sol::optional<sol::unsafe_function> onCollision = table["on_collision"];
if (onCollision != sol::nullopt) {
auto &luaCallbackComponent = registry.emplace<LuaCallbackComponent>(entity);
luaCallbackComponent.collisionCallback = onCollision.value();
entityCallbackContainer.collisionCallback = onCollision.value();
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/server/lua/loader/LuaEntityLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &registry, entt::entity entity) const;
void tryLoadCallbacks(const sol::table &table) const;
void tryLoadVisual(const sol::table &table, entt::registry &registry, entt::entity entity) const;
void tryLoadRotation(const sol::table &table, entt::registry &registry, entt::entity entity) const;
void tryLoadAnimation(const sol::table &table, entt::registry &registry, entt::entity entity) const;
Expand Down
10 changes: 6 additions & 4 deletions source/server/scene/controller/CollisionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &registry) {
registry.view<PositionComponent, gk::DoubleBox, LuaCallbackComponent>().each([&](auto id, auto &position, auto &box, auto &luaCallbackComponent) {
registry.view<PositionComponent, gk::DoubleBox, std::string>().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);
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions source/server/world/save/WorldSaveBasicBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -177,7 +178,7 @@ void WorldSaveBasicBackend::loadEntities(sf::Packet &save, ServerWorld &world) {
m_entityMap.emplace(entityID, entity);
registry.emplace<NetworkComponent>(entity, entity);

gkDebug() << "Creating entity" << std::underlying_type_t<entt::entity>(entityID);
// gkDebug() << "Creating entity" << std::underlying_type_t<entt::entity>(entityID);

u32 componentCount;
save >> componentCount;
Expand All @@ -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<entt::entity>(entityID);
// gkDebug() << "Loading component" << (u16)type << "for entity" << std::underlying_type_t<entt::entity>(entityID);

if (type == ComponentType::Position) {
save >> registry.emplace<PositionComponent>(entity);
Expand All @@ -206,14 +207,15 @@ void WorldSaveBasicBackend::loadEntities(sf::Packet &save, ServerWorld &world) {
else if (type == ComponentType::Hitbox) {
save >> registry.emplace<gk::DoubleBox>(entity);
}
else if (type == ComponentType::EntityID) {
save >> registry.emplace<std::string>(entity);
}
else
gkWarning() << "Unknown component with type" << (int)type;
}
}
}

#include "ComponentType.hpp"

void WorldSaveBasicBackend::saveEntities(sf::Packet &save, ServerWorld &world) {
entt::registry &registry = world.scene().registry();

Expand All @@ -232,18 +234,18 @@ void WorldSaveBasicBackend::saveEntities(sf::Packet &save, ServerWorld &world) {
Network::Packet packet = type.func("save"_hs).invoke({}, component).template cast<Network::Packet>();
entityPacket.append(packet.getData(), packet.getDataSize());

gkDebug() << "Serializing component" << type.prop("name"_hs).value().template cast<std::string>() << "for entity" << std::underlying_type_t<entt::entity>(entity) << "of size" << packet.getDataSize();
// gkDebug() << "Serializing component" << type.prop("name"_hs).value().template cast<std::string>() << "for entity" << std::underlying_type_t<entt::entity>(entity) << "of size" << packet.getDataSize();

++componentCount;
}
});

gkDebug() << "Saving" << componentCount << "components for entity" << std::underlying_type_t<entt::entity>(entity);
// gkDebug() << "Saving" << componentCount << "components for entity" << std::underlying_type_t<entt::entity>(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();
}

0 comments on commit 15d9aca

Please sign in to comment.