1
1
use crate :: prelude:: * ;
2
2
3
3
static COLLIDER_GRID_SIZE : u32 = 3 ;
4
- static COLLIDER_RESTING_POSITION : Vec3 = Vec3 :: new ( 0.0 , 0.0 , 0.0 ) ;
4
+ static COLLIDER_RESTING_POSITION : Vec3 = Vec3 :: ZERO ;
5
5
6
6
pub fn setup_coliders_system ( mut commands : Commands ) {
7
7
let collider_range = 0 ..COLLIDER_GRID_SIZE ;
8
8
9
9
for x in collider_range. clone ( ) {
10
10
for y in collider_range. clone ( ) {
11
11
for z in collider_range. clone ( ) {
12
- let key = x * COLLIDER_GRID_SIZE * COLLIDER_GRID_SIZE + y * COLLIDER_GRID_SIZE + z;
13
12
commands
14
13
. spawn ( Collider :: cuboid ( 0.5 , 0.5 , 0.5 ) )
15
14
. insert ( TransformBundle :: from ( Transform :: from_xyz (
16
15
x as f32 , y as f32 , z as f32 ,
17
16
) ) )
18
- . insert ( collider_components:: MyCollider { key } ) ;
17
+ . insert ( collider_components:: BlockCollider {
18
+ relative_position : Vec3 {
19
+ x : x as f32 ,
20
+ y : y as f32 ,
21
+ z : z as f32 ,
22
+ } ,
23
+ } ) ;
19
24
}
20
25
}
21
26
}
22
27
}
23
28
24
29
pub fn handle_collider_update_events_system (
25
30
mut collider_grid_events : EventReader < collider_events:: ColliderUpdateEvent > ,
26
- mut query : Query < ( & mut Transform , & collider_components:: MyCollider ) > ,
31
+ mut query : Query < ( & mut Transform , & collider_components:: BlockCollider ) > ,
27
32
mut chunk_manager : ResMut < terrain_resources:: ChunkManager > ,
28
33
) {
29
34
for event in collider_grid_events. read ( ) {
30
- let event_position =
31
- Vec3 :: new ( event. position [ 0 ] , event. position [ 1 ] , event. position [ 2 ] ) . floor ( ) ;
35
+ let event_position = Vec3 :: new (
36
+ event. grid_center_position [ 0 ] ,
37
+ event. grid_center_position [ 1 ] ,
38
+ event. grid_center_position [ 2 ] ,
39
+ )
40
+ . floor ( ) ;
32
41
for ( mut transform, collider) in query. iter_mut ( ) {
33
- let relative_position = relative_colider_position ( collider. key ) ;
42
+ let relative_position = collider. relative_position ;
34
43
let collider_position = ( event_position + relative_position) . floor ( ) ;
35
44
let block = chunk_manager. get_block ( collider_position) ;
36
45
@@ -50,18 +59,6 @@ pub fn handle_collider_update_events_system(
50
59
}
51
60
}
52
61
53
- fn relative_colider_position ( key : u32 ) -> Vec3 {
54
- let x = key / ( COLLIDER_GRID_SIZE * COLLIDER_GRID_SIZE ) ;
55
- let y = ( key % ( COLLIDER_GRID_SIZE * COLLIDER_GRID_SIZE ) ) / COLLIDER_GRID_SIZE ;
56
- let z = key % COLLIDER_GRID_SIZE ;
57
-
58
- Vec3 {
59
- x : x as f32 - ( COLLIDER_GRID_SIZE / 2 ) as f32 ,
60
- y : y as f32 - ( COLLIDER_GRID_SIZE / 2 ) as f32 ,
61
- z : z as f32 - ( COLLIDER_GRID_SIZE / 2 ) as f32 ,
62
- }
63
- }
64
-
65
62
#[ cfg( test) ]
66
63
mod tests {
67
64
use collider_events:: ColliderUpdateEvent ;
@@ -80,7 +77,7 @@ mod tests {
80
77
81
78
app. update ( ) ;
82
79
83
- let mut colliders_query = app. world . query :: < & collider_components:: MyCollider > ( ) ;
80
+ let mut colliders_query = app. world . query :: < & collider_components:: BlockCollider > ( ) ;
84
81
let colliders_count = colliders_query. iter ( & app. world ) . count ( ) ;
85
82
86
83
assert_eq ! ( colliders_count, 3 * 3 * 3 ) ;
@@ -103,7 +100,13 @@ mod tests {
103
100
} ,
104
101
..Default :: default ( )
105
102
} ,
106
- collider_components:: MyCollider { key : 0 } ,
103
+ collider_components:: BlockCollider {
104
+ relative_position : Vec3 {
105
+ x : 1.0 ,
106
+ y : 2.0 ,
107
+ z : 3.0 ,
108
+ } ,
109
+ } ,
107
110
) ) ;
108
111
109
112
let block = BlockId :: Dirt ;
@@ -122,28 +125,28 @@ mod tests {
122
125
resource. insert_chunks ( chunks) ;
123
126
resource. set_block (
124
127
Vec3 {
125
- x : 9 .0,
126
- y : 9 .0,
127
- z : 9 .0,
128
+ x : 6 .0,
129
+ y : 7 .0,
130
+ z : 8 .0,
128
131
} ,
129
132
block,
130
133
) ;
131
134
132
135
app. world . send_event ( ColliderUpdateEvent {
133
- position : [ 10 .0, 10 .0, 10 .0] ,
136
+ grid_center_position : [ 5 .0, 5 .0, 5 .0] ,
134
137
} ) ;
135
138
136
139
app. update ( ) ;
137
140
138
141
let mut collider_query = app
139
142
. world
140
- . query :: < ( & Transform , & collider_components:: MyCollider ) > ( ) ;
143
+ . query :: < ( & Transform , & collider_components:: BlockCollider ) > ( ) ;
141
144
let ( collider_transform, _) = collider_query. single ( & app. world ) ;
142
145
assert_eq ! (
143
146
Vec3 {
144
- x: 9 .5,
145
- y: 9 .5,
146
- z: 9 .5
147
+ x: 6 .5,
148
+ y: 7 .5,
149
+ z: 8 .5
147
150
} ,
148
151
collider_transform. translation
149
152
) ;
0 commit comments