From df76dc716953b511594f225f342d9e4deaf096d4 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Thu, 18 Apr 2024 22:09:50 -0400 Subject: [PATCH] updated asset loader prep for adding biomes --- engine/asset_loader/src/lib.rs | 32 ++++++++++++++++--- engine/world_generation/src/heightmap.rs | 4 ++- engine/world_generation/src/hex_utils.rs | 10 +++--- engine/world_generation/src/lib.rs | 23 +++++++++++-- engine/world_generation/src/mesh_generator.rs | 2 +- engine/world_generation/src/tile_manager.rs | 8 ++--- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/engine/asset_loader/src/lib.rs b/engine/asset_loader/src/lib.rs index 88b34bb..8dc097d 100644 --- a/engine/asset_loader/src/lib.rs +++ b/engine/asset_loader/src/lib.rs @@ -6,14 +6,36 @@ pub mod macros { $plugin_name: ident, $loader_name: ident, $asset_type: ident, - $extensions: expr + $extensions: expr, + $($string_name: ident -> $handle_name: ident)* ) => { use bevy::prelude::*; + use bevy::asset::{AssetLoader, AssetEvent, LoadContext, AsyncReadExt, io::Reader}; + use bevy::utils::BoxedFuture; pub struct $plugin_name; impl Plugin for $plugin_name { fn build(&self, app: &mut App) { app.init_asset::<$asset_type>() - .init_asset_loader::<$loader_name>(); + .init_asset_loader::<$loader_name>() + .add_systems(Update, finalize); + } + } + + fn finalize( + mut asset_events: EventReader>, + mut assets: ResMut>, + asset_server: Res + ) { + for event in asset_events.read() { + match event { + AssetEvent::LoadedWithDependencies { id } => { + let asset = assets.get_mut(id.clone()).unwrap(); + $( + asset.$handle_name = asset_server.load(&asset.$string_name); + )* + }, + _ => (), + } } } @@ -29,10 +51,10 @@ pub mod macros { fn load<'a>( &'a self, - reader: &'a mut bevy::asset::io::Reader, + reader: &'a mut Reader, _settings: &'a Self::Settings, - _load_context: &'a mut bevy::asset::LoadContext, - ) -> bevy::utils::BoxedFuture<'a, Result> { + _load_context: &'a mut LoadContext, + ) -> BoxedFuture<'a, Result> { return Box::pin(async move { let mut data: String = String::new(); let read_result = reader.read_to_string(&mut data).await; diff --git a/engine/world_generation/src/heightmap.rs b/engine/world_generation/src/heightmap.rs index c8a6593..4b7cfe7 100644 --- a/engine/world_generation/src/heightmap.rs +++ b/engine/world_generation/src/heightmap.rs @@ -33,7 +33,9 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed: } } return Chunk { - points: result, + heights: result, + moisture: result.clone(), + tempurature: result.clone(), chunk_offset: IVec2::new(chunk_x as i32, chunk_z as i32), }; } diff --git a/engine/world_generation/src/hex_utils.rs b/engine/world_generation/src/hex_utils.rs index 7e2ebe8..dd9fd89 100644 --- a/engine/world_generation/src/hex_utils.rs +++ b/engine/world_generation/src/hex_utils.rs @@ -128,15 +128,15 @@ impl HexCoord { return IVec2::new(self.hex.x + (self.hex.y / 2), self.hex.y); } - pub fn to_index(&self, width: usize) -> i32 { - return (self.hex.x + self.hex.y * width as i32) + (self.hex.y / 2); + pub fn to_index(&self, width: usize) -> usize { + return ((self.hex.x + self.hex.y * width as i32) + (self.hex.y / 2)) as usize; } - pub fn to_chunk_index(&self, width: usize) -> i32 { + pub fn to_chunk_index(&self, width: usize) -> usize { let pos = self.to_chunk_pos(); - return pos.x + pos.y * width as i32; + return (pos.x + pos.y * width as i32) as usize; } - pub fn to_chunk_local_index(&self) -> i32 { + pub fn to_chunk_local_index(&self) -> usize { return self.to_chunk().to_index(Chunk::SIZE); } diff --git a/engine/world_generation/src/lib.rs b/engine/world_generation/src/lib.rs index 20ce9d8..ce246ae 100644 --- a/engine/world_generation/src/lib.rs +++ b/engine/world_generation/src/lib.rs @@ -32,7 +32,9 @@ pub mod prelude { } pub struct Chunk { - pub points: [f32; Chunk::SIZE * Chunk::SIZE], + pub heights: [f32; Chunk::SIZE * Chunk::SIZE], + pub moisture: [f32; Chunk::SIZE * Chunk::SIZE], + pub tempurature: [f32; Chunk::SIZE * Chunk::SIZE], pub chunk_offset: IVec2, } @@ -59,12 +61,27 @@ pub mod prelude { continue; } let c_idx = n_tile.to_chunk_index(self.width); - let chunk = &self.chunks[c_idx as usize]; + let chunk = &self.chunks[c_idx]; let local = n_tile.to_chunk_local_index(); - results[i] = Some(chunk.points[local as usize]); + results[i] = Some(chunk.heights[local]); } return results; } + + pub fn get_height(&self, pos: &HexCoord) -> f32 { + let chunk = &self.chunks[pos.to_chunk_index(self.width)]; + return chunk.heights[pos.to_chunk_local_index()]; + } + + pub fn get_moisture(&self, pos: &HexCoord) -> f32 { + let chunk = &self.chunks[pos.to_chunk_index(self.width)]; + return chunk.moisture[pos.to_chunk_local_index()]; + } + + pub fn get_tempurature(&self, pos: &HexCoord) -> f32 { + let chunk = &self.chunks[pos.to_chunk_index(self.width)]; + return chunk.tempurature[pos.to_chunk_local_index()]; + } } pub const ATTRIBUTE_TEXTURE_INDEX: MeshVertexAttribute = diff --git a/engine/world_generation/src/mesh_generator.rs b/engine/world_generation/src/mesh_generator.rs index 3d912d2..b49583c 100644 --- a/engine/world_generation/src/mesh_generator.rs +++ b/engine/world_generation/src/mesh_generator.rs @@ -29,7 +29,7 @@ pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh { for z in 0..Chunk::SIZE { for x in 0..Chunk::SIZE { - let height = chunk.points[x + z * Chunk::SIZE]; + let height = chunk.heights[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( diff --git a/engine/world_generation/src/tile_manager.rs b/engine/world_generation/src/tile_manager.rs index b3515ca..ea7a788 100644 --- a/engine/world_generation/src/tile_manager.rs +++ b/engine/world_generation/src/tile_manager.rs @@ -1,6 +1,6 @@ use asset_loader::create_asset_loader; use bevy::{ - asset::{Asset, AssetLoader, AsyncReadExt}, + asset::{Asset, Handle}, ecs::system::Resource, reflect::TypePath, }; @@ -29,12 +29,12 @@ pub struct TileAsset { #[serde(skip)] pub id: usize, pub name: String, + #[serde(skip)] pub texture_id: u32, - #[serde(skip)] pub texture: String, - pub side_texture_id: u32, #[serde(skip)] + pub side_texture_id: u32, pub side_texture: String, } -create_asset_loader!(TileAssetPlugin, TileAssetLoader, TileAsset, &["tile.json"]); +create_asset_loader!(TileAssetPlugin, TileAssetLoader, TileAsset, &["tile.json"],);