Skip to content

Commit 1da876d

Browse files
committed
swimming
1 parent 6a8de89 commit 1da876d

File tree

5 files changed

+59
-45
lines changed

5 files changed

+59
-45
lines changed

src/blocks/block.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::HashMap;
33

44
use super::block_type::BlockType;
55
use crate::chunk::BlockVec;
6+
use crate::collision::CollisionBox;
67
use crate::effects::ao::{convert_ao_u8_to_f32, from_vertex_position};
78
use crate::world::CHUNK_SIZE;
89
use glam::Vec3;
@@ -12,6 +13,7 @@ use std::sync::{Arc, MutexGuard, RwLock};
1213
pub struct Block {
1314
pub position: glam::Vec3,
1415
pub absolute_position: glam::Vec3,
16+
pub collision_box: CollisionBox,
1517
pub block_type: BlockType,
1618
}
1719

@@ -115,7 +117,13 @@ impl Block {
115117
position.y,
116118
(chunk.1 * CHUNK_SIZE as i32 + position.z as i32) as f32,
117119
);
120+
let collision_box = CollisionBox::from_block_position(
121+
absolute_position.x,
122+
absolute_position.y,
123+
absolute_position.z,
124+
);
118125
Block {
126+
collision_box,
119127
position,
120128
block_type,
121129
absolute_position,

src/collision.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ impl Ray {
8686
}
8787

8888
#[derive(Debug)]
89-
pub struct RayResult<'a> {
89+
pub struct RayResult {
9090
pub points: Vec<glam::Vec3>,
91-
pub collision: &'a CollisionBox,
91+
pub collision: CollisionBox,
9292
}
9393

9494
impl CollisionPoint {

src/player.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use wgpu::util::{BufferInitDescriptor, DeviceExt};
99
use wgpu::BindGroupLayout;
1010

1111
use crate::blocks::block::{Block, FaceDirections};
12+
use crate::blocks::block_type::BlockType;
1213
use crate::collision::{CollisionPoint, RayResult};
1314
use crate::persistence::{Loadable, Saveable};
1415
use crate::{
@@ -42,6 +43,7 @@ pub struct Player {
4243
pub current_chunk: (i32, i32),
4344
pub on_ground: bool,
4445
pub is_jumping: bool,
46+
pub in_water: bool,
4547
pub jump_action_start: Option<Instant>,
4648
pub is_ghost: bool,
4749
pub facing_block: Option<Arc<RwLock<Block>>>,
@@ -72,8 +74,8 @@ impl Player {
7274
// Gets the block that the player is facing
7375
pub fn get_facing_block<'a>(
7476
&mut self,
75-
collisions: &'a Vec<CollisionBox>,
76-
) -> Option<(&'a CollisionBox, FaceDirections)> {
77+
blocks: &'a Vec<Arc<RwLock<Block>>>,
78+
) -> Option<(CollisionBox, FaceDirections)> {
7779
let forward = self.camera.get_forward_dir();
7880
let mut ray_results: Vec<RayResult> = vec![];
7981

@@ -82,11 +84,13 @@ impl Player {
8284
origin: self.camera.eye + PLAYER_VIEW_OFFSET,
8385
};
8486

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+
{
8791
ray_results.push(RayResult {
8892
points: intersection_points,
89-
collision,
93+
collision: block.read().unwrap().collision_box.clone(),
9094
})
9195
}
9296
}
@@ -104,7 +108,7 @@ impl Player {
104108

105109
if closest_point.distance(self.camera.eye) < max_distance {
106110
max_distance = closest_point.distance(self.camera.eye);
107-
block_collision = Some(result.collision);
111+
block_collision = Some(&result.collision);
108112
point = Some(closest_point.clone());
109113
}
110114
}
@@ -124,7 +128,7 @@ impl Player {
124128
face_direction = Some(face);
125129
}
126130
}
127-
Some((block_collision, *face_direction.unwrap()))
131+
Some((block_collision.clone(), *face_direction.unwrap()))
128132
}
129133
_ => None,
130134
};
@@ -141,7 +145,7 @@ impl Player {
141145
&mut self,
142146
direction: &Vec3,
143147
delta_time: f32,
144-
collisions: &Vec<CollisionBox>,
148+
blocks: &Vec<Arc<RwLock<Block>>>,
145149
) {
146150
let input_direction = direction;
147151
let player_collision = self.get_collision();
@@ -172,22 +176,38 @@ impl Player {
172176
return;
173177
}
174178

179+
self.in_water = false;
175180
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+
{
178188
velocity.z = 0.0;
179189
}
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+
{
184194
velocity.x = 0.0;
185195
}
196+
197+
if player_collision.intersects(&block_read.collision_box)
198+
&& block_read.block_type == BlockType::Water
199+
{
200+
self.in_water = true;
201+
}
186202
}
187203

188204
velocity.y -= GRAVITY * delta_time;
189205
self.on_ground = false;
190206

207+
if self.in_water {
208+
// Slow down gravity in water
209+
velocity.y *= 0.7;
210+
}
191211
if self.is_jumping {
192212
let now = Instant::now();
193213
let delta_jump = now
@@ -203,8 +223,10 @@ impl Player {
203223
}
204224

205225
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+
{
208230
velocity.y = 0.0;
209231
self.on_ground = true; // This can make it infinite to jump if there is a block above
210232
}
@@ -216,8 +238,6 @@ impl Player {
216238
}
217239

218240
self.camera.eye += velocity;
219-
220-
self.camera.needs_update = true;
221241
}
222242
}
223243
pub struct Camera {

src/state.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl State {
7878
let current_chunk = camera.eye.get_chunk_from_position_absolute();
7979
let player = Arc::new(RwLock::new(Player {
8080
camera,
81+
in_water: false,
8182
current_chunk,
8283
is_jumping: false,
8384
on_ground: false,
@@ -179,7 +180,7 @@ impl State {
179180
state: winit::event::ElementState::Pressed,
180181
..
181182
} => {
182-
if player.on_ground {
183+
if player.on_ground || player.in_water {
183184
player.is_jumping = true;
184185
player.jump_action_start = Some(std::time::Instant::now());
185186
}
@@ -246,28 +247,16 @@ impl State {
246247
}
247248
}
248249
pub fn update(&mut self, delta_time: f32, total_time: f32) {
249-
let mut collisions = vec![];
250-
251-
let start_update = Instant::now();
252-
if let Some(nearby_blocks) = self.world.get_blocks_nearby(Arc::clone(&self.player)) {
253-
for block in nearby_blocks.iter() {
254-
let block = block.read().unwrap();
255-
let collision = CollisionBox::from_block_position(
256-
block.absolute_position.x,
257-
block.position.y,
258-
block.absolute_position.z,
259-
);
260-
collisions.push(collision);
261-
}
262-
};
250+
let nearby_blocks = self.world.get_blocks_nearby(Arc::clone(&self.player));
251+
263252
let mut player = self.player.write().unwrap();
264253
player.move_camera(
265254
&self.camera_controller.movement_vector,
266255
delta_time,
267-
&collisions,
256+
&nearby_blocks,
268257
);
269258
player.update();
270-
if let Some((block, face_dir)) = player.get_facing_block(&collisions) {
259+
if let Some((block, face_dir)) = player.get_facing_block(&nearby_blocks) {
271260
let block = self.world.get_blocks_absolute(&block.to_block_position());
272261

273262
player.facing_block = block;

src/world.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,14 @@ impl World {
104104

105105
return Some(block);
106106
}
107-
pub fn get_blocks_nearby(
108-
&self,
109-
player: Arc<RwLock<Player>>,
110-
) -> Option<Vec<Arc<RwLock<Block>>>> {
107+
pub fn get_blocks_nearby(&self, player: Arc<RwLock<Player>>) -> Vec<Arc<RwLock<Block>>> {
111108
let player = player.read().unwrap();
112109
let mut positions = vec![];
113110
let mut nearby_blocks = vec![];
114111

115-
for i in -10..=10 {
116-
for j in -10..=10 {
117-
for h in -10..=10 {
112+
for i in -5..=5 {
113+
for j in -5..=5 {
114+
for h in -5..=5 {
118115
positions.push(player.camera.eye + glam::vec3(i as f32, h as f32, j as f32));
119116
}
120117
}
@@ -126,7 +123,7 @@ impl World {
126123
};
127124
}
128125

129-
return Some(nearby_blocks);
126+
return nearby_blocks;
130127
}
131128
pub fn update(
132129
&mut self,

0 commit comments

Comments
 (0)