@@ -11,7 +11,7 @@ use std::{alloc::Layout, time::Duration};
11
11
use bevy:: prelude:: * ;
12
12
use bevy_app:: ScheduleRunnerPlugin ;
13
13
use bevy_ecs:: {
14
- ComponentId , DynamicComponentInfo , DynamicComponentQuery , DynamicSystemSettings , RuntimeBundle ,
14
+ ComponentId , DynamicComponentInfo , DynamicComponentQuery , DynamicSystemSettings , EntityBuilder ,
15
15
TypeInfo ,
16
16
} ;
17
17
@@ -24,11 +24,12 @@ fn spawn_scene(world: &mut World, _resources: &mut Resources) {
24
24
// representing a Position and one representing a Velocity. Each of these will be made up of two
25
25
// bytes for simplicity, one representing the x and y position/velocity.
26
26
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
32
33
TypeInfo {
33
34
// We must provide a unique id for the compoonent
34
35
id : ComponentId :: ExternalId ( 0 ) ,
@@ -37,53 +38,62 @@ fn spawn_scene(world: &mut World, _resources: &mut Resources) {
37
38
// And we must specify a drop function for our component
38
39
drop : |_| ( ) ,
39
40
} ,
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 (
41
51
TypeInfo {
42
- // We must specify a different ID for the velocity component
52
+ // This component needs its own unique ID
43
53
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 ( ) ,
47
55
drop : |_| ( ) ,
48
56
} ,
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
55
57
vec ! [
56
58
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 (
70
69
TypeInfo {
71
70
id : ComponentId :: ExternalId ( 0 ) ,
72
- layout: Layout :: from_size_align( 2 /* size */ , 1 /* alignment */ ) . unwrap( ) ,
71
+ layout : Layout :: from_size_align ( 2 , 1 ) . unwrap ( ) ,
73
72
drop : |_| ( ) ,
74
73
} ,
74
+ vec ! [
75
+ 0 , // X position byte
76
+ 0 , // Y position byte
77
+ ]
78
+ . as_slice ( ) ,
79
+ )
80
+ . add_dynamic (
75
81
TypeInfo {
76
82
id : ComponentId :: ExternalId ( 1 ) ,
77
83
layout : Layout :: from_size_align ( 2 , 1 ) . unwrap ( ) ,
78
84
drop : |_| ( ) ,
79
85
} ,
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 ( ) ;
83
93
84
94
// Now we can spawn our entities
85
- world. spawn ( components1 ) ;
86
- world. spawn ( components2 ) ;
95
+ world. spawn ( entity1 ) ;
96
+ world. spawn ( entity2 ) ;
87
97
}
88
98
89
99
fn main ( ) {
0 commit comments