99 assembly.AddInternalCall(" RealEngine.InternalCalls" , #functionName, reinterpret_cast <void *>(&functionName));
1010
1111namespace RealEngine {
12- static std::unordered_map<Coral::Type*, std::function<bool (Entity)>> s_EntityHasComponentFuncs;
12+ struct ComponentFuncs {
13+ std::function<bool (Entity)> HasComponent;
14+ std::function<void *(Entity)> GetComponent;
15+ };
16+ static std::unordered_map<Coral::Type*, ComponentFuncs> s_EntityComponentFuncs;
1317
1418 enum class LogLevel {
1519 Trace = 0 ,
@@ -40,80 +44,20 @@ namespace RealEngine {
4044 RE_CORE_ASSERT (entity);
4145
4246 Coral::Type& componentType = componentReflectionType;
43- RE_CORE_ASSERT (s_EntityHasComponentFuncs.find (&componentType) != s_EntityHasComponentFuncs.end ());
44- return s_EntityHasComponentFuncs.at (&componentType)(entity);
45- }
46-
47- static void TransformComponent_GetTranslation (UUID entityID, glm::vec3* outTranslation) {
48- Ref<Scene> scene = Project::GetCurrentScene ();
49- RE_CORE_ASSERT (scene);
50- Entity entity = scene->GetEntity (entityID);
51- RE_CORE_ASSERT (entity);
52-
53- *outTranslation = entity ? entity.GetComponent <TransformComponent>().GetTransation () : glm::vec3 (0 .0f );
54- }
55-
56- static void TransformComponent_SetTranslation (UUID entityID, glm::vec3* translation) {
57- Ref<Scene> scene = Project::GetCurrentScene ();
58- RE_CORE_ASSERT (scene);
59- Entity entity = scene->GetEntity (entityID);
60- RE_CORE_ASSERT (entity);
61-
62- entity.GetComponent <TransformComponent>().SetTransation (*translation);
63- }
64-
65- static void TransformComponent_GetRotation (UUID entityID, glm::quat* outRotation) {
66- Ref<Scene> scene = Project::GetCurrentScene ();
67- RE_CORE_ASSERT (scene);
68- Entity entity = scene->GetEntity (entityID);
69- RE_CORE_ASSERT (entity);
70-
71- *outRotation = entity ? entity.GetComponent <TransformComponent>().GetRotationQuat () : glm::quat ();
72- }
73-
74- static void TransformComponent_SetRotation (UUID entityID, glm::quat* rotation) {
75- Ref<Scene> scene = Project::GetCurrentScene ();
76- RE_CORE_ASSERT (scene);
77- Entity entity = scene->GetEntity (entityID);
78- RE_CORE_ASSERT (entity);
79-
80- entity.GetComponent <TransformComponent>().SetRotationQuat (*rotation);
81- }
82-
83- static void TransformComponent_GetScale (UUID entityID, glm::vec3* outScale) {
84- Ref<Scene> scene = Project::GetCurrentScene ();
85- RE_CORE_ASSERT (scene);
86- Entity entity = scene->GetEntity (entityID);
87- RE_CORE_ASSERT (entity);
88-
89- *outScale = entity ? entity.GetComponent <TransformComponent>().GetScale () : glm::vec3 (0 .0f );
47+ RE_CORE_ASSERT (s_EntityComponentFuncs.find (&componentType) != s_EntityComponentFuncs.end ());
48+ return s_EntityComponentFuncs.at (&componentType).HasComponent (entity);
9049 }
9150
92- static void TransformComponent_SetScale (UUID entityID, glm::vec3* scale) {
93- Ref<Scene> scene = Project::GetCurrentScene ();
94- RE_CORE_ASSERT (scene);
95- Entity entity = scene->GetEntity (entityID);
96- RE_CORE_ASSERT (entity);
97-
98- entity.GetComponent <TransformComponent>().SetScale (*scale);
99- }
100-
101- static void SpriteRendererComponent_GetColor (UUID entityID, glm::vec4* outColor) {
102- Ref<Scene> scene = Project::GetCurrentScene ();
103- RE_CORE_ASSERT (scene);
104- Entity entity = scene->GetEntity (entityID);
105- RE_CORE_ASSERT (entity);
106-
107- *outColor = entity ? entity.GetComponent <SpriteRendererComponent>().Color : glm::vec4 (0 .0f );
108- }
109-
110- static void SpriteRendererComponent_SetColor (UUID entityID, glm::vec4* color) {
51+ static void * Entity_GetComponent (UUID entityID, Coral::ReflectionType componentReflectionType) {
52+ RE_PROFILE_FUNCTION ();
11153 Ref<Scene> scene = Project::GetCurrentScene ();
11254 RE_CORE_ASSERT (scene);
11355 Entity entity = scene->GetEntity (entityID);
11456 RE_CORE_ASSERT (entity);
11557
116- entity.GetComponent <SpriteRendererComponent>().Color = *color;
58+ Coral::Type& componentType = componentReflectionType;
59+ RE_CORE_ASSERT (s_EntityComponentFuncs.find (&componentType) != s_EntityComponentFuncs.end ());
60+ return s_EntityComponentFuncs.at (&componentType).GetComponent (entity);
11761 }
11862
11963 void ScriptGlue::RegisterFunctions (Coral::ManagedAssembly& assembly) {
@@ -125,20 +69,7 @@ namespace RealEngine {
12569 }
12670
12771 RE_ADD_INTERNAL_CALL (Entity_HasComponent);
128-
129- // TransformComponent
130- RE_ADD_INTERNAL_CALL (TransformComponent_GetTranslation);
131- RE_ADD_INTERNAL_CALL (TransformComponent_SetTranslation);
132- RE_ADD_INTERNAL_CALL (TransformComponent_GetRotation);
133- RE_ADD_INTERNAL_CALL (TransformComponent_SetRotation);
134- RE_ADD_INTERNAL_CALL (TransformComponent_GetScale);
135- RE_ADD_INTERNAL_CALL (TransformComponent_SetScale);
136- // TransformComponent
137-
138- // SpriteRendererComponent
139- RE_ADD_INTERNAL_CALL (SpriteRendererComponent_GetColor);
140- RE_ADD_INTERNAL_CALL (SpriteRendererComponent_SetColor);
141- // SpriteRendererComponent
72+ RE_ADD_INTERNAL_CALL (Entity_GetComponent);
14273
14374 RE_ADD_INTERNAL_CALL (NativeLog);
14475
@@ -154,8 +85,10 @@ namespace RealEngine {
15485
15586 Coral::Type& componentType = assembly.GetType (componentTypeName);
15687 RE_CORE_ASSERT (componentType);
157- s_EntityHasComponentFuncs[&componentType] = [](Entity entity) { return entity.HasComponent <Component>(); };
158- }(), ...);
88+ auto & funcs = s_EntityComponentFuncs[&componentType];
89+ funcs.HasComponent = [](Entity entity) { return entity.HasComponent <Component>(); };
90+ funcs.GetComponent = [](Entity entity) { return entity.TryGetComponent <Component>(); };
91+ }(), ...);
15992 }
16093
16194 template <typename ... Component>
@@ -168,7 +101,7 @@ namespace RealEngine {
168101 void ScriptGlue::RegisterComponents (Coral::ManagedAssembly& assembly) {
169102 RE_PROFILE_FUNCTION ();
170103
171- s_EntityHasComponentFuncs .clear ();
104+ s_EntityComponentFuncs .clear ();
172105 RegisterComponent (assembly, ComponentList::GetAllComponents ());
173106 }
174107}
0 commit comments