Resource Loading, Tile Mapper, and Biome Painter implementation

This commit is contained in:
2024-04-19 23:04:29 -04:00
parent b1dc3b9aef
commit d40c35e891
8 changed files with 74 additions and 19 deletions

View 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
?
);

View File

@@ -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),
};
}

View File

@@ -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()];
}
}

View File

@@ -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"],;?);

View File

@@ -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
?
);