Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
added crosshair, breaking/placing blocks works well, but it is slighl…
Browse files Browse the repository at this point in the history
…y off (in the direction)
  • Loading branch information
Adamkob12 committed Sep 25, 2023
1 parent 55e9372 commit f6a294f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
22 changes: 13 additions & 9 deletions src/add_break_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>)>,
pub change: VoxelChange,
}

Expand All @@ -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,
});
Expand All @@ -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!(
Expand All @@ -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))
}
}
}
})
Expand Down
52 changes: 45 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use add_break_blocks::*;
use bevy::{
prelude::*,
tasks::{AsyncComputeTaskPool, ComputeTaskPool, Task},
window::PrimaryWindow,
};
use bevy_meshem::prelude::*;
use block_reg::*;
Expand All @@ -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<StandardMaterial>);
Expand Down Expand Up @@ -60,7 +64,7 @@ fn main() {

app.add_state::<InitialChunkLoadState>();

app.add_systems(PreStartup, setup);
app.add_systems(PostStartup, setup);
app.add_systems(
PostUpdate,
update_closby_chunks.run_if(in_state(InitialChunkLoadState::Complete)),
Expand All @@ -85,6 +89,7 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
meshes: ResMut<Assets<Mesh>>,
asset_server: Res<AssetServer>,
primary_window: Query<&Window, With<PrimaryWindow>>,
) {
let noise = Perlin::new(GEN_SEED);
let texture_handle: Handle<Image> = asset_server.load("UV_map_example.png");
Expand All @@ -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(
Expand Down Expand Up @@ -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<Option<Block>> = vec![None; 6];
let mut neighboring_voxels: [Option<Block>; 6] = [None; 6];
Expand All @@ -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;
}

Expand All @@ -300,7 +338,7 @@ fn handle_block_break_place(

println!("hey3");
commands.entity(e).insert(ToUpdate);
break 'outer;
break 'A;
}
}
}
Expand Down

0 comments on commit f6a294f

Please sign in to comment.