chunk rebuilder system

refactoring
This commit is contained in:
2024-05-06 22:26:30 -04:00
parent cd4c9f2acf
commit 58a9f62dca
12 changed files with 195 additions and 41 deletions

View File

@@ -11,9 +11,8 @@ pub fn generate_chunk_collider(chunk: &Chunk, map: &Map) -> (Vec<Vec3>, Vec<[u32
for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE {
let height = chunk.heights[x + z * Chunk::SIZE];
let coord = HexCoord::from_offset(
IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32),
);
let coord =
HexCoord::from_offset(IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32));
let neighbors = map.get_neighbors(&coord);
let off_pos = Vec3::new(x as f32, height, z as f32);
let tile_pos = offset3d_to_world(off_pos);
@@ -23,12 +22,7 @@ pub fn generate_chunk_collider(chunk: &Chunk, map: &Map) -> (Vec<Vec3>, Vec<[u32
return (verts, indices);
}
fn create_tile_collider(
pos: Vec3,
verts: &mut Vec<Vec3>,
indices: &mut Vec<[u32; 3]>,
neighbors: &[Option<f32>; 6],
) {
fn create_tile_collider(pos: Vec3, verts: &mut Vec<Vec3>, indices: &mut Vec<[u32; 3]>, neighbors: &[Option<f32>; 6]) {
let idx = verts.len() as u32;
for i in 0..6 {
let p = pos + HEX_CORNERS[i];
@@ -66,13 +60,7 @@ fn create_tile_collider(
}
}
fn create_tile_wall_collider(
idx: u32,
pos: Vec3,
dir: usize,
verts: &mut Vec<Vec3>,
indices: &mut Vec<[u32; 3]>,
) {
fn create_tile_wall_collider(idx: u32, pos: Vec3, dir: usize, verts: &mut Vec<Vec3>, indices: &mut Vec<[u32; 3]>) {
let idx2 = verts.len() as u32;
verts.push(pos + HEX_CORNERS[dir]);

View File

@@ -28,6 +28,10 @@ pub fn offset_to_hex(offset: IVec2) -> IVec3 {
return v;
}
pub fn offset_to_index(offset: IVec2, width: usize) -> usize {
return offset.x as usize + offset.y as usize * width;
}
pub fn snap_to_hex_grid(world_pos: Vec3) -> Vec3 {
return offset_to_world(world_to_offset_pos(world_pos), world_pos.y);
}

View File

@@ -11,8 +11,6 @@ use bevy::{
},
};
pub fn generate_chunk_mesh(
chunk: &Chunk,
map: &Map,
@@ -33,9 +31,8 @@ pub fn generate_chunk_mesh(
let temperature = chunk.temperature[x + z * Chunk::SIZE];
let off_pos = Vec3::new(x as f32, height, z as f32);
let tile_pos = offset3d_to_world(off_pos);
let coord = HexCoord::from_offset(
IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32),
);
let coord =
HexCoord::from_offset(IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32));
let n = map.get_neighbors(&coord);
let biome = mappers.get(painter.sample_biome(moisture, temperature));
let tile_handle = biome.unwrap().sample_tile(height);

View File

@@ -28,9 +28,8 @@ pub fn generate_packed_chunk_mesh(
let height = chunk.heights[x + z * Chunk::SIZE];
let moisture = chunk.moisture[x + z * Chunk::SIZE];
let temperature = chunk.temperature[x + z * Chunk::SIZE];
let coord = HexCoord::from_offset(
IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32),
);
let coord =
HexCoord::from_offset(IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32));
let n = map.get_neighbors(&coord);
let biome = mappers.get(painter.sample_biome(moisture, temperature));
let tile_handle = biome.unwrap().sample_tile(height);