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

Commit

Permalink
New blocks! fixing bugs too
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamkob12 committed Oct 17, 2023
1 parent 84d111a commit bdfd1bd
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 87 deletions.
Binary file removed assets/UV_map_example.png
Binary file not shown.
Binary file added assets/blocks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 87 additions & 67 deletions src/block_reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,37 @@ pub const AIR: Block = 0;
pub const DIRT: Block = 1;
pub const GRASS: Block = 2;
pub const STONE: Block = 3;
pub const LIGHT_MAGIC: Block = 4;
pub const DARK_MAGIC: Block = 5;
pub const TRANSPERENT: Block = 6;
pub const WOOD_DARK_GREY: Block = 7;
pub const PINK_LEAVES: Block = 8;
pub const BRICKS: Block = 4;
pub const LOG: Block = 5;
pub const WOOD: Block = 6;
pub const LEAVES: Block = 7;
pub const GLASS: Block = 8;
pub const GLOWSTONE: Block = 9;

pub const VOXEL_DIMS: [f32; 3] = [1.0, 1.0, 1.0];

const ATLAS_CORDS: [u32; 2] = [24, 24];
const PADDING: f32 = 0.0625;

#[derive(Resource, Clone)]
pub struct BlockRegistry {
grass_block: Mesh,
dirt_block: Mesh,
stone_block: Mesh,
light_magic_block: Mesh,
dark_magic_block: Mesh,
transperent_block: Mesh,
wood_dark_grey_block: Mesh,
pink_leaves: Mesh,
bricks_block: Mesh,
log_block: Mesh,
wood_block: Mesh,
leaves_block: Mesh,
glass_block: Mesh,
glowstone_block: Mesh,
}

impl Default for BlockRegistry {
fn default() -> Self {
BlockRegistry {
grass_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [0, 0]),
(Bottom, [2, 0]),
Expand All @@ -42,12 +47,12 @@ impl Default for BlockRegistry {
(Forward, [1, 0]),
(Back, [1, 0]),
],
0.1,
PADDING,
Some(0.75),
),
dirt_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [2, 0]),
(Bottom, [2, 0]),
Expand All @@ -56,12 +61,12 @@ impl Default for BlockRegistry {
(Forward, [2, 0]),
(Back, [2, 0]),
],
0.1,
PADDING,
Some(0.75),
),
stone_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [3, 0]),
(Bottom, [3, 0]),
Expand All @@ -70,77 +75,91 @@ impl Default for BlockRegistry {
(Forward, [3, 0]),
(Back, [3, 0]),
],
0.1,
PADDING,
Some(0.75),
),
light_magic_block: generate_voxel_mesh(
bricks_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [2, 1]),
(Bottom, [2, 1]),
(Right, [2, 1]),
(Left, [2, 1]),
(Forward, [2, 1]),
(Back, [2, 1]),
(Top, [4, 0]),
(Bottom, [4, 0]),
(Right, [4, 0]),
(Left, [4, 0]),
(Forward, [4, 0]),
(Back, [4, 0]),
],
0.1,
PADDING,
Some(0.75),
),
dark_magic_block: generate_voxel_mesh(
log_block: generate_voxel_mesh(
VOXEL_DIMS,
ATLAS_CORDS,
[
(Top, [5, 0]),
(Bottom, [5, 0]),
(Right, [6, 0]),
(Left, [6, 0]),
(Forward, [6, 0]),
(Back, [6, 0]),
],
PADDING,
Some(0.90),
),
wood_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [1, 1]),
(Bottom, [1, 1]),
(Right, [1, 1]),
(Left, [1, 1]),
(Forward, [1, 1]),
(Back, [1, 1]),
(Top, [7, 0]),
(Bottom, [7, 0]),
(Right, [7, 0]),
(Left, [7, 0]),
(Forward, [7, 0]),
(Back, [7, 0]),
],
0.1,
PADDING,
Some(0.75),
),
transperent_block: generate_voxel_mesh(
leaves_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [3, 1]),
(Bottom, [3, 1]),
(Right, [3, 1]),
(Left, [3, 1]),
(Forward, [3, 1]),
(Back, [3, 1]),
(Top, [8, 0]),
(Bottom, [8, 0]),
(Right, [8, 0]),
(Left, [8, 0]),
(Forward, [8, 0]),
(Back, [8, 0]),
],
0.1,
PADDING,
Some(0.75),
),
wood_dark_grey_block: generate_voxel_mesh(
glass_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [0, 2]),
(Bottom, [0, 2]),
(Right, [0, 2]),
(Left, [0, 2]),
(Forward, [0, 2]),
(Back, [0, 2]),
(Top, [9, 0]),
(Bottom, [9, 0]),
(Right, [9, 0]),
(Left, [9, 0]),
(Forward, [9, 0]),
(Back, [9, 0]),
],
0.1,
PADDING,
Some(0.75),
),
pink_leaves: generate_voxel_mesh(
glowstone_block: generate_voxel_mesh(
VOXEL_DIMS,
[4, 4],
ATLAS_CORDS,
[
(Top, [1, 2]),
(Bottom, [1, 2]),
(Right, [1, 2]),
(Left, [1, 2]),
(Forward, [1, 2]),
(Back, [1, 2]),
(Top, [10, 0]),
(Bottom, [10, 0]),
(Right, [10, 0]),
(Left, [10, 0]),
(Forward, [10, 0]),
(Back, [10, 0]),
],
0.1,
PADDING,
Some(0.75),
),
}
Expand Down Expand Up @@ -168,7 +187,7 @@ impl VoxelRegistry for BlockRegistry {
}

