Skip to content

Commit

Permalink
[feat] just make string id a string, instead of namespace:path, which…
Browse files Browse the repository at this point in the history
… is very unconvenient and can't be turned into &str
  • Loading branch information
BreakingLead committed Dec 31, 2024
1 parent e4c491c commit a3da1be
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 85 deletions.
3 changes: 2 additions & 1 deletion blockworld-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ serde = { version = "1.0.202", features = ["derive", "serde_derive"] }
serde_json = "1.0.117"
thiserror = "1.0.61"
tokio = "1.37.0"
wgpu = "0.20.0"
wgpu = "0.23.0"
winit = "0.30.0"
slab = "0.4.9"
once_cell = "1.19.0"
bimap = "0.6.3"
enumflags2 = "0.7.10"
bevy_ecs = "0.14.1"
egui-wgpu = "0.30.0"

[[bin]]
name = "blockworld-client"
Expand Down
33 changes: 12 additions & 21 deletions blockworld-client/block/block.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
use blockworld_utils::ResourceLocation;
use blockworld_utils::{HasResourceLocation, ResourceLocation};

pub type NumberID = u32;

pub trait Block: Send + Sync + 'static {
fn hardness(&self) -> f32;
fn material(&self) -> Material;
pub struct Block {
pub id: ResourceLocation,
}

