@@ -11,7 +11,7 @@ use std::{alloc::Layout, time::Duration};
1111use  bevy:: prelude:: * ; 
1212use  bevy_app:: ScheduleRunnerPlugin ; 
1313use  bevy_ecs:: { 
14-     ComponentId ,  DynamicComponentInfo ,  DynamicComponentQuery ,  DynamicSystemSettings ,  RuntimeBundle , 
14+     ComponentId ,  DynamicComponentInfo ,  DynamicComponentQuery ,  DynamicSystemSettings ,  EntityBuilder , 
1515    TypeInfo , 
1616} ; 
1717
@@ -24,11 +24,12 @@ fn spawn_scene(world: &mut World, _resources: &mut Resources) {
2424    // representing a Position and one representing a Velocity. Each of these will be made up of two 
2525    // bytes for simplicity, one representing the x and y position/velocity. 
2626
27-     // We create our first component bundle 
28-     let  components1 = RuntimeBundle  { 
29-         // we must define our components' type information 
30-         components :  vec ! [ 
31-             // First we define our "Position" component 
27+     // We create our first entity 
28+     let  mut  builder = EntityBuilder :: new ( ) ; 
29+     // Then we add our "Position component" 
30+     let  entity1 = builder
31+         . add_dynamic ( 
32+             // We need to describe our component's information 
3233            TypeInfo  { 
3334                // We must provide a unique id for the compoonent 
3435                id :  ComponentId :: ExternalId ( 0 ) , 
@@ -37,53 +38,62 @@ fn spawn_scene(world: &mut World, _resources: &mut Resources) {
3738                // And we must specify a drop function for our component 
3839                drop :  |_| ( ) , 
3940            } , 
40-             // Next we define our "Velocity" component 
41+             // And provide the raw byte data data for the component 
42+             vec ! [ 
43+                 0 ,  // X position byte 
44+                 0 ,  // Y position byte 
45+             ] 
46+             // And cast the data to a pointer 
47+             . as_slice ( ) , 
48+         ) 
49+         // Next we add our "Velocity component" 
50+         . add_dynamic ( 
4151            TypeInfo  { 
42-                 // We must specify a different ID for the velocity component  
52+                 // This component needs its own unique ID  
4353                id :  ComponentId :: ExternalId ( 1 ) , 
44-                 // We specify the layout which happens to be the same as "Position" 
45-                 layout:  Layout :: from_size_align( 2 ,  1 ) . unwrap( ) , 
46-                 // And the drop function 
54+                 layout :  Layout :: from_size_align ( 2  /* size */ ,  1  /* alignment */ ) . unwrap ( ) , 
4755                drop :  |_| ( ) , 
4856            } , 
49-         ] , 
50- 
51-         // Data must be a Vector of Vectors of bytes and must contain the raw byte data for 
52-         // each of the components we want to add 
53-         data :  vec ! [ 
54-             // This will be the raw byte data for our position component 
5557            vec ! [ 
5658                0 ,  // X position byte 
57-                 0 ,  // Y position byte 
58-             ] , 
59-             // This will be the raw byte data for our velocity component 
60-             vec![ 
61-                 1 ,  // X velocity byte 
62-                 0 ,  // Y velocity byte 
63-             ] , 
64-         ] , 
65-     } ; 
66- 
67-     // Now we create another bundle for our next entity 
68-     let  components2 = RuntimeBundle  { 
69-         components :  vec ! [ 
59+                 1 ,  // Y position byte 
60+             ] 
61+             . as_slice ( ) , 
62+         ) 
63+         . build ( ) ; 
64+ 
65+     // And let's create another entity 
66+     let  mut  builder = EntityBuilder :: new ( ) ; 
67+     let  entity2 = builder
68+         . add_dynamic ( 
7069            TypeInfo  { 
7170                id :  ComponentId :: ExternalId ( 0 ) , 
72-                 layout:  Layout :: from_size_align( 2   /* size */ ,  1   /* alignment */ ) . unwrap( ) , 
71+                 layout :  Layout :: from_size_align ( 2 ,  1 ) . unwrap ( ) , 
7372                drop :  |_| ( ) , 
7473            } , 
74+             vec ! [ 
75+                 0 ,  // X position byte 
76+                 0 ,  // Y position byte 
77+             ] 
78+             . as_slice ( ) , 
79+         ) 
80+         . add_dynamic ( 
7581            TypeInfo  { 
7682                id :  ComponentId :: ExternalId ( 1 ) , 
7783                layout :  Layout :: from_size_align ( 2 ,  1 ) . unwrap ( ) , 
7884                drop :  |_| ( ) , 
7985            } , 
80-         ] , 
81-         data :  vec ! [ vec![ 0 ,  0 ] ,  vec![ 0 ,  2 ] ] , 
82-     } ; 
86+             vec ! [ 
87+                 2 ,  // X position byte 
88+                 0 ,  // Y position byte 
89+             ] 
90+             . as_slice ( ) , 
91+         ) 
92+         . build ( ) ; 
8393
8494    // Now we can spawn our entities 
85-     world. spawn ( components1 ) ; 
86-     world. spawn ( components2 ) ; 
95+     world. spawn ( entity1 ) ; 
96+     world. spawn ( entity2 ) ; 
8797} 
8898
8999fn  main ( )  { 
0 commit comments