Skip to content

Commit f0c6784

Browse files
committed
reorganize code:
- move reticle.rs to hud folder - move block destruction logic to player module - move mouse capturing logic to input module - add debug folder in hud module
1 parent 74e7a25 commit f0c6784

25 files changed

+171
-178
lines changed

get_source.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/python
22
import os
3+
import argparse
34

45
def collect_rust_files(directory, output_file):
56
with open(output_file, 'w') as outfile:
@@ -13,8 +14,15 @@ def collect_rust_files(directory, output_file):
1314
outfile.write("\n\n")
1415

1516
if __name__ == "__main__":
16-
source_directory = "./src"
17-
output_file = "source_code.txt"
17+
parser = argparse.ArgumentParser(description="Collect Rust source files from a directory.")
18+
parser.add_argument("directory", help="The path to the source directory.")
19+
parser.add_argument("-o", "--output", default="source_code.txt", help="The output file (default: source_code.txt)")
20+
21+
args = parser.parse_args()
22+
23+
source_directory = args.directory
24+
output_file = args.output
1825

1926
collect_rust_files(source_directory, output_file)
20-
print(f"source code collected in {output_file}")
27+
print(f"Source code collected in {output_file}")
28+

src/camera/cursor.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/camera/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
mod controller;
2-
mod cursor;
3-
mod reticle;
42

53
pub use controller::*;
6-
pub use cursor::*;
7-
pub use reticle::*;

src/debug/mod.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/hud/debug/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub mod blocks;
2+
pub mod chunks;
3+
pub mod coords;
4+
pub mod fps;
5+
mod loaded_stats;
6+
pub mod setup;
7+
pub mod targeted_block;
8+
9+
pub use blocks::*;
10+
pub use chunks::*;
11+
pub use coords::*;
12+
pub use fps::*;
13+
pub use loaded_stats::*;
14+
pub use setup::*;

src/hud/debug.rs renamed to src/hud/debug/setup.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::hud::block::BlockText;
2-
use crate::hud::loaded_stats::{BlocksNumberText, ChunksNumberText};
3-
use crate::hud::{CoordsText, FpsText};
1+
use crate::hud::debug::loaded_stats::{BlocksNumberText, ChunksNumberText};
2+
use crate::hud::debug::targeted_block::BlockText;
3+
use crate::hud::debug::{CoordsText, FpsText};
44
use crate::input::keyboard::{get_action_keys, GameAction};
55
use bevy::prelude::*;
66

File renamed without changes.

src/hud/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
pub mod block;
2-
pub mod coords;
31
pub mod debug;
4-
pub mod fps;
5-
mod loaded_stats;
2+
pub mod reticle;
63

7-
pub use coords::*;
8-
pub use debug::*;
9-
pub use fps::*;
10-
pub use loaded_stats::*;
4+
pub use reticle::*;
File renamed without changes.

src/input/mouse.rs

Lines changed: 13 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,20 @@
1-
use crate::constants::{CUBE_SIZE, INTERACTION_DISTANCE};
2-
use crate::player::inventory::*;
31
use crate::player::Player;
4-
use crate::{items, UIMode};
5-
use crate::world::WorldMap;
6-
use crate::world::WorldRenderRequestUpdateEvent;
7-
use crate::camera::*;
8-
use bevy::math::NormedVectorSpace;
2+
use crate::UIMode;
93
use bevy::prelude::*;
10-
use bevy_mod_raycast::prelude::*;
4+
use bevy::window::{CursorGrabMode, PrimaryWindow};
115

