@@ -9,6 +9,7 @@ use wgpu::util::{BufferInitDescriptor, DeviceExt};
9
9
use wgpu:: BindGroupLayout ;
10
10
11
11
use crate :: blocks:: block:: { Block , FaceDirections } ;
12
+ use crate :: blocks:: block_type:: BlockType ;
12
13
use crate :: collision:: { CollisionPoint , RayResult } ;
13
14
use crate :: persistence:: { Loadable , Saveable } ;
14
15
use crate :: {
@@ -42,6 +43,7 @@ pub struct Player {
42
43
pub current_chunk : ( i32 , i32 ) ,
43
44
pub on_ground : bool ,
44
45
pub is_jumping : bool ,
46
+ pub in_water : bool ,
45
47
pub jump_action_start : Option < Instant > ,
46
48
pub is_ghost : bool ,
47
49
pub facing_block : Option < Arc < RwLock < Block > > > ,
@@ -72,8 +74,8 @@ impl Player {
72
74
// Gets the block that the player is facing
73
75
pub fn get_facing_block < ' a > (
74
76
& mut self ,
75
- collisions : & ' a Vec < CollisionBox > ,
76
- ) -> Option < ( & ' a CollisionBox , FaceDirections ) > {
77
+ blocks : & ' a Vec < Arc < RwLock < Block > > > ,
78
+ ) -> Option < ( CollisionBox , FaceDirections ) > {
77
79
let forward = self . camera . get_forward_dir ( ) ;
78
80
let mut ray_results: Vec < RayResult > = vec ! [ ] ;
79
81
@@ -82,11 +84,13 @@ impl Player {
82
84
origin : self . camera . eye + PLAYER_VIEW_OFFSET ,
83
85
} ;
84
86
85
- for collision in collisions. iter ( ) {
86
- if let Some ( intersection_points) = ray. intersects_box ( collision) {
87
+ for block in blocks. iter ( ) {
88
+ if let Some ( intersection_points) =
89
+ ray. intersects_box ( & block. read ( ) . unwrap ( ) . collision_box )
90
+ {
87
91
ray_results. push ( RayResult {
88
92
points : intersection_points,
89
- collision,
93
+ collision : block . read ( ) . unwrap ( ) . collision_box . clone ( ) ,
90
94
} )
91
95
}
92
96
}
@@ -104,7 +108,7 @@ impl Player {
104
108
105
109
if closest_point. distance ( self . camera . eye ) < max_distance {
106
110
max_distance = closest_point. distance ( self . camera . eye ) ;
107
- block_collision = Some ( result. collision ) ;
111
+ block_collision = Some ( & result. collision ) ;
108
112
point = Some ( closest_point. clone ( ) ) ;
109
113
}
110
114
}
@@ -124,7 +128,7 @@ impl Player {
124
128
face_direction = Some ( face) ;
125
129
}
126
130
}
127
- Some ( ( block_collision, * face_direction. unwrap ( ) ) )
131
+ Some ( ( block_collision. clone ( ) , * face_direction. unwrap ( ) ) )
128
132
}
129
133
_ => None ,
130
134
} ;
@@ -141,7 +145,7 @@ impl Player {
141
145
& mut self ,
142
146
direction : & Vec3 ,
143
147
delta_time : f32 ,
144
- collisions : & Vec < CollisionBox > ,
148
+ blocks : & Vec < Arc < RwLock < Block > > > ,
145
149
) {
146
150
let input_direction = direction;
147
151
let player_collision = self . get_collision ( ) ;
@@ -172,22 +176,38 @@ impl Player {
172
176
return ;
173
177
}
174
178
179
+ self . in_water = false ;
175
180
let can_move_z = player_collision. clone ( ) + glam:: vec3 ( 0.0 , 0.0 , velocity. z ) ;
176
- for collision in collisions. iter ( ) {
177
- if can_move_z. intersects ( collision) {
181
+ let can_move_x = player_collision. clone ( ) + glam:: vec3 ( velocity. x , 0.0 , 0.0 ) ;
182
+
183
+ for block in blocks. iter ( ) {
184
+ let block_read = block. read ( ) . unwrap ( ) ;
185
+ if can_move_z. intersects ( & block_read. collision_box )
186
+ && block_read. block_type != BlockType :: Water
187
+ {
178
188
velocity. z = 0.0 ;
179
189
}
180
- }
181
- let can_move_x = player_collision . clone ( ) + glam :: vec3 ( velocity . x , 0.0 , 0.0 ) ;
182
- for collision in collisions . iter ( ) {
183
- if can_move_x . intersects ( collision ) {
190
+
191
+ if can_move_x . intersects ( & block_read . collision_box )
192
+ && block_read . block_type != BlockType :: Water
193
+ {
184
194
velocity. x = 0.0 ;
185
195
}
196
+
197
+ if player_collision. intersects ( & block_read. collision_box )
198
+ && block_read. block_type == BlockType :: Water
199
+ {
200
+ self . in_water = true ;
201
+ }
186
202
}
187
203
188
204
velocity. y -= GRAVITY * delta_time;
189
205
self . on_ground = false ;
190
206
207
+ if self . in_water {
208
+ // Slow down gravity in water
209
+ velocity. y *= 0.7 ;
210
+ }
191
211
if self . is_jumping {
192
212
let now = Instant :: now ( ) ;
193
213
let delta_jump = now
@@ -203,8 +223,10 @@ impl Player {
203
223
}
204
224
205
225
let can_move_y = player_collision. clone ( ) + glam:: vec3 ( 0.0 , velocity. y , 0.0 ) ;
206
- for collision in collisions. iter ( ) {
207
- if can_move_y. intersects ( collision) {
226
+ for block in blocks. iter ( ) {
227
+ if can_move_y. intersects ( & block. read ( ) . unwrap ( ) . collision_box )
228
+ && block. read ( ) . unwrap ( ) . block_type != BlockType :: Water
229
+ {
208
230
velocity. y = 0.0 ;
209
231
self . on_ground = true ; // This can make it infinite to jump if there is a block above
210
232
}
@@ -216,8 +238,6 @@ impl Player {
216
238
}
217
239
218
240
self . camera . eye += velocity;
219
-
220
- self . camera . needs_update = true ;
221
241
}
222
242
}
223
243
pub struct Camera {
0 commit comments