@@ -390,7 +390,7 @@ pub struct Archetype {
390390}
391391
392392impl Archetype {
393- /// `table_components` and `sparse_set_components` must be sorted
393+ /// `table_components` must be sorted
394394 pub ( crate ) fn new (
395395 components : & Components ,
396396 component_index : & mut ComponentIndex ,
@@ -399,6 +399,7 @@ impl Archetype {
399399 table_id : TableId ,
400400 table_components : impl Iterator < Item = ComponentId > ,
401401 sparse_set_components : impl Iterator < Item = ComponentId > ,
402+ resource_components : impl Iterator < Item = ComponentId > ,
402403 ) -> Self {
403404 let ( min_table, _) = table_components. size_hint ( ) ;
404405 let ( min_sparse, _) = sparse_set_components. size_hint ( ) ;
@@ -424,22 +425,23 @@ impl Archetype {
424425 . insert ( id, ArchetypeRecord { column : Some ( idx) } ) ;
425426 }
426427
427- for component_id in sparse_set_components {
428+ for component_id in sparse_set_components. chain ( resource_components ) {
428429 // SAFETY: We are creating an archetype that includes this component so it must exist
429430 let info = unsafe { components. get_info_unchecked ( component_id) } ;
430431 info. update_archetype_flags ( & mut flags) ;
431432 observers. update_archetype_flags ( component_id, & mut flags) ;
432433 archetype_components. insert (
433434 component_id,
434435 ArchetypeComponentInfo {
435- storage_type : StorageType :: SparseSet ,
436+ storage_type : info . storage_type ( ) ,
436437 } ,
437438 ) ;
438439 component_index
439440 . entry ( component_id)
440441 . or_default ( )
441442 . insert ( id, ArchetypeRecord { column : None } ) ;
442443 }
444+
443445 Self {
444446 id,
445447 table_id,
@@ -523,6 +525,19 @@ impl Archetype {
523525 . map ( |( id, _) | * id)
524526 }
525527
528+ /// Gets an iterator of all of the components stored in [`ResourceStorages`].
529+ ///
530+ /// All of the IDs are unique.
531+ ///
532+ /// [`ResourceStorages`]: crate::storage::ResourceStorages
533+ #[ inline]
534+ pub fn resource_components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
535+ self . components
536+ . iter ( )
537+ . filter ( |( _, component) | component. storage_type == StorageType :: Resource )
538+ . map ( |( id, _) | * id)
539+ }
540+
526541 /// Returns a slice of all of the components in the archetype.
527542 ///
528543 /// All of the IDs are unique.
@@ -754,10 +769,13 @@ impl ArchetypeGeneration {
754769 }
755770}
756771
772+ /// Components must be sorted
773+ /// (which allows `ArchetypeComponents` to be used as an archetype's identity).
757774#[ derive( Hash , PartialEq , Eq ) ]
758775struct ArchetypeComponents {
759776 table_components : Box < [ ComponentId ] > ,
760777 sparse_set_components : Box < [ ComponentId ] > ,
778+ resource_components : Box < [ ComponentId ] > ,
761779}
762780
763781/// Maps a [`ComponentId`] to the list of [`Archetypes`]([`Archetype`]) that contain the [`Component`](crate::component::Component),
@@ -804,6 +822,7 @@ impl Archetypes {
804822 TableId :: empty ( ) ,
805823 Vec :: new ( ) ,
806824 Vec :: new ( ) ,
825+ Vec :: new ( ) ,
807826 ) ;
808827 }
809828 archetypes
@@ -896,22 +915,24 @@ impl Archetypes {
896915 /// Specifically, it returns a tuple where the first element
897916 /// is the [`ArchetypeId`] that the given inputs belong to, and the second element is a boolean indicating whether a new archetype was created.
898917 ///
899- /// `table_components` and `sparse_set_components ` must be sorted
918+ /// `table_components`, `sparse_set_components` and `resource_components ` must be sorted
900919 ///
901920 /// # Safety
902921 /// [`TableId`] must exist in tables
903- /// `table_components` and `sparse_set_components ` must exist in `components`
922+ /// `table_components`, `sparse_set_components` and `resource_components ` must exist in `components`
904923 pub ( crate ) unsafe fn get_id_or_insert (
905924 & mut self ,
906925 components : & Components ,
907926 observers : & Observers ,
908927 table_id : TableId ,
909928 table_components : Vec < ComponentId > ,
910929 sparse_set_components : Vec < ComponentId > ,
930+ resource_components : Vec < ComponentId > ,
911931 ) -> ( ArchetypeId , bool ) {
912932 let archetype_identity = ArchetypeComponents {
913933 sparse_set_components : sparse_set_components. into_boxed_slice ( ) ,
914934 table_components : table_components. into_boxed_slice ( ) ,
935+ resource_components : resource_components. into_boxed_slice ( ) ,
915936 } ;
916937
917938 let archetypes = & mut self . archetypes ;
@@ -922,6 +943,7 @@ impl Archetypes {
922943 let ArchetypeComponents {
923944 table_components,
924945 sparse_set_components,
946+ resource_components,
925947 } = vacant. key ( ) ;
926948 let id = ArchetypeId :: new ( archetypes. len ( ) ) ;
927949 archetypes. push ( Archetype :: new (
@@ -932,6 +954,7 @@ impl Archetypes {
932954 table_id,
933955 table_components. iter ( ) . copied ( ) ,
934956 sparse_set_components. iter ( ) . copied ( ) ,
957+ resource_components. iter ( ) . copied ( ) ,
935958 ) ) ;
936959 vacant. insert ( id) ;
937960 ( id, true )
0 commit comments