Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
ComponentFunctionsDefinitions.hpp
1#pragma once
2
3#include <entt/entt.hpp>
4#include <EngineCore/WorldContext/WorldContextSet.hpp>
5
6#include "ComponentFunctions.hpp"
7
8namespace Grindstone::ECS {
9 template<typename T>
10 concept HasConstruct =
11 requires {
12 &T::Construct;
13 }&&
14 std::same_as<
15 decltype(&T::Construct),
16 void (*)(Grindstone::WorldContextSet&, entt::entity)
17 >;
18
19 template<typename T>
20 concept HasDestroy =
21 requires {
22 &T::Destroy;
23 }&&
24 std::same_as<
25 decltype(&T::Destroy),
26 void (*)(Grindstone::WorldContextSet&, entt::entity)
27 >;
28
29 template<typename ComponentType>
30 void* CreateComponent(entt::registry& registry, entt::entity entity) {
31 return &registry.emplace<ComponentType>(entity);
32 }
33
34 template<typename ComponentType>
35 void RemoveComponent(entt::registry& registry, entt::entity entity) {
36 if (registry.any_of<ComponentType>(entity)) {
37 registry.remove<ComponentType>(entity);
38 }
39 }
40
41 template<typename ComponentType>
42 void ClearComponents(WorldContextSet& cxtSet) {
43 entt::registry& registry = cxtSet.GetEntityRegistry();
44 registry.clear<ComponentType>();
45 }
46
47 template<typename ComponentType>
48 Grindstone::Reflection::TypeDescriptor_Struct GetComponentReflectionData() {
49 return ComponentType::reflectionInfo;
50 }
51
52 template<typename ComponentType>
53 bool TryGetComponent(entt::registry& registry, entt::entity entity, void*& outComponent) {
54 ComponentType* foundComp = registry.try_get<ComponentType>(entity);
55 outComponent = foundComp;
56
57 return foundComp != nullptr;
58 }
59
60 template<typename ComponentType>
61 bool HasComponent(entt::registry& registry, entt::entity entity) {
62 return registry.all_of<ComponentType>(entity);
63 }
64
65 template<typename T, typename = void>
66 struct has_clone : std::false_type {};
67
68 template<typename T>
69 struct has_clone<T, std::void_t<decltype(
70 std::declval<const T&>().Clone(
71 std::declval<Grindstone::WorldContextSet&>(),
72 std::declval<entt::entity>()
73 )
74 )>> : std::true_type {};
75
76 template<typename T>
77 constexpr bool has_clone_v = has_clone<T>::value;
78
79 template<typename ComponentType>
80 void CopyRegistryComponents(WorldContextSet& dst, WorldContextSet& src) {
81 entt::registry& srcRegistry = src.GetEntityRegistry();
82 entt::registry& dstRegistry = dst.GetEntityRegistry();
83
84 auto srcEntities = srcRegistry.view<ComponentType>();
85 if constexpr (has_clone_v<ComponentType>) {
86 for (entt::entity entityID : srcEntities) {
87 ComponentType& srcComponent = srcRegistry.get<ComponentType>(entityID);
88 dstRegistry.emplace_or_replace<ComponentType>(entityID, srcComponent.Clone(dst, entityID));
89 }
90 }
91 else {
92 for (entt::entity entityID : srcEntities) {
93 dstRegistry.emplace_or_replace<ComponentType>(entityID, srcRegistry.get<ComponentType>(entityID));
94 }
95 }
96 }
97}
Definition WorldContextSet.hpp:11
Definition ComponentFunctionsDefinitions.hpp:10
Definition ComponentFunctionsDefinitions.hpp:20
Definition ComponentFunctionsDefinitions.hpp:66