diff --git a/src/add_break_blocks.rs b/src/add_break_blocks.rs index b468298..286ec06 100644 --- a/src/add_break_blocks.rs +++ b/src/add_break_blocks.rs @@ -2,13 +2,15 @@ use crate::{one_d_cords, three_d_cords, *}; use bevy::prelude::*; use bevy_meshem::prelude::*; -const RAY_FORWARD_STEP: f32 = 0.03; +const RAY_FORWARD_STEP: f32 = 0.01; const NANO_STEP_FACTOR: f32 = 15.0; -const REACH_DISTANCE: u8 = 4; +const REACH_DISTANCE: u8 = 7; #[derive(Event)] pub struct BlockChange { - pub blocks: Vec<([i32; 2], usize)>, + // Only if we are placing a block, we need to know what block is against the block we are + // placing, because we can't place blocks in the air. If change is `Broken` then this is None. + pub blocks: Vec<([i32; 2], usize, Option)>, pub change: VoxelChange, } @@ -29,7 +31,7 @@ pub fn add_break_detector( block_change_event_writer.send(BlockChange { blocks: blocks_in_the_way(pos, forward, REACH_DISTANCE) .iter() - .map(|(x, y, z)| (*x, one_d_cords(*y, CHUNK_DIMS))) + .map(|(x, y, z)| (*x, one_d_cords(*y, CHUNK_DIMS), None)) .collect(), change: VoxelChange::Broken, }); @@ -44,7 +46,7 @@ pub fn add_break_detector( let tmp = one_d_cords(y, CHUNK_DIMS); if let Some(block) = get_neighbor(tmp, z, CHUNK_DIMS) { // dbg!((x, block)); - (x, block) + (x, block, Some(tmp)) } else { match z { Top => panic!( @@ -53,10 +55,12 @@ pub fn add_break_detector( Bottom => { panic!("\nIn-Game Error: \nCan't build lower than y = 0.") } - Right => ([x[0] + 1, x[1]], tmp - WIDTH + 1), - Left => ([x[0] - 1, x[1]], tmp + WIDTH - 1), - Back => ([x[0], x[1] + 1], tmp - WIDTH * (LENGTH - 1)), - Forward => ([x[0], x[1] - 1], tmp + WIDTH * (LENGTH - 1)), + Right => ([x[0] + 1, x[1]], tmp - WIDTH + 1, Some(tmp)), + Left => ([x[0] - 1, x[1]], tmp + WIDTH - 1, Some(tmp)), + Back => ([x[0], x[1] + 1], tmp - WIDTH * (LENGTH - 1), Some(tmp)), + Forward => { + ([x[0], x[1] - 1], tmp + WIDTH * (LENGTH - 1), Some(tmp)) + } } } }) diff --git a/src/main.rs b/src/main.rs index 0c012c6..1b2d927 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use add_break_blocks::*; use bevy::{ prelude::*, tasks::{AsyncComputeTaskPool, ComputeTaskPool, Task}, + window::PrimaryWindow, }; use bevy_meshem::prelude::*; use block_reg::*; @@ -21,10 +22,13 @@ use player::*; use std::{default, sync::Arc}; pub use utils::*; +use crate::utils::three_d_cords; + // const FACTOR: usize = CHUNK_DIMS.0; // Render distance should be above 1. pub const RENDER_DISTANCE: i32 = 16; pub const GEN_SEED: u32 = 5; +const CROSSHAIR_SIZE: f32 = 36.0; #[derive(Resource, Clone)] pub struct BlockMaterial(Handle); @@ -60,7 +64,7 @@ fn main() { app.add_state::(); - app.add_systems(PreStartup, setup); + app.add_systems(PostStartup, setup); app.add_systems( PostUpdate, update_closby_chunks.run_if(in_state(InitialChunkLoadState::Complete)), @@ -85,6 +89,7 @@ fn setup( mut materials: ResMut>, meshes: ResMut>, asset_server: Res, + primary_window: Query<&Window, With>, ) { let noise = Perlin::new(GEN_SEED); let texture_handle: Handle = asset_server.load("UV_map_example.png"); @@ -94,6 +99,31 @@ fn setup( }); commands.insert_resource(BlockMaterial(mat)); commands.spawn(LoadedChunks(0)); + let mut window_width = CROSSHAIR_SIZE; + let mut window_height = CROSSHAIR_SIZE; + if let Ok(window) = primary_window.get_single() { + (window_width, window_height) = (window.resolution.width(), window.resolution.height()); + } else { + warn!("Primary window not found "); + } + + commands.spawn( + TextBundle::from_section( + format!("+"), + TextStyle { + font_size: CROSSHAIR_SIZE, + color: Color::LIME_GREEN, + ..default() + }, + ) + .with_style(Style { + position_type: PositionType::Absolute, + align_items: AlignItems::Center, + top: Val::Px(window_height / 2.0 - CROSSHAIR_SIZE / 2.0), + left: Val::Px(window_width / 2.0 - CROSSHAIR_SIZE / 2.0), + ..default() + }), + ); } fn frame_chunk_update( @@ -250,15 +280,14 @@ fn handle_block_break_place( mut commands: Commands, ) { for event in block_change.iter() { - 'outer: for &(chunk, block) in event.blocks.iter() { + 'A: for &(chunk, block, onto) in event.blocks.iter() { let ent = chunk_map.get_ent(chunk).expect( "Chunk should be loaded into internal data structure `ChunkMap` but it isn't.", ); - for (e, mut c) in chunk_query.iter_mut() { + 'B: for (e, mut c) in chunk_query.iter_mut() { if e != ent { - continue; + continue 'B; } - println!("hey1"); assert_eq!(c.cords, chunk); let tmp_neighbors: Vec> = vec![None; 6]; let mut neighboring_voxels: [Option; 6] = [None; 6]; @@ -272,12 +301,21 @@ fn handle_block_break_place( } } let vox = c.grid[block]; + let onto = onto.unwrap_or(usize::max_value()); + let onto = if onto == usize::max_value() { + u16::max_value() + } else { + c.grid[onto] + }; + + dbg!(block, onto, vox, three_d_cords(block, CHUNK_DIMS)); if vox == AIR && matches!(event.change, VoxelChange::Broken) { println!("hey2"); break; } - if vox != AIR && matches!(event.change, VoxelChange::Added) { + if (onto == AIR || vox != AIR) && matches!(event.change, VoxelChange::Added) { + println!("hey2"); break; } @@ -300,7 +338,7 @@ fn handle_block_break_place( println!("hey3"); commands.entity(e).insert(ToUpdate); - break 'outer; + break 'A; } } }