macro_rules! def_basic_block {
($name:ident, $hardness:literal, $material:expr) => {
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct $name;
impl Block for $name {
fn hardness(&self) -> f32 {
$hardness
}
fn material(&self) -> Material {
$material
}
}
};
impl HasResourceLocation for Block {
fn get_id(&self) -> ResourceLocation {
self.id.clone()
}
}

def_basic_block!(Air, 1.5, Material::Air);
def_basic_block!(Stone, 1.5, Material::Solid);
def_basic_block!(Grass, 0.6, Material::Solid);
def_basic_block!(Dirt, 0.5, Material::Solid);
impl Block {
pub fn new(id: ResourceLocation) -> Self {
Self { id }
}
}

#[derive(Debug, Default, Clone, Copy)]
pub enum Material {
Expand Down
14 changes: 5 additions & 9 deletions blockworld-client/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ pub use block::*;
use blockworld_utils::Registry;
use once_cell::sync::Lazy;

pub static BLOCK_REGISTRY: Lazy<Registry<&'static dyn Block>> = Lazy::new(|| {
pub static BLOCK_REGISTRY: Lazy<Registry<Block>> = Lazy::new(|| {
let mut r = Registry::new();
let a0 = Block::new("minecraft:air".into());
r.register(a0);
let a1 = Block::new("minecraft:stone".into());
r.register(a1);

let mut number_id = 0;
r.register("air".into(), &Air as &dyn Block);

number_id += 1;
r.register("stone".into(), &Stone);

number_id += 1;
r.register("grass".into(), &Grass);
r
});
5 changes: 4 additions & 1 deletion blockworld-client/renderer/atlas_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ impl Atlas {
atlas.copy_from(&img, x * tile_size, y * tile_size).unwrap();

let item_name = path.file_stem().unwrap();
let r = ResourceLocation::new(item_name.to_str().unwrap());

let r = ResourceLocation::new(
format!("minecraft:{}", item_name.to_str().unwrap()).as_str(),
);

name_to_xy_map.insert(r, uvec2(x, y));

Expand Down
31 changes: 7 additions & 24 deletions blockworld-client/renderer/bytes_provider.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::*;
use std::path::{Path, PathBuf};

use blockworld_utils::ResourceLocation;
use thiserror::Error;

/// A abstraction over the way resources are loaded.
/// This trait is implemented by different resource providers,
Expand All @@ -14,14 +14,14 @@ pub trait BytesProvider: Send + Sync {
///
/// `minecraft:textures/block/stone.png`
/// `assets/minecraft/textures/block/stone.png`
fn get_bytes(&self, id: &ResourceLocation) -> Result<Vec<u8>, ResourceError>;
fn get_bytes(&self, id: &ResourceLocation) -> Result<Vec<u8>>;
}

/// A resource provider that provides resources from a static value (embedded in the binary).
pub struct StaticBytesProvider;

impl BytesProvider for StaticBytesProvider {
fn get_bytes(&self, id: &ResourceLocation) -> Result<Vec<u8>, ResourceError> {
fn get_bytes(&self, id: &ResourceLocation) -> Result<Vec<u8>> {
if id == &"minecraft:assets/shaders/wireframe_shader.wgsl".into() {
let r = include_bytes!("shaders/wireframe_shader.wgsl").to_vec();
return Ok(r);
Expand All @@ -30,7 +30,7 @@ impl BytesProvider for StaticBytesProvider {
let r = include_bytes!("shaders/default_shader.wgsl").to_vec();
return Ok(r);
}
Err(ResourceError::NotFound(id.clone()))
bail!("Resource not found: {:?}", id);
}
}

Expand All @@ -47,36 +47,19 @@ impl FilesystemBytesProvider {
}

impl BytesProvider for FilesystemBytesProvider {
fn get_bytes(&self, identifier: &ResourceLocation) -> Result<Vec<u8>, ResourceError> {
fn get_bytes(&self, identifier: &ResourceLocation) -> anyhow::Result<Vec<u8>> {
let path = self
.root_dir
.join(Path::new("assets/").join(identifier.get_namespace()))
.join(identifier.get_path());
dbg!(&path);

if !path.exists() {
return Err(ResourceError::NotFound(identifier.clone()));
anyhow::bail!("File not found: {:?}", path);
}

let buf = std::fs::read(path).map_err(|e| ResourceError::Io(e))?;
let buf = std::fs::read(path)?;

Ok(buf)
}
}

#[derive(Error, Debug)]
pub enum ResourceError {
#[error("Resource not found: {0}")]
NotFound(ResourceLocation),
#[error("IO error: {0}")]
Io(std::io::Error),
}

#[test]
fn filesystem_resource_provider_test() {
let p = FilesystemBytesProvider::new(".");
let bytes = p
.get_bytes(&ResourceLocation::new("minecraft:texts/splashes.txt"))
.unwrap();
let s = String::from_utf8(bytes).unwrap();
}
6 changes: 6 additions & 0 deletions blockworld-client/renderer/wgpu/render_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct RenderState {
pub global_timer: Instant,

pub world_renderer: WorldRenderer,

pub egui_renderer: egui_wgpu::Renderer,
}

impl RenderState {
Expand All @@ -44,6 +46,8 @@ impl RenderState {

let world_renderer = WorldRenderer::new(&device, &config, &queue, size);

let egui_renderer = egui_wgpu::Renderer::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, None, 4, true);

Self {
window: window_arc,
surface,
Expand All @@ -55,6 +59,8 @@ impl RenderState {
world_renderer,
dt_timer: Instant::now(),
global_timer: Instant::now(),

egui_renderer:
}
}

Expand Down
10 changes: 0 additions & 10 deletions blockworld-client/world/block_access.rs

This file was deleted.

51 changes: 33 additions & 18 deletions blockworld-client/world/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::{borrow::Cow, ops::Div};
use std::{
borrow::{Borrow, Cow},
ops::Div,
};

use bevy_ecs::system::Res;
use blockworld_utils::ResourceLocation;
use enumflags2::{BitFlag, BitFlags};
use glam::*;
Expand All @@ -16,18 +20,21 @@ pub const CHUNK_BLOCK_NUM: usize = CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT;
// ! Should be optimized later by using 4 bit instead of u8
type LightLevel = u8;

// ExtendedBlockStorage.java
pub struct SubChunk {
/// A total count of the number of non-air blocks in this block storage's Chunk.
block_ref_count: u32,
block_array: [&'static str; SUBCHUNK_SIZE * SUBCHUNK_SIZE * SUBCHUNK_SIZE],
// block_palette: Vec<NumberID>,
// in yzx order
// can be empty
// blocks: Option<Vec<u16>>,

// temp, low performance
blocks: Box<[u32; 4096]>,
}

impl SubChunk {
pub fn new() -> Self {
Self {
block_ref_count: 0,
block_array: core::array::from_fn(|_| "minecraft:air"),
// block_palette: Vec::new(),
blocks: Box::new([0; 4096]),
}
}

Expand All @@ -37,27 +44,35 @@ impl SubChunk {
///
/// From xyz to Index of the block array.
///
/// Don't pass negative numbers into this function!
fn index(x: i32, y: i32, z: i32) -> usize {
// Make sure the index is in the range of 0..15
assert!(
x >= 0
&& y >= 0
&& z >= 0
&& x <= SUBCHUNK_SIZE as i32
&& y <= SUBCHUNK_SIZE as i32
&& z <= SUBCHUNK_SIZE as i32
);

(y * CHUNK_SIZE as i32 * CHUNK_SIZE as i32 + z * CHUNK_SIZE as i32 + x) as usize
}

pub fn set_blockid(&mut self, x: i32, y: i32, z: i32, block_id: &'static str) {
self.block_array[Self::index(x, y, z)] = block_id;
if block_id != "minecraft:air" {
self.block_ref_count += 1;
}
pub fn set_blockid(&mut self, x: i32, y: i32, z: i32, block_id: &str) {
let number_id = BLOCK_REGISTRY.name_to_number_id(&block_id.into());
self.blocks[Self::index(x, y, z)] = number_id;
}

pub fn remove_block(&mut self, x: i32, y: i32, z: i32) {
if self.block_array[Self::index(x, y, z)] != "minecraft:air" {
self.block_array[Self::index(x, y, z)] = "minecraft:air";
self.block_ref_count -= 1;
}
self.blocks[Self::index(x, y, z)] = 0;
}

pub fn get_blockid(&self, x: i32, y: i32, z: i32) -> &'static str {
self.block_array[Self::index(x, y, z)]
if let Some(r) = BLOCK_REGISTRY.number_id_to_name(self.blocks[Self::index(x, y, z)]) {
r
} else {
"minecraft:air"
}
}
}

Expand Down
1 change: 0 additions & 1 deletion blockworld-client/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use once_cell::sync::Lazy;

use crate::renderer::entity::Player;

pub mod block_access;
pub mod chunk;
pub mod chunk_array;

Expand Down

0 comments on commit a3da1be

Please sign in to comment.