3#include <entt/entt.hpp>
4#include <EngineCore/WorldContext/WorldContextSet.hpp>
6#include "ComponentFunctions.hpp"
8namespace Grindstone::ECS {
15 decltype(&T::Construct),
25 decltype(&T::Destroy),
29 template<
typename ComponentType>
30 void* CreateComponent(entt::registry& registry, entt::entity entity) {
31 return ®istry.emplace<ComponentType>(entity);
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);
41 template<
typename ComponentType>
42 void ClearComponents(WorldContextSet& cxtSet) {
43 entt::registry& registry = cxtSet.GetEntityRegistry();
44 registry.clear<ComponentType>();
47 template<
typename ComponentType>
48 Grindstone::Reflection::TypeDescriptor_Struct GetComponentReflectionData() {
49 return ComponentType::reflectionInfo;
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;
57 return foundComp !=
nullptr;
60 template<
typename ComponentType>
61 bool HasComponent(entt::registry& registry, entt::entity entity) {
62 return registry.all_of<ComponentType>(entity);
65 template<
typename T,
typename =
void>
70 std::declval<const T&>().Clone(
71 std::declval<Grindstone::WorldContextSet&>(),
72 std::declval<entt::entity>()
74 )>> : std::true_type {};
79 template<
typename ComponentType>
81 entt::registry& srcRegistry = src.GetEntityRegistry();
82 entt::registry& dstRegistry = dst.GetEntityRegistry();
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));
92 for (entt::entity entityID : srcEntities) {
93 dstRegistry.emplace_or_replace<ComponentType>(entityID, srcRegistry.get<ComponentType>(entityID));
Definition WorldContextSet.hpp:11
Definition ComponentFunctionsDefinitions.hpp:10
Definition ComponentFunctionsDefinitions.hpp:20
Definition ComponentFunctionsDefinitions.hpp:66