From 346d4ed290742d35adcbc455b32e42d1d15d0d9e Mon Sep 17 00:00:00 2001 From: Rendo Date: Wed, 5 Aug 2026 02:36:58 +0500 Subject: [PATCH] feat: Paint mode done --- src/view/editor.rs | 16 +++++++++++----- src/view/ui.rs | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/view/editor.rs b/src/view/editor.rs index 6c64554..a865f63 100644 --- a/src/view/editor.rs +++ b/src/view/editor.rs @@ -7,7 +7,9 @@ pub struct EditorPlugin; impl Plugin for EditorPlugin { fn build(&self, app: &mut App) { - app.init_resource::().add_observer(apply_tool).add_systems(Update, calculate_tools); + app.init_resource::() + .add_observer(apply_tool) + .add_systems(Update, calculate_tools); } } @@ -31,7 +33,8 @@ pub fn calculate_tools( ray_map: Res, mut raycast: MeshRayCast, mut tool: ResMut, - mut gizmos: Gizmos + mut gizmos: Gizmos, + grid: Res ) { let new_tool: Option = match tool.clone() { EditorTools::None => None, @@ -39,8 +42,9 @@ pub fn calculate_tools( let mut new_position: Option = None; for (_, ray) in ray_map.iter() { let Some((_, hit)) = raycast.cast_ray(*ray, &MeshRayCastSettings::default()).first() else {continue;}; - gizmos.sphere(hit.point.round(), 0.5, Color::BLACK); - new_position = Some(hit.point.floor().as_uvec3()); + let converted_position = (hit.point + 0.51 * hit.normal.normalize()).round(); + gizmos.sphere(converted_position, 0.5, Color::BLACK); + new_position = Some(converted_position.as_ivec3().max(IVec3::ZERO).as_uvec3().min(grid.size-UVec3::ONE)); } new_position.map(|pos|{ @@ -65,7 +69,9 @@ pub fn apply_tool(params: On, mut grid: ResMut) { match params.0 { EditorTools::None => {}, EditorTools::Paint { position, block } => { - let index = grid.indexify(position).unwrap(); + let Some(index) = grid.indexify(position) else { + error!("Grid position {position} is incorrect"); + return;}; grid.data[index] = block; }, } diff --git a/src/view/ui.rs b/src/view/ui.rs index 3af88c0..e9c66a3 100644 --- a/src/view/ui.rs +++ b/src/view/ui.rs @@ -113,7 +113,7 @@ fn editor_column() -> impl Scene { @caption: bsn!{label("Paint")} } on(|_activate: On, mut tool: ResMut, mut label: Single<&mut Text, With>| { - *tool = EditorTools::Paint { position: UVec3::default(), block: GridElement::Solid }; + *tool = EditorTools::Paint { position: UVec3::default(), block: GridElement::Water { amount: 1000 } }; label.0 = "Current tool: Paint".to_string(); }), ]