12
12
#include " ecsact/cpp/type_info.hh"
13
13
#include " ecsact/entt/system_view.hh"
14
14
#include " ecsact/entt/event_markers.hh"
15
+ #include " ecsact/entt/entity.hh"
15
16
#include " ecsact/entt/detail/internal_markers.hh"
16
17
17
18
#include " meta_util.hh"
18
19
19
20
namespace ecsact_entt_rt {
20
- using entity_id_map_t = std::unordered_map<ecsact_entity_id, entt::entity>;
21
21
22
22
template <typename Package>
23
23
struct registry_info {
24
24
using package = Package;
25
25
26
26
std::optional<std::reference_wrapper<std::mutex>> mutex;
27
27
::entt::registry registry;
28
- entity_id_map_t entities_map;
29
-
30
- /* *
31
- * Index of this vector is a statically casted EnTT ID
32
- */
33
- std::vector<ecsact_entity_id> _ecsact_entity_ids;
34
-
35
- ecsact_entity_id last_entity_id{};
36
-
37
- struct create_new_entity_result {
38
- entt::entity entt_entity_id;
39
- ecsact_entity_id ecsact_entity_id;
40
- };
41
28
42
29
void init_registry () {
43
30
using ecsact::entt::component_added;
@@ -64,18 +51,19 @@ struct registry_info {
64
51
using boost::mp11::mp_with_index;
65
52
using ecsact::entt::detail::association;
66
53
67
- auto entity_field = field.template get <ecsact_entity_id>(&component);
68
- auto entity_field_entt = entities_map.at (entity_field);
54
+ ecsact::entt::entity_id entity_field =
55
+ field.template get <ecsact_entity_id>(&component);
56
+
69
57
// TODO(zaucy): Increasing the mp_with_index number causes really long
70
58
// compile times. Iterating over the available associations
71
59
// would perform better here.
72
60
assert (field.offset < 32 );
73
61
mp_with_index<32 >(field.offset , [&](auto I) {
74
- if (registry.all_of <association<C, I>>(entity_field_entt )) {
75
- auto & assoc_comp = registry.get <association<C, I>>(entity_field_entt );
62
+ if (registry.all_of <association<C, I>>(entity_field )) {
63
+ auto & assoc_comp = registry.get <association<C, I>>(entity_field );
76
64
assoc_comp.ref_count += 1 ;
77
65
} else {
78
- registry.emplace <association<C, I>>(entity_field_entt , 1 );
66
+ registry.emplace <association<C, I>>(entity_field , 1 );
79
67
}
80
68
});
81
69
}
@@ -88,30 +76,31 @@ struct registry_info {
88
76
using boost::mp11::mp_with_index;
89
77
using ecsact::entt::detail::association;
90
78
91
- auto entity_field = field.template get <ecsact_entity_id>(&component);
92
- auto entity_field_entt = entities_map.at (entity_field);
79
+ ecsact::entt::entity_id entity_field =
80
+ field.template get <ecsact_entity_id>(&component);
81
+
93
82
assert (field.offset < 32 );
94
83
// TODO(zaucy): Increasing the mp_with_index number causes really long
95
84
// compile times. Iterating over the available associations
96
85
// would perform better here.
97
86
mp_with_index<32 >(field.offset , [&](auto I) {
98
- auto & assoc_comp = registry.get <association<C, I>>(entity_field_entt );
87
+ auto & assoc_comp = registry.get <association<C, I>>(entity_field );
99
88
assoc_comp.ref_count -= 1 ;
100
89
if (assoc_comp.ref_count == 0 ) {
101
- registry.erase <association<C, I>>(entity_field_entt );
90
+ registry.erase <association<C, I>>(entity_field );
102
91
}
103
92
});
104
93
}
105
94
106
95
template <typename C>
107
96
requires (std::is_empty_v<C>)
108
- void add_component (::entt::entity entity) {
97
+ void add_component (ecsact ::entt::entity_id entity) {
109
98
registry.emplace <C>(entity);
110
99
}
111
100
112
101
template <typename C, typename ... Args>
113
102
requires (!std::is_empty_v<C>)
114
- void add_component (::entt::entity entity, Args&&... args) {
103
+ void add_component (ecsact ::entt::entity_id entity, Args&&... args) {
115
104
using boost::mp11::mp_with_index;
116
105
using ecsact::entt::detail::mp_for_each_available_component;
117
106
@@ -142,7 +131,7 @@ struct registry_info {
142
131
}
143
132
144
133
template <typename C>
145
- void remove_component (::entt::entity entity) {
134
+ void remove_component (ecsact ::entt::entity_id entity) {
146
135
using ecsact::entt::detail::mp_for_each_available_component;
147
136
148
137
constexpr auto fields_info = ecsact::fields_info<C>();
@@ -168,32 +157,27 @@ struct registry_info {
168
157
}
169
158
170
159
/* * @internal */
171
- inline auto _create_entity (ecsact_entity_id ecsact_entity_id) {
172
- auto new_entt_entity_id = registry.create ();
173
- entities_map[ecsact_entity_id] = new_entt_entity_id;
174
- _ecsact_entity_ids.resize (static_cast <size_t >(new_entt_entity_id) + 1 );
175
- _ecsact_entity_ids[_ecsact_entity_ids.size () - 1 ] = ecsact_entity_id;
176
- return new_entt_entity_id;
160
+ inline auto _create_entity ( //
161
+ ecsact::entt::entity_id entity
162
+ ) -> ecsact::entt::entity_id {
163
+ if (registry.valid (entity)) {
164
+ return entity;
165
+ }
166
+
167
+ auto new_entity = registry.create (entity.as_entt ());
168
+ // Our valid check above should have allowed this to happen
169
+ assert (new_entity == entity.as_entt ());
170
+ return new_entity;
177
171
}
178
172
179
173
/* * @internal */
180
- inline create_new_entity_result _create_entity () {
181
- auto new_entity_id =
182
- static_cast <ecsact_entity_id>(static_cast <int >(last_entity_id) + 1 );
183
- while (entities_map.contains (new_entity_id)) {
184
- new_entity_id =
185
- static_cast <ecsact_entity_id>(static_cast <int >(new_entity_id) + 1 );
186
- }
187
- last_entity_id = new_entity_id;
188
- return {
189
- .entt_entity_id = _create_entity (new_entity_id),
190
- .ecsact_entity_id = new_entity_id,
191
- };
174
+ inline auto _create_entity () -> ecsact::entt::entity_id {
175
+ return registry.create ();
192
176
}
193
177
194
- // Creates an entity and also makes sure there is a matching one in the
195
- // pending registry
196
- inline auto create_entity (ecsact_entity_id ecsact_entity_id) {
178
+ inline auto create_entity ( //
179
+ ecsact::entt::entity_id ecsact_entity_id
180
+ ) -> ecsact::entt::entity_id {
197
181
std::scoped_lock lk (mutex->get ());
198
182
return _create_entity (ecsact_entity_id);
199
183
}
@@ -203,19 +187,8 @@ struct registry_info {
203
187
return _create_entity ();
204
188
}
205
189
206
- inline void destroy_entity (ecsact_entity_id entity_id) {
207
- auto entt_entity_id = entities_map.at (entity_id);
208
-
209
- registry.destroy (entt_entity_id);
210
- entities_map.erase (entity_id);
211
- }
212
-
213
- entt::entity get_entt_entity_id (ecsact_entity_id ecsact_entity_id) const {
214
- return entities_map.at (ecsact_entity_id);
215
- }
216
-
217
- ecsact_entity_id get_ecsact_entity_id (entt::entity entt_entity_id) const {
218
- return _ecsact_entity_ids.at (static_cast <size_t >(entt_entity_id));
190
+ inline void destroy_entity (ecsact::entt::entity_id entity_id) {
191
+ registry.destroy (entity_id);
219
192
}
220
193
};
221
194
} // namespace ecsact_entt_rt
0 commit comments