@ -10,7 +10,12 @@ pub fn place_break_blocks(
mouse_button_input : Res < ButtonInput < MouseButton > > ,
window : Single < ( & Window , & CursorOptions ) > ,
camera : Single < ( & GlobalTransform , & Camera ) > ,
block_lookup : Query < & Transform , With < Block > > ,
) {
if ! mouse_button_input . any_just_pressed ( [ MouseButton ::Left , MouseButton ::Right ] ) {
return ; // only run this system when left or right mouse button is pressed
}
let ( window , cursor ) = window . into_inner ( ) ;
let ( cam_transform , camera ) = camera . into_inner ( ) ;
@ -26,7 +31,7 @@ pub fn place_break_blocks(
let Some ( ( block , hit ) ) = ray_cast . cast_ray ( ray , settings ) . first ( ) else {
return ; // ray didn't hit anything
} ;
let Some ( block_pos ) = blocks . position ( * block ) else {
let Ok ( block_transform ) = block_lookup . get ( * block ) else {
return ; // entity hit is not a block
} ;
@ -35,10 +40,9 @@ pub fn place_break_blocks(
commands . entity ( * block ) . despawn ( ) ;
} else if mouse_button_input . just_pressed ( MouseButton ::Right ) {
// Create a new block next to the one that was just clicked.
let pos = block_transform . translation . floor ( ) . as_ivec3 ( ) ;
// FIXME: This only works for axis-aligned normals.
let offset = hit . normal . normalize ( ) . round ( ) . as_ivec3 ( ) ;
blocks . spawn ( block_pos + offset ) ;
blocks . spawn ( pos + offset ) ;
}
}