Skip to content

Commit f40254a

Browse files
committed
add block placement
1 parent cad32b8 commit f40254a

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

src/input/mouse.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,54 @@ use crate::camera::*;
22
use bevy::prelude::*;
33
use bevy_mod_raycast::prelude::*;
44

5-
pub fn handle_block_breaking(
5+
// Helper function to snap a Vec3 position to the grid
6+
fn snap_to_grid(position: Vec3) -> Vec3 {
7+
Vec3::new(position.x.round(), position.y.round(), position.z.round())
8+
}
9+
10+
// Function to handle block placement and breaking
11+
pub fn handle_block_interactions(
612
mut commands: Commands,
7-
mouse_input: Res<ButtonInput<MouseButton>>, // to handle mouse input
13+
mouse_input: Res<ButtonInput<MouseButton>>, // to handle mouse input
14+
mut meshes: ResMut<Assets<Mesh>>, // for adding new block meshes
15+
mut materials: ResMut<Assets<StandardMaterial>>, // for adding new block materials
816
raycast_source: Query<&RaycastSource<BlockRaycastSet>>, // raycast from the camera
917
) {
10-
// check if the left mouse button was pressed
11-
if mouse_input.just_pressed(MouseButton::Left) {
12-
let raycast_source = raycast_source.single();
18+
let dirt_material = materials.add(Color::srgb(0.5, 0.25, 0.0)); // Material for dirt block
19+
let cube_mesh = meshes.add(Mesh::from(Cuboid::new(1.0, 1.0, 1.0))); // Cube mesh for the blocks
1320

14-
// check if there are any intersections with a block
21+
let raycast_source = raycast_source.single();
22+
23+
// Handle left-click for breaking blocks
24+
if mouse_input.just_pressed(MouseButton::Left) {
25+
// Check if there are any intersections with a block
1526
if let Some((entity, _intersection)) = raycast_source.intersections().first() {
16-
// println!("block hit, removing...");
17-
// remove the hit block
27+
// Remove the hit block
1828
commands.entity(*entity).despawn();
1929
}
2030
}
31+
32+
// Handle right-click for placing blocks
33+
if mouse_input.just_pressed(MouseButton::Right) {
34+
if let Some((_entity, intersection)) = raycast_source.intersections().first() {
35+
// Get the normal of the face where the block will be placed
36+
let normal = intersection.normal(); // This is already a Vec3, no need to unwrap
37+
// Calculate the block position by adding a small offset to the intersection point
38+
let mut position = intersection.position() + normal * 0.51;
39+
40+
// Snap the position to the grid
41+
position = snap_to_grid(position);
42+
43+
// Spawn a new dirt block at the snapped position
44+
commands.spawn((
45+
PbrBundle {
46+
mesh: cube_mesh.clone(),
47+
material: dirt_material.clone(),
48+
transform: Transform::from_translation(position),
49+
..Default::default()
50+
},
51+
RaycastMesh::<BlockRaycastSet>::default(), // Mark the new block as raycastable
52+
));
53+
}
54+
}
2155
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ fn main() {
3232
.add_systems(Update, player_movement_system)
3333
.add_systems(Update, camera_control_system)
3434
.add_systems(Update, (fps_text_update_system, fps_counter_showhide))
35-
.add_systems(Update, handle_block_breaking) // Ajout du système de clic pour casser les blocs
35+
.add_systems(Update, handle_block_interactions) // Ajout du système de clic pour casser les blocs
3636
.run();
3737
}

src/player/controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn player_movement_system(
6060

6161
let speed = 5.0;
6262
let gravity = (-9.8) * 4.0;
63-
let jump_velocity = 5.0 * 2.0;
63+
let jump_velocity = 6.0 * 2.0;
6464

6565
// Calculate movement directions relative to the camera
6666
let mut forward = camera_transform.forward().xyz();

0 commit comments

Comments
 (0)