18#define REGISTER_COMPONENT_TYPE(type) \
19 namespace type##RegisterComponent \
22 bool type##Registered = BodyComponentDB::AddComponentRegistrar(&Register); \
24 void type##RegisterComponent::Register()
109 m_components.erase(body);
115 T *
get(
const Body *body) {
return &m_components.at(body); }
118 template <
typename U>
120 template <
typename U>
123 std::map<const Body *, T> m_components;
132 template <
typename T>
141 pool->get(body)->SaveToJson(obj, space);
146 auto *component =
pool->newComponent(body);
147 component->LoadFromJson(obj, space);
152 template <
typename T>
156 if (iter == m_componentPools.end()) {
160 return static_cast<Pool<T> *
>(iter->second.get());
167 assert(componentIndex < m_componentTypes.size());
168 return m_componentTypes[componentIndex];
175 auto iter = m_componentNames.find(typeName);
176 if (iter != m_componentNames.end())
184 template <
typename T>
188 assert(m_componentIdx < 64);
189 assert(m_componentPools.find(
TypeId<T>::Get()) == m_componentPools.end());
192 pool->typeName = typeName;
194 m_componentNames.emplace(typeName, pool);
195 m_componentTypes.push_back(pool);
199 template <
typename T>
202 Pool<T> *pool = GetComponentType<T>();
214 template <
typename T>
217 Pool<T> *pool = GetComponentType<T>();
218 if (typeName.empty())
219 typeName = pool->typeName;
221 assert(!m_componentSerializers.count(typeName));
223 SerializerBase *serial =
new Serializer<T>(pool);
224 pool->serializer = serial;
226 m_componentSerializers.emplace(typeName, serial);
227 m_componentNames.emplace(typeName, pool);
236 auto iter = m_componentSerializers.find(typeName);
237 if (iter != m_componentSerializers.end())
238 return iter->second.get();
244 static std::map<size_t, std::unique_ptr<PoolBase>> m_componentPools;
245 static std::map<std::string, std::unique_ptr<SerializerBase>> m_componentSerializers;
246 static std::map<std::string, PoolBase *> m_componentNames;
247 static std::vector<PoolBase *> m_componentTypes;
248 static size_t m_componentIdx;
nlohmann::json Json
Definition Json.h:8
Definition BodyComponent.h:32
static PoolBase * GetComponentType(size_t componentIndex)
Definition BodyComponent.h:165
static void Uninit()
Definition BodyComponent.cpp:23
static void Init()
Definition BodyComponent.cpp:16
static bool RegisterSerializer(std::string typeName={})
Definition BodyComponent.h:215
static SerializerBase * GetSerializer(const std::string &typeName)
Definition BodyComponent.h:234
static bool AddComponentRegistrar(void(*registrar)())
Definition BodyComponent.cpp:33
static Pool< T > * GetComponentType()
Definition BodyComponent.h:153
static void RegisterComponent(std::string typeName)
Definition BodyComponent.h:185
static bool RegisterLuaInterface()
Definition BodyComponent.h:200
static PoolBase * GetComponentType(const std::string &typeName)
Definition BodyComponent.h:173
Definition BodyComponent.h:54
virtual void PushToLua(const Body *body)=0
virtual ~LuaInterfaceBase()
Definition BodyComponent.h:56
LuaInterfaceBase()
Definition BodyComponent.h:55
virtual void DeregisterComponent(const Body *body)=0
Definition LuaBodyComponent.h:18
Definition BodyComponent.h:64
LuaInterfaceBase * luaInterface
Definition BodyComponent.h:79
std::string typeName
Definition BodyComponent.h:82
size_t componentType
Definition BodyComponent.h:71
virtual void deleteComponent(Body *body)=0
virtual ~PoolBase()
Definition BodyComponent.cpp:39
SerializerBase * serializer
Definition BodyComponent.h:75
PoolBase(size_t index, size_t type)
Definition BodyComponent.h:65
size_t componentIndex
Definition BodyComponent.h:70
Definition BodyComponent.h:100
virtual void deleteComponent(Body *body) override
Definition BodyComponent.h:104
T * newComponent(const Body *body)
Definition BodyComponent.h:113
T * get(const Body *body)
Definition BodyComponent.h:115
Definition BodyComponent.h:43
SerializerBase()
Definition BodyComponent.h:44
virtual void fromJson(Body *body, const Json &obj, Space *space)=0
virtual void toJson(const Body *body, Json &obj, Space *space)=0
virtual ~SerializerBase()
Definition BodyComponent.h:45
Definition BodyComponent.h:133
virtual void toJson(const Body *body, Json &obj, Space *space) override
Definition BodyComponent.h:139
Pool< T > * pool
Definition BodyComponent.h:137
Serializer(Pool< T > *pool)
Definition BodyComponent.h:134
virtual void fromJson(Body *body, const Json &obj, Space *space) override
Definition BodyComponent.h:144