Skip to content

Commit

Permalink
change chunk pos format
Browse files Browse the repository at this point in the history
  • Loading branch information
BreakingLead committed May 31, 2024
1 parent 61a9e4c commit 3553643
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
30 changes: 26 additions & 4 deletions blockworld-client/game/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,33 @@ pub const CHUNK_SIZE: usize = 16;
pub const CHUNK_HEIGHT: usize = 256;
pub const CHUNK_BLOCK_NUM: usize = CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT;

/// from `net/minecraft/util/math/ChunkPos.java`
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct ChunkPos {
pub x: i32,
pub z: i32,
}

impl ChunkPos {
pub fn new(x: i32, z: i32) -> Self {
Self { x, z }
}

pub fn as_long(x: i32, z: i32) -> i64 {
//? From minecraft source code
return (x as i64 & 0xFFFFFFFFi64) | (z as i64 & 0xFFFFFFFFi64) << 32;
}

pub fn hash_code(&self) -> i32 {
let i: i32 = 1664525 * self.x + 1013904223;
let j: i32 = 1664525 * (self.z ^ -559038737) + 1013904223;
i ^ j
}
}

pub struct Chunk {
pub blocks: Box<[Block; CHUNK_HEIGHT * CHUNK_SIZE * CHUNK_SIZE]>,
pub x_pos: i32,
pub z_pos: i32,
pub pos: ChunkPos,
}

impl Chunk {
Expand Down Expand Up @@ -116,8 +139,7 @@ impl Default for Chunk {

Self {
blocks,
x_pos: 0,
z_pos: 0,
pos: ChunkPos::new(0, 0),
}
}
}
3 changes: 2 additions & 1 deletion blockworld-client/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use self::player_state::PlayerState;

pub mod block;
pub mod chunk;
pub mod chunk_array;
pub mod console_instr;
pub mod player_state;
pub mod register;
pub mod save;
pub mod settings;

#[derive(Default,Debug)]
#[derive(Default, Debug)]
pub struct Game {
pub player_state: PlayerState,
}
Expand Down
19 changes: 9 additions & 10 deletions blockworld-client/game/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use glam::{ivec2, IVec2, IVec3};

use crate::game::chunk::CHUNK_SIZE;

use super::chunk::Chunk;
use super::chunk::{Chunk, ChunkPos};
struct ChunkPool {
chunks: HashMap<IVec2, Chunk>,
chunks: HashMap<ChunkPos, Chunk>,
}

impl ChunkPool {
Expand All @@ -19,19 +19,18 @@ impl ChunkPool {
}
}

pub fn load_chunk(x: i32, y: i32) -> Result<()> {
let _ = x;
let _ = y;
pub fn load_chunk(pos: ChunkPos) -> Result<()> {
let _ = pos;
todo!();
}
pub fn generate_chunk(&mut self, x: i32, z: i32) -> Result<()> {
if self.chunks.contains_key(&ivec2(x, z)) {

pub fn generate_chunk(&mut self, pos: ChunkPos) -> Result<()> {
if self.chunks.contains_key(&pos) {
Err(anyhow::Error::msg("Chunk already generated"))
} else {
let mut chunk = Chunk::default();
chunk.x_pos = x;
chunk.z_pos = x;
self.chunks.insert(ivec2(x, z), chunk);
chunk.pos = pos;
self.chunks.insert(pos, chunk);
Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions blockworld-client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod debug;
mod game;
mod io;
mod render;
mod util;

#[derive(Parser, Clone, Default)]
#[command(author, version, about)]
Expand Down
2 changes: 1 addition & 1 deletion blockworld-client/render/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl WireframePipeline {
});

let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"),
label: Some("Wireframe Pipeline"),
layout: Some(&layout),
vertex: wgpu::VertexState {
module: &shader,
Expand Down
4 changes: 2 additions & 2 deletions blockworld-client/render/render_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl RenderChunk {
for y in 0..CHUNK_HEIGHT as i32 {
for z in 0..CHUNK_SIZE as i32 {
let (abs_x, abs_z) = (
(chunk.x_pos * CHUNK_SIZE as i32 + x as i32) as f32,
(chunk.z_pos * CHUNK_SIZE as i32 + z as i32) as f32,
(chunk.pos.x * CHUNK_SIZE as i32 + x as i32) as f32,
(chunk.pos.z * CHUNK_SIZE as i32 + z as i32) as f32,
);
let block_id = chunk.blocks[Chunk::index(x, y, z)].id;
// info!("Block: {}", block_id);
Expand Down

0 comments on commit 3553643

Please sign in to comment.