12-
// Helper function to snap a Vec3 position to the grid
13-
fn snap_to_grid(position: Vec3) -> Vec3 {
14-
Vec3::new(position.x.round(), position.y.round(), position.z.round())
6+
// System to hide and lock the cursor
7+
pub fn mouse_grab_system(mut windows: Query<&mut Window, With<PrimaryWindow>>) {
8+
let mut window = windows.single_mut();
9+
window.cursor.visible = false;
10+
window.cursor.grab_mode = CursorGrabMode::Locked;
1511
}
1612

17-
// Function to handle block placement and breaking
18-
pub fn handle_block_interactions(
19-
mut player: Query<&mut Player>,
20-
mut p_transform: Query<&mut Transform, With<Player>>,
21-
mouse_input: Res<ButtonInput<MouseButton>>, // to handle mouse input
22-
raycast_source: Query<&RaycastSource<BlockRaycastSet>>, // raycast from the camera
23-
mut world_map: ResMut<WorldMap>,
24-
mut ev_render: EventWriter<WorldRenderRequestUpdateEvent>,
13+
pub fn set_mouse_visibility(
14+
mut windows: Query<&mut Window, With<PrimaryWindow>>,
15+
player: Query<&Player>,
2516
) {
26-
if player.single().ui_mode == UIMode::Opened {
27-
return;
28-
}
29-
30-
let raycast_source = raycast_source.single();
31-
32-
// Handle left-click for breaking blocks
33-
if mouse_input.just_pressed(MouseButton::Left) {
34-
// Check if there are any intersections with a block
35-
if let Some((_, intersection)) = raycast_source.intersections().first() {
36-
// Check if block is close enough to the player
37-
if (intersection.position() - p_transform.single_mut().translation).norm()
38-
< INTERACTION_DISTANCE
39-
{
40-
let block_pos = intersection.position() - intersection.normal() * (CUBE_SIZE / 2.);
41-
let global_block_coords = IVec3::new(
42-
block_pos.x.floor() as i32,
43-
block_pos.y.floor() as i32,
44-
block_pos.z.floor() as i32,
45-
);
46-
47-
// Remove the hit block
48-
let block = world_map.remove_block_by_coordinates(&global_block_coords);
49-
50-
if let Some(block) = block {
51-
// add the block to the player's inventory
52-
let item_type = items::item_from_block(block);
53-
// If block has corresponding item, add it to inventory
54-
if let Some(item_type) = item_type {
55-
add_item_to_inventory(&mut player, item_type, 1);
56-
}
57-
58-
ev_render.send(WorldRenderRequestUpdateEvent::BlockToReload(
59-
global_block_coords,
60-
));
61-
}
62-
}
63-
}
64-
}
65-
66-
// Handle right-click for placing blocks
67-
if mouse_input.just_pressed(MouseButton::Right) {
68-
if let Some((_entity, intersection)) = raycast_source.intersections().first() {
69-
let block_pos = intersection.position() - intersection.normal() * (CUBE_SIZE / 2.);
70-
let global_block_coords = IVec3::new(
71-
block_pos.x.floor() as i32,
72-
block_pos.y.floor() as i32,
73-
block_pos.z.floor() as i32,
74-
);
75-
76-
// Get the normal of the face where the block will be placed
77-
let normal = intersection.normal(); // This is already a Vec3, no need to unwrap
78-
// Calculate the block position by adding a small offset to the intersection point
79-
let mut position = global_block_coords.as_vec3() + normal * 0.51;
80-
// Snap the position to the grid
81-
position = snap_to_grid(position);
82-
83-
// Check if target space is close enough to the player
84-
// Guarantees a block cannot be placed too close to the player (which would be unable to move because of constant collision)
85-
if (intersection.position() - p_transform.single_mut().translation).norm()
86-
<= INTERACTION_DISTANCE
87-
&& (position - p_transform.single_mut().translation).norm() >= CUBE_SIZE
88-
{
89-
let item_type = items::ItemsType::Dirt;
90-
// Check if the block is in the player's inventory
91-
if has_item(&mut player, item_type) {
92-
// Remove the block from the player's inventory
93-
remove_item_from_inventory(&mut player, item_type, 1);
94-
} else {
95-
return;
96-
}
97-
98-
let block_pos = IVec3::new(position.x as i32, position.y as i32, position.z as i32);
99-
100-
world_map.set_block(&block_pos, items::block_from_item(item_type).unwrap());
101-
102-
ev_render.send(WorldRenderRequestUpdateEvent::BlockToReload(block_pos));
103-
}
104-
}
105-
}
17+
let mut window = windows.single_mut();
18+
let player = player.single();
19+
window.cursor.visible = player.ui_mode == UIMode::Opened;
10620
}

src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::debug::BlockDebugWireframeSettings;
12
use bevy::color::palettes::basic::WHITE;
23
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
34
use bevy::pbr::wireframe::{WireframeConfig, WireframePlugin};
@@ -6,12 +7,12 @@ use bevy::render::render_resource::WgpuFeatures;
67
use bevy::render::settings::{RenderCreation, WgpuSettings};
78
use bevy::render::RenderPlugin;
89
use bevy_mod_raycast::deferred::DeferredRaycastingPlugin;
9-
use block::block_text_update_system;
10+
use debug::targeted_block::block_text_update_system;
1011
use lighting::setup_main_lighting;
1112

1213
use camera::*;
13-
use debug::*;
1414
use exit::*;
15+
use hud::debug::*;
1516
use hud::*;
1617
use input::*;
1718
use lighting::*;
@@ -20,7 +21,6 @@ use ui::inventory::*;
2021
use world::*;
2122
mod camera;
2223
mod constants;
23-
mod debug;
2424
mod exit;
2525
mod hud;
2626
mod input;
@@ -79,7 +79,7 @@ fn main() {
7979
.add_systems(Startup, spawn_reticle)
8080
.add_systems(Startup, setup_hud)
8181
.add_systems(Startup, setup_inventory)
82-
.add_systems(Startup, cursor_grab_system)
82+
.add_systems(Startup, mouse_grab_system)
8383
.add_systems(Startup, setup_chunk_ghost)
8484
.add_systems(Update, toggle_inventory)
8585
.add_systems(Update, set_ui_mode)
@@ -99,7 +99,7 @@ fn main() {
9999
.add_systems(Update, exit_system)
100100
.add_systems(Update, toggle_wireframe_system)
101101
.add_systems(Update, world_render_system)
102-
.add_systems(Update, set_cursor_visibility)
102+
.add_systems(Update, set_mouse_visibility)
103103
.add_systems(Update, inventory_cell_interaction_system)
104104
.add_systems(Update, update_celestial_bodies)
105105
.run();

src/player/controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::camera::CameraController;
22
use crate::constants::GRAVITY;
33
use crate::input::keyboard::*;
44
use crate::player::{Player, ViewMode};
5-
use crate::UIMode;
65
use crate::world::{load_chunk_around_player, WorldMap, WorldRenderRequestUpdateEvent, WorldSeed};
6+
use crate::UIMode;
77
use bevy::prelude::*;
88

99
fn is_block_at_position(position: Vec3, world_map: &WorldMap) -> bool {

src/player/interactions.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use crate::camera::*;
2+
use crate::constants::{CUBE_SIZE, INTERACTION_DISTANCE};
3+
use crate::player::inventory::*;
4+
use crate::player::Player;
5+
use crate::world::WorldMap;
6+
use crate::world::WorldRenderRequestUpdateEvent;
7+
use crate::{items, UIMode};
8+
use bevy::math::NormedVectorSpace;
9+
use bevy::prelude::*;
10+
use bevy_mod_raycast::prelude::*;
11+
12+
// Helper function to snap a Vec3 position to the grid
13+
fn snap_to_grid(position: Vec3) -> Vec3 {
14+
Vec3::new(position.x.round(), position.y.round(), position.z.round())
15+
}
16+
17+
// Function to handle block placement and breaking
18+
pub fn handle_block_interactions(
19+
mut player: Query<&mut Player>,
20+
mut p_transform: Query<&mut Transform, With<Player>>,
21+
mouse_input: Res<ButtonInput<MouseButton>>, // to handle mouse input
22+
raycast_source: Query<&RaycastSource<BlockRaycastSet>>, // raycast from the camera
23+
mut world_map: ResMut<WorldMap>,
24+
mut ev_render: EventWriter<WorldRenderRequestUpdateEvent>,
25+
) {
26+
if player.single().ui_mode == UIMode::Opened {
27+
return;
28+
}
29+
30+
let raycast_source = raycast_source.single();
31+
32+
// Handle left-click for breaking blocks
33+
if mouse_input.just_pressed(MouseButton::Left) {
34+
// Check if there are any intersections with a block
35+
if let Some((_, intersection)) = raycast_source.intersections().first() {
36+
// Check if block is close enough to the player
37+
if (intersection.position() - p_transform.single_mut().translation).norm()
38+
< INTERACTION_DISTANCE
39+
{
40+
let block_pos = intersection.position() - intersection.normal() * (CUBE_SIZE / 2.);
41+
let global_block_coords = IVec3::new(
42+
block_pos.x.floor() as i32,
43+
block_pos.y.floor() as i32,
44+
block_pos.z.floor() as i32,
45+
);
46+
47+
// Remove the hit block
48+
let block = world_map.remove_block_by_coordinates(&global_block_coords);
49+
50+
if let Some(block) = block {
51+
// add the block to the player's inventory
52+
let item_type = items::item_from_block(block);
53+
// If block has corresponding item, add it to inventory
54+
if let Some(item_type) = item_type {
55+
add_item_to_inventory(&mut player, item_type, 1);
56+
}
57+
58+
ev_render.send(WorldRenderRequestUpdateEvent::BlockToReload(
59+
global_block_coords,
60+
));
61+
}
62+
}
63+
}
64+
}
65+
66+
// Handle right-click for placing blocks
67+
if mouse_input.just_pressed(MouseButton::Right) {
68+
if let Some((_entity, intersection)) = raycast_source.intersections().first() {
69+
let block_pos = intersection.position() - intersection.normal() * (CUBE_SIZE / 2.);
70+
let global_block_coords = IVec3::new(
71+
block_pos.x.floor() as i32,
72+
block_pos.y.floor() as i32,
73+
block_pos.z.floor() as i32,
74+
);
75+
76+
// Get the normal of the face where the block will be placed
77+
let normal = intersection.normal(); // This is already a Vec3, no need to unwrap
78+
// Calculate the block position by adding a small offset to the intersection point
79+
let mut position = global_block_coords.as_vec3() + normal * 0.51;
80+
// Snap the position to the grid
81+
position = snap_to_grid(position);
82+
83+
// Check if target space is close enough to the player
84+
// Guarantees a block cannot be placed too close to the player (which would be unable to move because of constant collision)
85+
if (intersection.position() - p_transform.single_mut().translation).norm()
86+
<= INTERACTION_DISTANCE
87+
&& (position - p_transform.single_mut().translation).norm() >= CUBE_SIZE
88+
{
89+
let item_type = items::ItemsType::Dirt;
90+
// Check if the block is in the player's inventory
91+
if has_item(&mut player, item_type) {
92+
// Remove the block from the player's inventory
93+
remove_item_from_inventory(&mut player, item_type, 1);
94+
} else {
95+
return;
96+
}
97+
98+
let block_pos = IVec3::new(position.x as i32, position.y as i32, position.z as i32);
99+
100+
world_map.set_block(&block_pos, items::block_from_item(item_type).unwrap());
101+
102+
ev_render.send(WorldRenderRequestUpdateEvent::BlockToReload(block_pos));
103+
}
104+
}
105+
}
106+
}

src/player/inventory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::constants::MAX_ITEM_SLOTS;
22
use crate::constants::MAX_ITEM_STACK;
33
use crate::player::Player;
4-
use bevy::prelude::*;
54
use crate::ui::inventory::items;
65
use crate::ui::inventory::items::Item;
6+
use bevy::prelude::*;
77

88
// Ajoute un item à l'inventaire du joueur
99
pub fn add_item_to_inventory(

0 commit comments

Comments
 (0)