MQEngine
载入中...
搜索中...
未找到
ComponentReflection.h
浏览该文件的文档.
1#ifndef COMPONENTREFLECTION_H
2#define COMPONENTREFLECTION_H
3
4#include "../EnginePCH.h"
5#include "../Thirdparty/thirdparty.h"
6#include "../data/Camera.h"
7#include "../data/Component.h"
8#include "../data/NameTag.h"
9#include <boost/describe.hpp>
10#include <boost/mp11.hpp>
11#include <string>
12#include <variant>
13#include <unordered_map>
14#include <functional>
15
16namespace MQEngine {
17
18 using ComponentValue = std::variant<
19 bool, int, float, double, std::string,
20 FCT::Vec3
21 >;
22
24 std::string name;
25 std::string type;
26 std::function<ComponentValue(void*)> getter;
27 std::function<void(void*, const ComponentValue&)> setter;
28 };
29
31 std::string name;
32 std::vector<ComponentFieldInfo> fields;
33 std::function<bool(entt::registry&, entt::entity)> hasComponent;
34 std::function<void*(entt::registry&, entt::entity)> getComponent;
35 std::function<void(entt::registry&, entt::entity)> addComponent;
36 std::function<void(entt::registry&, entt::entity)> removeComponent;
37 };
38
40 public:
43
44 // 获取所有注册的组件信息
45 const std::unordered_map<std::string, ComponentInfo>& getComponentInfos() const;
46
47 // 检查实体是否有指定组件
48 bool hasComponent(entt::registry& registry, entt::entity entity, const std::string& componentName) const;
49
50 // 获取组件字段值
51 ComponentValue getComponentField(entt::registry& registry, entt::entity entity,
52 const std::string& componentName, const std::string& fieldName) const;
53
54 // 设置组件字段值
55 void setComponentField(entt::registry& registry, entt::entity entity,
56 const std::string& componentName, const std::string& fieldName,
57 const ComponentValue& value) const;
58
59 // 添加组件到实体
60 void addComponent(entt::registry& registry, entt::entity entity, const std::string& componentName) const;
61
62 // 从实体移除组件
63 void removeComponent(entt::registry& registry, entt::entity entity, const std::string& componentName) const;
64
65 // 获取组件字段名
66 std::vector<std::string> getComponentFieldNames(const std::string& componentName) const;
67
68 // 获取组件字段类型
69 std::string getComponentFieldType(const std::string& componentName, const std::string& fieldName) const;
70
71 // 获取所有注册的组件名
72 std::vector<std::string> getRegisteredComponentNames() const;
73
74 private:
75 std::unordered_map<std::string, ComponentInfo> m_componentInfos;
76
77 // 注册组件的模板函数
78 template<typename T>
79 void registerComponent(const std::string& name);
80 };
81
82} // namespace MQEngine
83
84#endif // COMPONENTREFLECTION_H
#define ENGINE_API
定义 EnginePCH.h:14
std::vector< std::string > getComponentFieldNames(const std::string &componentName) const
定义 ComponentReflection.cpp:99
std::vector< std::string > getRegisteredComponentNames() const
定义 ComponentReflection.cpp:126
ComponentValue getComponentField(entt::registry &registry, entt::entity entity, const std::string &componentName, const std::string &fieldName) const
定义 ComponentReflection.cpp:31
void registerComponent(const std::string &name)
定义 ComponentReflection.cpp:135
const std::unordered_map< std::string, ComponentInfo > & getComponentInfos() const
定义 ComponentReflection.cpp:19
void addComponent(entt::registry &registry, entt::entity entity, const std::string &componentName) const
定义 ComponentReflection.cpp:83
std::unordered_map< std::string, ComponentInfo > m_componentInfos
定义 ComponentReflection.h:75
bool hasComponent(entt::registry &registry, entt::entity entity, const std::string &componentName) const
定义 ComponentReflection.cpp:23
std::string getComponentFieldType(const std::string &componentName, const std::string &fieldName) const
定义 ComponentReflection.cpp:111
ComponentReflection()
定义 ComponentReflection.cpp:7
void removeComponent(entt::registry &registry, entt::entity entity, const std::string &componentName) const
定义 ComponentReflection.cpp:91
void setComponentField(entt::registry &registry, entt::entity entity, const std::string &componentName, const std::string &fieldName, const ComponentValue &value) const
定义 ComponentReflection.cpp:56
定义 application.h:5
std::variant< bool, int, float, double, std::string, FCT::Vec3 > ComponentValue
定义 ComponentReflection.h:18
定义 ComponentReflection.h:23
std::string type
定义 ComponentReflection.h:25
std::string name
定义 ComponentReflection.h:24
std::function< ComponentValue(void *)> getter
定义 ComponentReflection.h:26
std::function< void(void *, const ComponentValue &)> setter
定义 ComponentReflection.h:27
定义 ComponentReflection.h:30
std::string name
定义 ComponentReflection.h:31
std::function< void(entt::registry &, entt::entity)> removeComponent
定义 ComponentReflection.h:36
std::vector< ComponentFieldInfo > fields
定义 ComponentReflection.h:32
std::function< void(entt::registry &, entt::entity)> addComponent
定义 ComponentReflection.h:35
std::function< void *(entt::registry &, entt::entity)> getComponent
定义 ComponentReflection.h:34
std::function< bool(entt::registry &, entt::entity)> hasComponent
定义 ComponentReflection.h:33