@@ -121,25 +121,29 @@ impl SparseSetIndex for RelationshipId {
121
121
}
122
122
}
123
123
124
- pub mod default_relationship_kinds {
124
+ pub mod relationship_kinds {
125
125
pub struct HasComponent ;
126
126
pub struct HasResource ;
127
127
}
128
128
129
129
#[ derive( Debug , Copy , Clone , Hash , Eq , PartialEq ) ]
130
- pub enum ComponentIdOrEntity {
130
+ pub enum EntityOrDummyId {
131
131
Entity ( crate :: entity:: Entity ) ,
132
- ComponentId ( ComponentId ) ,
132
+ /// DummyId is used so that we can fit regular components into the relationship model i.e.
133
+ /// HasComponent Position
134
+ /// however we dont want to implicitly spawn entities for use as the relationship target
135
+ /// so we use a dummy id
136
+ DummyId ( DummyId ) ,
133
137
}
134
138
135
139
#[ derive( Debug , Copy , Clone , Hash , Eq , PartialEq ) ]
136
140
pub struct Relship {
137
141
kind : RelationshipKindId ,
138
- target : ComponentIdOrEntity ,
142
+ target : EntityOrDummyId ,
139
143
}
140
144
141
145
impl Relship {
142
- pub fn new ( kind : RelationshipKindId , target : ComponentIdOrEntity ) -> Self {
146
+ pub fn new ( kind : RelationshipKindId , target : EntityOrDummyId ) -> Self {
143
147
Self { kind, target }
144
148
}
145
149
}
@@ -169,25 +173,26 @@ pub struct RelationshipKindInfo {
169
173
}
170
174
171
175
#[ derive( Debug , Copy , Clone , Hash , Eq , PartialEq ) ]
172
- pub struct ComponentInfo {
176
+ pub struct DummyInfo {
173
177
static_type : Option < TypeId > ,
174
- id : ComponentId ,
178
+ id : DummyId ,
175
179
}
176
180
#[ derive( Debug , Copy , Clone , Hash , Eq , PartialEq ) ]
177
- pub struct ComponentId ( usize ) ;
181
+ pub struct DummyId ( usize ) ;
178
182
179
183
#[ derive( Debug ) ]
180
184
pub struct Relationships {
181
185
relationships : Vec < RelationshipInfo > ,
182
186
relationship_indices : HashMap < Relship , RelationshipId , fxhash:: FxBuildHasher > ,
183
187
184
188
kinds : Vec < RelationshipKindInfo > ,
189
+ kind_indices : HashMap < DummyId , RelationshipKindId , fxhash:: FxBuildHasher > ,
185
190
186
- components : Vec < ComponentInfo > ,
187
- // FIXME(Relationships) we use TypeId here because bevy needs to do this conversion *somewhere*
188
- // scripting stuff can directly use ComponentId/RelationshipKindId somehow
189
- typeid_to_component_id : HashMap < TypeId , ComponentId , fxhash :: FxBuildHasher > ,
190
- kind_indices : HashMap < TypeId , RelationshipKindId , fxhash:: FxBuildHasher > ,
191
+ // These two fields are only use for bevy to map TypeId -> DummyId, scripting/modding
192
+ // support does not need to care about these two fields and should directly use DummyId's
193
+ // and roll their own mapping of ScriptingId -> DummyId if necessary
194
+ dummy_id_to_type_id : Vec < DummyInfo > ,
195
+ type_id_to_dummy_id : HashMap < TypeId , DummyId , fxhash:: FxBuildHasher > ,
191
196
}
192
197
193
198
impl Default for Relationships {
@@ -197,16 +202,19 @@ impl Default for Relationships {
197
202
relationship_indices : Default :: default ( ) ,
198
203
199
204
kinds : Default :: default ( ) ,
200
-
201
- components : Default :: default ( ) ,
202
- typeid_to_component_id : Default :: default ( ) ,
203
205
kind_indices : Default :: default ( ) ,
206
+
207
+ dummy_id_to_type_id : Default :: default ( ) ,
208
+ type_id_to_dummy_id : Default :: default ( ) ,
204
209
} ;
205
210
206
- this. new_relationship_kind ( Some (
207
- TypeId :: of :: < default_relationship_kinds:: HasComponent > ( ) ,
208
- ) ) ;
209
- this. new_relationship_kind ( Some ( TypeId :: of :: < default_relationship_kinds:: HasResource > ( ) ) ) ;
211
+ let has_component_id =
212
+ this. new_dummy_id ( Some ( TypeId :: of :: < relationship_kinds:: HasComponent > ( ) ) ) ;
213
+ this. new_relationship_kind ( has_component_id) ;
214
+
215
+ let has_resource_id =
216
+ this. new_dummy_id ( Some ( TypeId :: of :: < relationship_kinds:: HasResource > ( ) ) ) ;
217
+ this. new_relationship_kind ( has_resource_id) ;
210
218
211
219
this
212
220
}
@@ -220,80 +228,65 @@ pub enum ComponentsError {
220
228
221
229
impl Relationships {
222
230
pub fn relkind_of_has_component ( & self ) -> RelationshipKindId {
223
- self . kind_indices [ & TypeId :: of :: < default_relationship_kinds:: HasComponent > ( ) ]
231
+ let has_component_id =
232
+ self . type_id_to_dummy_id [ & TypeId :: of :: < relationship_kinds:: HasComponent > ( ) ] ;
233
+ self . kind_indices [ & has_component_id]
224
234
}
225
235
226
236
pub fn relkind_of_has_resource ( & self ) -> RelationshipKindId {
227
- self . kind_indices [ & TypeId :: of :: < default_relationship_kinds:: HasResource > ( ) ]
237
+ let has_resource_id =
238
+ self . type_id_to_dummy_id [ & TypeId :: of :: < relationship_kinds:: HasResource > ( ) ] ;
239
+ self . kind_indices [ & has_resource_id]
228
240
}
229
241
230
- /// TypeId is used for bevy to map from relationship kind structs -> RelationshipKindId
231
- /// scripting/untyped use of this should pass in None
232
- pub fn new_relationship_kind ( & mut self , type_id : Option < TypeId > ) -> RelationshipKindId {
242
+ pub fn new_relationship_kind ( & mut self , dummy_id : DummyId ) -> RelationshipKindId {
233
243
let id = RelationshipKindId ( self . kinds . len ( ) ) ;
234
-
235
- if let Some ( type_id) = type_id {
236
- let previously_inserted = self
237
- . kind_indices
238
- . insert ( type_id, RelationshipKindId ( self . kinds . len ( ) ) ) ;
239
- assert ! ( previously_inserted. is_none( ) ) ;
240
- }
241
-
244
+ self . kind_indices . insert ( dummy_id, id) ;
242
245
self . kinds . push ( RelationshipKindInfo { id } ) ;
243
246
id
244
247
}
245
248
246
- /// TypeId is used by bevy to map from component type -> component id
247
- /// scripting/untyped use of this should pass in None
248
- pub ( crate ) fn new_component_id ( & mut self , type_id : Option < TypeId > ) -> ComponentId {
249
- let component_id = ComponentId ( self . components . len ( ) ) ;
250
- self . components . push ( ComponentInfo {
249
+ /// TypeId is used by bevy to register a mapping from typeid -> dummyid
250
+ /// dynamic component use of this should pass in None or else it could
251
+ /// interfere with bevy's use of this `Relationships` struct
252
+ pub ( crate ) fn new_dummy_id ( & mut self , type_id : Option < TypeId > ) -> DummyId {
253
+ let dummy_id = DummyId ( self . dummy_id_to_type_id . len ( ) ) ;
254
+ self . dummy_id_to_type_id . push ( DummyInfo {
251
255
static_type : type_id,
252
- id : component_id ,
256
+ id : dummy_id ,
253
257
} ) ;
254
258
255
259
if let Some ( type_id) = type_id {
256
- let previously_inserted = self . typeid_to_component_id . insert ( type_id, component_id ) ;
260
+ let previously_inserted = self . type_id_to_dummy_id . insert ( type_id, dummy_id ) ;
257
261
assert ! ( previously_inserted. is_none( ) ) ;
258
262
}
259
- component_id
263
+ dummy_id
260
264
}
261
265
262
- pub ( crate ) fn type_id_to_component_id ( & self , type_id : TypeId ) -> Option < ComponentId > {
263
- self . typeid_to_component_id . get ( & type_id) . copied ( )
266
+ pub ( crate ) fn type_id_to_dummy_id ( & self , type_id : TypeId ) -> Option < DummyId > {
267
+ self . type_id_to_dummy_id . get ( & type_id) . copied ( )
264
268
}
265
269
266
270
pub ( crate ) fn register_relationship (
267
271
& mut self ,
268
272
relationship : Relship ,
269
273
comp_descriptor : ComponentDescriptor ,
270
274
) -> Result < & RelationshipInfo , ComponentsError > {
271
- let new_id = RelationshipId ( self . relationships . len ( ) ) ;
275
+ let rel_id = RelationshipId ( self . relationships . len ( ) ) ;
272
276
273
277
if let Entry :: Occupied ( _) = self . relationship_indices . entry ( relationship) {
274
278
return Err ( ComponentsError :: ComponentAlreadyExists ( relationship) ) ;
275
279
}
276
280
277
- if let Some ( type_id) = comp_descriptor. type_id {
278
- if let ComponentIdOrEntity :: ComponentId ( id) = relationship. target {
279
- if let Some ( stored_type_id) = self . components [ id. 0 ] . static_type {
280
- assert ! ( stored_type_id == type_id) ;
281
- }
282
-
283
- let component_id = self . typeid_to_component_id [ & type_id] ;
284
- assert ! ( ComponentIdOrEntity :: ComponentId ( component_id) == relationship. target) ;
285
- }
286
- }
287
-
288
- self . relationship_indices . insert ( relationship, new_id) ;
281
+ self . relationship_indices . insert ( relationship, rel_id) ;
289
282
self . relationships . push ( RelationshipInfo {
290
- id : new_id ,
283
+ id : rel_id ,
291
284
relationship,
292
285
data : comp_descriptor,
293
286
} ) ;
294
287
295
288
// Safety: Just inserted ^^^
296
- unsafe { Ok ( self . get_relationship_info_unchecked ( new_id ) ) }
289
+ unsafe { Ok ( self . get_relationship_info_unchecked ( rel_id ) ) }
297
290
}
298
291
299
292
#[ inline]
@@ -310,7 +303,7 @@ impl Relationships {
310
303
pub fn get_resource_id ( & self , type_id : TypeId ) -> Option < RelationshipId > {
311
304
self . get_relationship_id ( Relship {
312
305
kind : self . relkind_of_has_resource ( ) ,
313
- target : ComponentIdOrEntity :: ComponentId ( self . type_id_to_component_id ( type_id) ?) ,
306
+ target : EntityOrDummyId :: DummyId ( self . type_id_to_dummy_id ( type_id) ?) ,
314
307
} )
315
308
}
316
309
#[ inline]
@@ -330,15 +323,15 @@ impl Relationships {
330
323
type_id : TypeId ,
331
324
data_layout : impl FnOnce ( ) -> TypeInfo ,
332
325
) -> & RelationshipInfo {
333
- let component_id = match self . type_id_to_component_id ( type_id) {
326
+ let component_id = match self . type_id_to_dummy_id ( type_id) {
334
327
Some ( id) => id,
335
- None => self . new_component_id ( Some ( type_id) ) ,
328
+ None => self . new_dummy_id ( Some ( type_id) ) ,
336
329
} ;
337
330
338
331
self . get_relationship_info_or_insert_with (
339
332
Relship {
340
333
kind : self . relkind_of_has_resource ( ) ,
341
- target : ComponentIdOrEntity :: ComponentId ( component_id) ,
334
+ target : EntityOrDummyId :: DummyId ( component_id) ,
342
335
} ,
343
336
data_layout,
344
337
)
@@ -348,7 +341,7 @@ impl Relationships {
348
341
pub fn get_component_id ( & self , type_id : TypeId ) -> Option < RelationshipId > {
349
342
self . get_relationship_id ( Relship {
350
343
kind : self . relkind_of_has_component ( ) ,
351
- target : ComponentIdOrEntity :: ComponentId ( self . type_id_to_component_id ( type_id) ?) ,
344
+ target : EntityOrDummyId :: DummyId ( self . type_id_to_dummy_id ( type_id) ?) ,
352
345
} )
353
346
}
354
347
#[ inline]
@@ -361,15 +354,15 @@ impl Relationships {
361
354
type_id : TypeId ,
362
355
data_layout : impl FnOnce ( ) -> TypeInfo ,
363
356
) -> & RelationshipInfo {
364
- let component_id = match self . type_id_to_component_id ( type_id) {
357
+ let component_id = match self . type_id_to_dummy_id ( type_id) {
365
358
Some ( id) => id,
366
- None => self . new_component_id ( Some ( type_id) ) ,
359
+ None => self . new_dummy_id ( Some ( type_id) ) ,
367
360
} ;
368
361
369
362
self . get_relationship_info_or_insert_with (
370
363
Relship {
371
364
kind : self . relkind_of_has_component ( ) ,
372
- target : ComponentIdOrEntity :: ComponentId ( component_id) ,
365
+ target : EntityOrDummyId :: DummyId ( component_id) ,
373
366
} ,
374
367
data_layout,
375
368
)
0 commit comments