1
+ use bevy_utils:: HashMap ;
2
+ use bevy_utils:: StableHashMap ;
3
+
1
4
use crate :: {
2
5
bundle:: BundleId ,
3
- component:: { ComponentId , StorageType } ,
6
+ component:: { RelationKindId , StorageType } ,
4
7
entity:: { Entity , EntityLocation } ,
5
8
storage:: { Column , SparseArray , SparseSet , SparseSetIndex , TableId } ,
6
9
} ;
7
10
use std:: {
8
11
borrow:: Cow ,
9
- collections:: HashMap ,
10
12
hash:: Hash ,
11
13
ops:: { Index , IndexMut } ,
12
14
} ;
@@ -124,54 +126,73 @@ pub struct Archetype {
124
126
entities : Vec < Entity > ,
125
127
edges : Edges ,
126
128
table_info : TableInfo ,
127
- table_components : Cow < ' static , [ ComponentId ] > ,
128
- sparse_set_components : Cow < ' static , [ ComponentId ] > ,
129
- pub ( crate ) unique_components : SparseSet < ComponentId , Column > ,
130
- pub ( crate ) components : SparseSet < ComponentId , ArchetypeComponentInfo > ,
129
+ table_components : Cow < ' static , [ ( RelationKindId , Option < Entity > ) ] > ,
130
+ sparse_set_components : Cow < ' static , [ ( RelationKindId , Option < Entity > ) ] > ,
131
+ pub ( crate ) unique_components : SparseSet < RelationKindId , Column > ,
132
+ pub ( crate ) components : SparseSet < RelationKindId , ArchetypeComponentInfo > ,
133
+ pub ( crate ) relations : SparseSet < RelationKindId , StableHashMap < Entity , ArchetypeComponentInfo > > ,
131
134
}
132
135
133
136
impl Archetype {
134
137
pub fn new (
135
138
id : ArchetypeId ,
136
139
table_id : TableId ,
137
- table_components : Cow < ' static , [ ComponentId ] > ,
138
- sparse_set_components : Cow < ' static , [ ComponentId ] > ,
140
+ table_components : Cow < ' static , [ ( RelationKindId , Option < Entity > ) ] > ,
141
+ sparse_set_components : Cow < ' static , [ ( RelationKindId , Option < Entity > ) ] > ,
139
142
table_archetype_components : Vec < ArchetypeComponentId > ,
140
143
sparse_set_archetype_components : Vec < ArchetypeComponentId > ,
141
144
) -> Self {
145
+ // FIXME(Relationships) sort out this capacity weirdness
142
146
let mut components =
143
147
SparseSet :: with_capacity ( table_components. len ( ) + sparse_set_components. len ( ) ) ;
144
- for ( component_id, archetype_component_id) in
148
+ let mut relations = SparseSet :: new ( ) ;
149
+ for ( ( kind_id, target) , archetype_component_id) in
145
150
table_components. iter ( ) . zip ( table_archetype_components)
146
151
{
147
- components. insert (
148
- * component_id,
149
- ArchetypeComponentInfo {
150
- storage_type : StorageType :: Table ,
151
- archetype_component_id,
152
- } ,
153
- ) ;
152
+ let arch_comp_info = ArchetypeComponentInfo {
153
+ storage_type : StorageType :: Table ,
154
+ archetype_component_id,
155
+ } ;
156
+
157
+ match target {
158
+ None => {
159
+ components. insert ( * kind_id, arch_comp_info) ;
160
+ }
161
+ Some ( target) => {
162
+ let set = relations. get_or_insert_with ( * kind_id, StableHashMap :: default) ;
163
+ set. insert ( * target, arch_comp_info) ;
164
+ }
165
+ } ;
154
166
}
155
167
156
- for ( component_id , archetype_component_id) in sparse_set_components
168
+ for ( ( kind_id , target ) , archetype_component_id) in sparse_set_components
157
169
. iter ( )
158
170
. zip ( sparse_set_archetype_components)
159
171
{
160
- components. insert (
161
- * component_id,
162
- ArchetypeComponentInfo {
163
- storage_type : StorageType :: SparseSet ,
164
- archetype_component_id,
165
- } ,
166
- ) ;
172
+ let arch_comp_info = ArchetypeComponentInfo {
173
+ storage_type : StorageType :: SparseSet ,
174
+ archetype_component_id,
175
+ } ;
176
+
177
+ match target {
178
+ None => {
179
+ components. insert ( * kind_id, arch_comp_info) ;
180
+ }
181
+ Some ( target) => {
182
+ let set = relations. get_or_insert_with ( * kind_id, StableHashMap :: default) ;
183
+ set. insert ( * target, arch_comp_info) ;
184
+ }
185
+ } ;
167
186
}
187
+
168
188
Self {
169
189
id,
170
190
table_info : TableInfo {
171
191
id : table_id,
172
192
entity_rows : Default :: default ( ) ,
173
193
} ,
174
194
components,
195
+ relations,
175
196
table_components,
176
197
sparse_set_components,
177
198
unique_components : SparseSet :: new ( ) ,
@@ -201,28 +222,38 @@ impl Archetype {
201
222
}
202
223
203
224
#[ inline]
204
- pub fn table_components ( & self ) -> & [ ComponentId ] {
225
+ pub fn table_components ( & self ) -> & [ ( RelationKindId , Option < Entity > ) ] {
205
226
& self . table_components
206
227
}
207
228
208
229
#[ inline]
209
- pub fn sparse_set_components ( & self ) -> & [ ComponentId ] {
230
+ pub fn sparse_set_components ( & self ) -> & [ ( RelationKindId , Option < Entity > ) ] {
210
231
& self . sparse_set_components
211
232
}
212
233
213
234
#[ inline]
214
- pub fn unique_components ( & self ) -> & SparseSet < ComponentId , Column > {
235
+ pub fn unique_components ( & self ) -> & SparseSet < RelationKindId , Column > {
215
236
& self . unique_components
216
237
}
217
238
218
239
#[ inline]
219
- pub fn unique_components_mut ( & mut self ) -> & mut SparseSet < ComponentId , Column > {
240
+ pub fn unique_components_mut ( & mut self ) -> & mut SparseSet < RelationKindId , Column > {
220
241
& mut self . unique_components
221
242
}
222
243
244
+ // FIXME(Relationships) this also yields relations which feels weird but also needed
223
245
#[ inline]
224
- pub fn components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
225
- self . components . indices ( )
246
+ pub fn components ( & self ) -> impl Iterator < Item = ( RelationKindId , Option < Entity > ) > + ' _ {
247
+ self . components
248
+ . indices ( )
249
+ . map ( |kind| ( kind, None ) )
250
+ . chain ( self . relations . indices ( ) . flat_map ( move |kind_id| {
251
+ self . relations
252
+ . get ( kind_id)
253
+ . unwrap ( )
254
+ . keys ( )
255
+ . map ( move |target| ( kind_id, Some ( * target) ) )
256
+ } ) )
226
257
}
227
258
228
259
#[ inline]
@@ -289,25 +320,51 @@ impl Archetype {
289
320
}
290
321
291
322
#[ inline]
292
- pub fn contains ( & self , component_id : ComponentId ) -> bool {
293
- self . components . contains ( component_id)
323
+ pub fn contains ( & self , relation_kind : RelationKindId , relation_target : Option < Entity > ) -> bool {
324
+ match relation_target {
325
+ None => self . components . contains ( relation_kind) ,
326
+ Some ( target) => self
327
+ . relations
328
+ . get ( relation_kind)
329
+ . map ( |set| set. get ( & target) )
330
+ . flatten ( )
331
+ . is_some ( ) ,
332
+ }
294
333
}
295
334
335
+ // FIXME(Relationships) technically the target is unnecessary here as all `KindId` have the same storage type
296
336
#[ inline]
297
- pub fn get_storage_type ( & self , component_id : ComponentId ) -> Option < StorageType > {
298
- self . components
299
- . get ( component_id)
300
- . map ( |info| info. storage_type )
337
+ pub fn get_storage_type (
338
+ & self ,
339
+ relation_kind : RelationKindId ,
340
+ relation_target : Option < Entity > ,
341
+ ) -> Option < StorageType > {
342
+ match relation_target {
343
+ None => self . components . get ( relation_kind) ,
344
+ Some ( target) => self
345
+ . relations
346
+ . get ( relation_kind)
347
+ . map ( |set| set. get ( & target) )
348
+ . flatten ( ) ,
349
+ }
350
+ . map ( |info| info. storage_type )
301
351
}
302
352
303
353
#[ inline]
304
354
pub fn get_archetype_component_id (
305
355
& self ,
306
- component_id : ComponentId ,
356
+ relation_kind : RelationKindId ,
357
+ relation_target : Option < Entity > ,
307
358
) -> Option < ArchetypeComponentId > {
308
- self . components
309
- . get ( component_id)
310
- . map ( |info| info. archetype_component_id )
359
+ match relation_target {
360
+ None => self . components . get ( relation_kind) ,
361
+ Some ( target) => self
362
+ . relations
363
+ . get ( relation_kind)
364
+ . map ( |set| set. get ( & target) )
365
+ . flatten ( ) ,
366
+ }
367
+ . map ( |info| info. archetype_component_id )
311
368
}
312
369
}
313
370
@@ -329,8 +386,8 @@ impl ArchetypeGeneration {
329
386
330
387
#[ derive( Hash , PartialEq , Eq ) ]
331
388
pub struct ArchetypeIdentity {
332
- table_components : Cow < ' static , [ ComponentId ] > ,
333
- sparse_set_components : Cow < ' static , [ ComponentId ] > ,
389
+ table_components : Cow < ' static , [ ( RelationKindId , Option < Entity > ) ] > ,
390
+ sparse_set_components : Cow < ' static , [ ( RelationKindId , Option < Entity > ) ] > ,
334
391
}
335
392
336
393
#[ derive( Debug , Copy , Clone , Eq , PartialEq , Hash ) ]
@@ -453,6 +510,10 @@ impl Archetypes {
453
510
a : ArchetypeId ,
454
511
b : ArchetypeId ,
455
512
) -> ( & mut Archetype , & mut Archetype ) {
513
+ if a. 0 == b. 0 {
514
+ panic ! ( "both indexes were the same" ) ;
515
+ }
516
+
456
517
if a. index ( ) > b. index ( ) {
457
518
let ( b_slice, a_slice) = self . archetypes . split_at_mut ( a. index ( ) ) ;
458
519
( & mut a_slice[ 0 ] , & mut b_slice[ b. index ( ) ] )
@@ -475,8 +536,8 @@ impl Archetypes {
475
536
pub ( crate ) fn get_id_or_insert (
476
537
& mut self ,
477
538
table_id : TableId ,
478
- table_components : Vec < ComponentId > ,
479
- sparse_set_components : Vec < ComponentId > ,
539
+ table_components : Vec < ( RelationKindId , Option < Entity > ) > ,
540
+ sparse_set_components : Vec < ( RelationKindId , Option < Entity > ) > ,
480
541
) -> ArchetypeId {
481
542
let table_components = Cow :: from ( table_components) ;
482
543
let sparse_set_components = Cow :: from ( sparse_set_components) ;
0 commit comments