Resource Loading, Tile Mapper, and Biome Painter implementation
This commit is contained in:
@@ -7,7 +7,8 @@ pub mod macros {
|
||||
$loader_name: ident,
|
||||
$asset_type: ident,
|
||||
$extensions: expr,
|
||||
$($string_name: ident -> $handle_name: ident)*
|
||||
$($string_name: ident -> $handle_name: ident)* ;
|
||||
$($string_array_name: ident -> $handle_array_name: ident)* ?
|
||||
) => {
|
||||
use bevy::prelude::*;
|
||||
use bevy::asset::{AssetLoader, AssetEvent, LoadContext, AsyncReadExt, io::Reader};
|
||||
@@ -30,9 +31,15 @@ pub mod macros {
|
||||
match event {
|
||||
AssetEvent::LoadedWithDependencies { id } => {
|
||||
let asset = assets.get_mut(id.clone()).unwrap();
|
||||
|
||||
$(
|
||||
asset.$handle_name = asset_server.load(&asset.$string_name);
|
||||
)*
|
||||
$(
|
||||
for i in 0..asset.$string_array_name.len(){
|
||||
asset.$handle_array_name.push(asset_server.load(&asset.$string_array_name[i]));
|
||||
}
|
||||
)?
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
|
||||
33
engine/world_generation/src/biome_painter.rs
Normal file
33
engine/world_generation/src/biome_painter.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use asset_loader::create_asset_loader;
|
||||
use bevy::{
|
||||
asset::{Asset, Handle},
|
||||
reflect::TypePath,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::tile_mapper::TileMapperAsset;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, TypePath, Asset)]
|
||||
pub struct BiomePainterAsset {
|
||||
#[serde(skip)]
|
||||
pub biomes: Vec<Handle<TileMapperAsset>>,
|
||||
pub biomes_path: [String; 16],
|
||||
}
|
||||
|
||||
impl BiomePainterAsset {
|
||||
pub fn sample_biome(&self, moisture: f32, temperature: f32) -> Handle<TileMapperAsset> {
|
||||
let x = (moisture.clamp(0., 1.) * 4.).ceil() as usize;
|
||||
let y = (temperature.clamp(0., 1.) * 4.).ceil() as usize;
|
||||
return self.biomes[x + y * 4].clone();
|
||||
}
|
||||
}
|
||||
|
||||
create_asset_loader!(
|
||||
BiomePainterPlugin,
|
||||
BiomePainterLoader,
|
||||
BiomePainterAsset,
|
||||
&["bimoes.json"],
|
||||
;
|
||||
biomes_path -> biomes
|
||||
?
|
||||
);
|
||||
@@ -35,7 +35,7 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed:
|
||||
return Chunk {
|
||||
heights: result,
|
||||
moisture: result.clone(),
|
||||
tempurature: result.clone(),
|
||||
temperature: result.clone(),
|
||||
chunk_offset: IVec2::new(chunk_x as i32, chunk_z as i32),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod biome_painter;
|
||||
pub mod heightmap;
|
||||
pub mod hex_utils;
|
||||
pub mod mesh_generator;
|
||||
@@ -35,7 +36,7 @@ pub mod prelude {
|
||||
pub struct Chunk {
|
||||
pub heights: [f32; Chunk::SIZE * Chunk::SIZE],
|
||||
pub moisture: [f32; Chunk::SIZE * Chunk::SIZE],
|
||||
pub tempurature: [f32; Chunk::SIZE * Chunk::SIZE],
|
||||
pub temperature: [f32; Chunk::SIZE * Chunk::SIZE],
|
||||
pub chunk_offset: IVec2,
|
||||
}
|
||||
|
||||
@@ -81,7 +82,7 @@ pub mod prelude {
|
||||
|
||||
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()];
|
||||
return chunk.temperature[pos.to_chunk_local_index()];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,12 +29,12 @@ pub struct TileAsset {
|
||||
#[serde(skip)]
|
||||
pub id: usize,
|
||||
pub name: String,
|
||||
#[serde(skip)]
|
||||
pub texture_id: u32,
|
||||
pub texture: String,
|
||||
#[serde(skip)]
|
||||
pub texture: String,
|
||||
pub side_texture_id: u32,
|
||||
#[serde(skip)]
|
||||
pub side_texture: String,
|
||||
}
|
||||
|
||||
create_asset_loader!(TileAssetPlugin, TileAssetLoader, TileAsset, &["tile.json"],);
|
||||
create_asset_loader!(TileAssetPlugin, TileAssetLoader, TileAsset, &["tile.json"],;?);
|
||||
|
||||
@@ -11,15 +11,30 @@ use crate::tile_manager::TileAsset;
|
||||
pub struct TileMapper;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, TypePath, Asset)]
|
||||
struct TileMapperAsset {
|
||||
pub struct TileMapperAsset {
|
||||
#[serde(skip)]
|
||||
pub tiles: Vec<Handle<TileAsset>>,
|
||||
pub tiles_path: Vec<String>,
|
||||
pub thresholds: Vec<f32>,
|
||||
}
|
||||
|
||||
impl TileMapperAsset {
|
||||
pub fn sample_tile(&self, height: f32) -> Handle<TileAsset> {
|
||||
for i in 0..self.thresholds.len() {
|
||||
let t = self.thresholds[i];
|
||||
if t >= height {
|
||||
return self.tiles[i].clone();
|
||||
}
|
||||
}
|
||||
return self.tiles.last().unwrap().clone();
|
||||
}
|
||||
}
|
||||
|
||||
create_asset_loader!(
|
||||
TileMapperAssetPlugin,
|
||||
TileMapperAssetLoader,
|
||||
TileMapperAsset,
|
||||
&["mapper.json"],
|
||||
&["mapper.json"],;
|
||||
tiles_path -> tiles
|
||||
?
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user