updated asset loader

prep for adding biomes
This commit is contained in:
2024-04-18 22:09:50 -04:00
parent 9fb305a887
commit df76dc7169
6 changed files with 60 additions and 19 deletions

View File

@@ -6,14 +6,36 @@ pub mod macros {
$plugin_name: ident, $plugin_name: ident,
$loader_name: ident, $loader_name: ident,
$asset_type: ident, $asset_type: ident,
$extensions: expr $extensions: expr,
$($string_name: ident -> $handle_name: ident)*
) => { ) => {
use bevy::prelude::*; use bevy::prelude::*;
use bevy::asset::{AssetLoader, AssetEvent, LoadContext, AsyncReadExt, io::Reader};
use bevy::utils::BoxedFuture;
pub struct $plugin_name; pub struct $plugin_name;
impl Plugin for $plugin_name { impl Plugin for $plugin_name {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_asset::<$asset_type>() 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<AssetEvent<$asset_type>>,
mut assets: ResMut<Assets<$asset_type>>,
asset_server: Res<AssetServer>
) {
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>( fn load<'a>(
&'a self, &'a self,
reader: &'a mut bevy::asset::io::Reader, reader: &'a mut Reader,
_settings: &'a Self::Settings, _settings: &'a Self::Settings,
_load_context: &'a mut bevy::asset::LoadContext, _load_context: &'a mut LoadContext,
) -> bevy::utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> { ) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
return Box::pin(async move { return Box::pin(async move {
let mut data: String = String::new(); let mut data: String = String::new();
let read_result = reader.read_to_string(&mut data).await; let read_result = reader.read_to_string(&mut data).await;

View File

@@ -33,7 +33,9 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed:
} }
} }
return Chunk { return Chunk {
points: result, heights: result,
moisture: result.clone(),
tempurature: result.clone(),
chunk_offset: IVec2::new(chunk_x as i32, chunk_z as i32), chunk_offset: IVec2::new(chunk_x as i32, chunk_z as i32),
}; };
} }

View File

@@ -128,15 +128,15 @@ impl HexCoord {
return IVec2::new(self.hex.x + (self.hex.y / 2), self.hex.y); return IVec2::new(self.hex.x + (self.hex.y / 2), self.hex.y);
} }
pub fn to_index(&self, width: usize) -> i32 { pub fn to_index(&self, width: usize) -> usize {
return (self.hex.x + self.hex.y * width as i32) + (self.hex.y / 2); 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(); 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); return self.to_chunk().to_index(Chunk::SIZE);
} }

View File

@@ -32,7 +32,9 @@ pub mod prelude {
} }
pub struct Chunk { 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, pub chunk_offset: IVec2,
} }
@@ -59,12 +61,27 @@ pub mod prelude {
continue; continue;
} }
let c_idx = n_tile.to_chunk_index(self.width); 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(); let local = n_tile.to_chunk_local_index();
results[i] = Some(chunk.points[local as usize]); results[i] = Some(chunk.heights[local]);
} }
return results; 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 = pub const ATTRIBUTE_TEXTURE_INDEX: MeshVertexAttribute =

View File

@@ -29,7 +29,7 @@ pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh {
for z in 0..Chunk::SIZE { for z in 0..Chunk::SIZE {
for x 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 off_pos = Vec3::new(x as f32, height, z as f32);
let tile_pos = offset3d_to_world(off_pos); let tile_pos = offset3d_to_world(off_pos);
let coord = HexCoord::from_offset( let coord = HexCoord::from_offset(

View File

@@ -1,6 +1,6 @@
use asset_loader::create_asset_loader; use asset_loader::create_asset_loader;
use bevy::{ use bevy::{
asset::{Asset, AssetLoader, AsyncReadExt}, asset::{Asset, Handle},
ecs::system::Resource, ecs::system::Resource,
reflect::TypePath, reflect::TypePath,
}; };
@@ -29,12 +29,12 @@ pub struct TileAsset {
#[serde(skip)] #[serde(skip)]
pub id: usize, pub id: usize,
pub name: String, pub name: String,
#[serde(skip)]
pub texture_id: u32, pub texture_id: u32,
#[serde(skip)]
pub texture: String, pub texture: String,
pub side_texture_id: u32,
#[serde(skip)] #[serde(skip)]
pub side_texture_id: u32,
pub side_texture: String, pub side_texture: String,
} }
create_asset_loader!(TileAssetPlugin, TileAssetLoader, TileAsset, &["tile.json"]); create_asset_loader!(TileAssetPlugin, TileAssetLoader, TileAsset, &["tile.json"],);