fn is_covering(&self, voxel: &Self::Voxel, _side: prelude::Face) -> bool {
*voxel != AIR && *voxel != PINK_LEAVES && *voxel != TRANSPERENT
*voxel != AIR && *voxel != LEAVES && *voxel != GLASS
}

fn get_mesh(&self, voxel: &Self::Voxel) -> VoxelMesh<&Mesh> {
Expand All @@ -177,11 +196,12 @@ impl VoxelRegistry for BlockRegistry {
DIRT => VoxelMesh::NormalCube(&self.dirt_block),
GRASS => VoxelMesh::NormalCube(&self.grass_block),
STONE => VoxelMesh::NormalCube(&self.stone_block),
LIGHT_MAGIC => VoxelMesh::NormalCube(&self.light_magic_block),
DARK_MAGIC => VoxelMesh::NormalCube(&self.dark_magic_block),
TRANSPERENT => VoxelMesh::NormalCube(&self.transperent_block),
WOOD_DARK_GREY => VoxelMesh::NormalCube(&self.wood_dark_grey_block),
PINK_LEAVES => VoxelMesh::NormalCube(&self.pink_leaves),
BRICKS => VoxelMesh::NormalCube(&self.bricks_block),
LOG => VoxelMesh::NormalCube(&self.log_block),
WOOD => VoxelMesh::NormalCube(&self.wood_block),
LEAVES => VoxelMesh::NormalCube(&self.leaves_block),
GLASS => VoxelMesh::NormalCube(&self.glass_block),
GLOWSTONE => VoxelMesh::NormalCube(&self.glowstone_block),
_ => VoxelMesh::Null,
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ impl Plugin for ChunkPlugin {
(
spawn_and_despawn_chunks,
frame_chunk_update,
(update_closby_chunks, update_mesh_frame)
.run_if(in_state(InitialChunkLoadState::Complete)),
(update_closby_chunks).run_if(in_state(InitialChunkLoadState::Complete)),
),
);
app.add_systems(
PostUpdate,
(cull_sides_of_mesh.run_if(
in_state(InitialChunkLoadState::Complete)
.and_then(resource_changed::<GlobalSecondsCounter>()),
),),
(
cull_sides_of_mesh.run_if(
in_state(InitialChunkLoadState::Complete)
.and_then(resource_changed::<GlobalSecondsCounter>()),
),
update_mesh_frame,
),
);

// Resources
Expand Down
4 changes: 3 additions & 1 deletion src/chunk/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ pub(crate) fn update_mesh_frame(
.expect("Can't find chunk mesh in internal assets");
update_mesh(mesh_ref_mut, &mut chunk.meta_data, &*breg.clone());
if let Some(aabb) = mesh_ref_mut.compute_aabb() {
commands.entity(ent).insert(aabb).remove::<ToUpdate>();
if let Some(mut comm) = commands.get_entity(ent) {
comm.insert(aabb).remove::<ToUpdate>();
}
} else {
warn!("Couldn't compute Aabb for mesh after updating");
}
Expand Down
9 changes: 1 addition & 8 deletions src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ impl Default for Inventory {
Inventory {
current: 0,
items: vec![
STONE,
GRASS,
DIRT,
LIGHT_MAGIC,
DARK_MAGIC,
TRANSPERENT,
WOOD_DARK_GREY,
PINK_LEAVES,
GRASS, DIRT, STONE, BRICKS, LOG, WOOD, LEAVES, GLASS, GLOWSTONE,
],
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use utils::*;

// const FACTOR: usize = CHUNK_DIMS.0;
// Render distance should be above 1.
pub const RENDER_DISTANCE: i32 = 8;
pub const RENDER_DISTANCE: i32 = 6;
pub const GEN_SEED: u32 = 5;
const CROSSHAIR_SIZE: f32 = 22.0;

Expand Down Expand Up @@ -68,7 +68,7 @@ fn main() {

.init_resource::<BlockRegistry>()
.insert_resource(AmbientLight {
brightness: 1.2, color: Color::ANTIQUE_WHITE})
brightness: 0.75, color: Color::ANTIQUE_WHITE})
.insert_resource(CycleTimer(Timer::new(
bevy::utils::Duration::from_millis(50),
TimerMode::Repeating,)))
Expand Down Expand Up @@ -102,7 +102,7 @@ fn setup(
asset_server: Res<AssetServer>,
mut camera_query: Query<&mut Projection>,
) {
let texture_handle: Handle<Image> = asset_server.load("UV_map_example.png");
let texture_handle: Handle<Image> = asset_server.load("blocks.png");
let mat = materials.add(StandardMaterial {
base_color_texture: Some(texture_handle),
reflectance: 0.0,
Expand All @@ -114,7 +114,7 @@ fn setup(
commands.spawn(LoadedChunks(0));
let mut projection = camera_query.get_single_mut().unwrap();
if let Projection::Perspective(ref mut perspective) = *projection {
perspective.fov = PI / 3.0;
perspective.fov = PI / 3.5;
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/player/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,21 @@ pub(super) fn player_move(
(HALF_CAGE_I as i32) as usize,
(HALF_CAGE_I + dz as i32) as usize,
];
let xzblock_cords = [
(HALF_CAGE_I + dx as i32) as usize,
(HALF_CAGE_I as i32) as usize,
(HALF_CAGE_I + dz as i32) as usize,
];

let xblock = one_d_cords(xblock_cords, CAGE_DIMS);
let yblock = one_d_cords(yblock_cords, CAGE_DIMS);
let zblock = one_d_cords(zblock_cords, CAGE_DIMS);
let xzblock = one_d_cords(xzblock_cords, CAGE_DIMS);
let xblock = cage.blocks[xblock];
let yblock = cage.blocks[yblock];
let zblock = cage.blocks[zblock];
let xzblock = cage.blocks[xzblock];

if xblock == AIR {
transform.translation.x = new_pos.x;
} else if (pos.x - pos.x.round() + 0.5 * dx.signum() * -1.0).abs() > 0.5 {
Expand All @@ -145,7 +153,7 @@ pub(super) fn player_move(
transform.translation.y +=
velocity.y * (pos.y - pos.y.round() + 0.5 * dy.signum() * -1.0).powi(2);
}
if zblock == AIR {
if zblock == AIR && xzblock == AIR {
transform.translation.z = new_pos.z;
} else if (pos.z - pos.z.round() + 0.5 * dz.signum() * -1.0).abs() > 0.5 {
transform.translation.z +=
Expand Down

0 comments on commit bdfd1bd

Please sign in to comment.