Loading...
Searching...
No Matches
ComponentRegistry.h
Go to the documentation of this file.
1//
2// Created by denzel on 30/11/2025.
3//
4
5#pragma once
6#include <functional>
7#include <typeindex>
8
9#include "Entity.h"
10#include "hellfire/serializers/Serializer.h"
11#include "nlohmann/json.hpp"
12
13namespace hellfire {
14 class Entity;
15 class Component;
16
18 public:
20 using DeserializeFn = std::function<void(Entity&, const nlohmann::json &)>;
21 using HasComponentFn = std::function<bool(const Entity&)>;
22
24 static ComponentRegistry registry;
25 return registry;
26 }
27
28 // Prevent copying
31
32
33 template<ComponentType T>
34 void register_component(const std::string &type_name) {
35 const auto type = std::type_index(typeid(T));
36 type_names_[type] = type_name;
37 name_to_type_.insert_or_assign(type_name, type);
38
39 has_component_[type] = [](const Entity& e) {
40 return e.has_component<T>();
41 };
42
43 // Lambda method for returning the serialization method
44 serializers_[type] = [](const Entity& e) {
45 std::ostringstream stream;
46 Serializer<T>::serialize(stream, e.get_component<T>());
47 nlohmann::json j = nlohmann::json::parse(stream.str());
48 return j;
49 };
50
51 deserializers_[type_name] = [](Entity& e, const nlohmann::json& j) {
52 auto comp = e.add_component<T>();
53 std::istringstream stream(j.dump());
54 Serializer<T>::deserialize(stream, comp);
55 };
56 }
57
59 nlohmann::ordered_json components = nlohmann::ordered_json::array();
60
61 for (const auto& [type, has_fn] : has_component_) {
62 if (has_fn(entity)) {
63 nlohmann::ordered_json comp = serializers_.at(type)(entity);
64 comp["_type"] = type_names_.at(type);
65 components.push_back(comp);
66 }
67 }
68
69 return components;
70 }
71
72 bool deserialize_component(Entity& entity, const nlohmann::json& comp_json) const {
73 if (!comp_json.contains("_type")) return false;
74
75 std::string type_name = comp_json.at("_type");
76 const auto it = deserializers_.find(type_name);
77 if (it == deserializers_.end()) return false;
78
79 it->second(entity, comp_json);
80 return true;
81 }
82
83 void deserialize_all_components(Entity& entity, const nlohmann::json& components) const {
84 for (const auto& comp_json : components) {
85 deserialize_component(entity, comp_json);
86 }
87 }
88
89 private:
90 ComponentRegistry() = default;
91
97 };
98}
bool deserialize_component(Entity &entity, const nlohmann::json &comp_json) const
void register_component(const std::string &type_name)
ComponentRegistry(const ComponentRegistry &)=delete
ComponentRegistry & operator=(const ComponentRegistry &)=delete
std::unordered_map< std::type_index, SerializeFn > serializers_
std::unordered_map< std::string, DeserializeFn > deserializers_
nlohmann::json serialize_all_components(const Entity &entity) const
std::unordered_map< std::type_index, HasComponentFn > has_component_
std::unordered_map< std::string, std::type_index > name_to_type_
std::unordered_map< std::type_index, std::string > type_names_
static ComponentRegistry & instance()
void deserialize_all_components(Entity &entity, const nlohmann::json &components) const
void register_all_components()