migration to dynamic asset loading
This commit is contained in:
@@ -16,7 +16,7 @@ bevy_rapier3d = { version = "0.27.0", features = ["simd-stable", "parallel"] }
|
||||
rayon = "1.10.0"
|
||||
buildings = { path = "../buildings" }
|
||||
shared = { path = "../shared" }
|
||||
bevy_asset_loader = "0.21.0"
|
||||
bevy_asset_loader = {version ="0.21.0", features = ["standard_dynamic_assets", "3d"]}
|
||||
|
||||
[features]
|
||||
tracing = ["bevy/trace_tracy", "world_generation/tracing", "buildings/tracing"]
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
#[derive(AssetCollection, Serialize, Deserialize, Debug, TypePath, Asset, Clone)]
|
||||
pub struct BiomePainterAsset {
|
||||
#[asset(path)]
|
||||
pub biomes: Vec<Handle<BiomeAsset>>,
|
||||
}
|
||||
|
||||
impl BiomePainterAsset {
|
||||
pub fn sample_biome(&self, assets: &Assets<BiomeAsset>, data: &BiomeData) -> AssetId<BiomeAsset> {
|
||||
assert!(self.biomes.length() != 0, "There are no biomes");
|
||||
let mut biome = self.biomes.first().unwrap().id();
|
||||
let mut dist = f32::INFINITY;
|
||||
|
||||
for b in &self.biomes {
|
||||
let asset = assets.get(b.id()).unwrap();
|
||||
let d = asset.distance(data.into());
|
||||
if d < dist {
|
||||
biome = b.id();
|
||||
dist = d;
|
||||
}
|
||||
}
|
||||
|
||||
return biome;
|
||||
}
|
||||
|
||||
pub fn build(&self, assets: &Assets<BiomeAsset>) -> BiomePainter {
|
||||
let mut biomes = Vec::with_capacity(self.biomes.len());
|
||||
for b in &self.biomes {
|
||||
let asset = assets.get(b.id()).unwrap();
|
||||
biomes.push(asset.clone());
|
||||
}
|
||||
return BiomePainter { biomes };
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
pub mod biome_painer;
|
||||
@@ -11,7 +11,6 @@ mod phos;
|
||||
mod prelude;
|
||||
mod shader_extensions;
|
||||
mod utlis;
|
||||
pub mod assets;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
|
||||
@@ -5,6 +5,8 @@ use bevy::{
|
||||
pbr::{ExtendedMaterial, NotShadowCaster},
|
||||
prelude::*,
|
||||
};
|
||||
use bevy_asset_loader::prelude::*;
|
||||
|
||||
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
use shared::states::{AssetLoadState, GameplayState, MenuState};
|
||||
@@ -31,21 +33,18 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
use super::{
|
||||
chunk_rebuild::ChunkRebuildPlugin, prelude::CurrentBiomePainter, terraforming_test::TerraFormingTestPlugin,
|
||||
};
|
||||
use super::{chunk_rebuild::ChunkRebuildPlugin, terraforming_test::TerraFormingTestPlugin};
|
||||
|
||||
pub struct MapInitPlugin;
|
||||
|
||||
impl Plugin for MapInitPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_state(GeneratorState::Startup);
|
||||
app.insert_state(AssetLoadState::StartLoading);
|
||||
app.insert_state(AssetLoadState::Loading);
|
||||
|
||||
//Assets
|
||||
app.add_plugins(TileAssetPlugin);
|
||||
app.add_plugins(TileMapperAssetPlugin);
|
||||
app.add_plugins(BiomePainterPlugin);
|
||||
app.add_plugins(BiomeAssetPlugin);
|
||||
|
||||
app.add_plugins(ResourceInspectorPlugin::<GenerationConfig>::default());
|
||||
@@ -62,19 +61,33 @@ impl Plugin for MapInitPlugin {
|
||||
},
|
||||
));
|
||||
|
||||
//app.add_systems(Startup, (load_textures, load_tiles).in_set(AssetLoaderSet));
|
||||
//app.configure_sets(Startup, AssetLoaderSet.run_if(in_state(AssetLoadState::StartLoading)));
|
||||
app.configure_loading_state(
|
||||
LoadingStateConfig::new(AssetLoadState::Loading)
|
||||
.with_dynamic_assets_file::<StandardDynamicAssetCollection>("phos.assets.ron")
|
||||
.load_collection::<ChunkAtlas>(),
|
||||
);
|
||||
|
||||
app.add_systems(Startup, load_textures.run_if(in_state(AssetLoadState::FinalizeAssets)));
|
||||
|
||||
app.add_systems(
|
||||
Update,
|
||||
create_heightmap.run_if(in_state(GeneratorState::GenerateHeightmap)),
|
||||
);
|
||||
|
||||
app.add_systems(Update, check_asset_load.run_if(in_state(AssetLoadState::Loading)));
|
||||
// app.add_systems(
|
||||
// Update,
|
||||
// check_asset_load.run_if(in_state(AssetLoadState::FinalizeAssets)),
|
||||
// );
|
||||
app.add_systems(
|
||||
Update,
|
||||
(finalize_texture, finalize_biome_painter).run_if(in_state(AssetLoadState::FinalizeAssets)),
|
||||
);
|
||||
app.add_systems(
|
||||
Update,
|
||||
finalize_biome_painter
|
||||
.run_if(in_state(AssetLoadState::FinalizeAssets))
|
||||
.run_if(in_state(AssetLoadState::LoadComplete)),
|
||||
);
|
||||
app.add_systems(Update, despawn_map.run_if(in_state(GeneratorState::Regenerate)));
|
||||
app.add_systems(
|
||||
Update,
|
||||
@@ -87,9 +100,6 @@ impl Plugin for MapInitPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
struct AssetLoaderSet;
|
||||
|
||||
#[derive(Resource, Reflect, Default)]
|
||||
#[reflect(Resource)]
|
||||
struct WaterInspect(Handle<ExtendedMaterial<StandardMaterial, WaterMaterial>>);
|
||||
@@ -118,60 +128,16 @@ fn load_textures(
|
||||
atlas.water_material = water_material;
|
||||
}
|
||||
|
||||
fn load_tiles(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut next_state: ResMut<NextState<AssetLoadState>>,
|
||||
) {
|
||||
let handle: Handle<BiomePainterAsset> = asset_server.load("biome_painters/terra.biomes.json");
|
||||
commands.insert_resource(CurrentBiomePainter { handle });
|
||||
next_state.set(AssetLoadState::Loading);
|
||||
}
|
||||
|
||||
fn check_asset_load(
|
||||
asset_server: Res<AssetServer>,
|
||||
atlas: Res<ChunkAtlas>,
|
||||
painter: Res<CurrentBiomePainter>,
|
||||
painter_load: Res<BiomePainterLoadState>,
|
||||
biome_load: Res<BiomeAssetLoadState>,
|
||||
tile_load: Res<TileAssetLoadState>,
|
||||
mapper_load: Res<TileMapperLoadState>,
|
||||
mut next_state: ResMut<NextState<AssetLoadState>>,
|
||||
) {
|
||||
if !painter_load.is_all_loaded()
|
||||
|| !tile_load.is_all_loaded()
|
||||
|| !mapper_load.is_all_loaded()
|
||||
|| !biome_load.is_all_loaded()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if asset_server.load_state(atlas.handle.id()) != LoadState::Loaded {
|
||||
return;
|
||||
}
|
||||
if asset_server.load_state(painter.handle.id()) != LoadState::Loaded {
|
||||
return;
|
||||
}
|
||||
next_state.set(AssetLoadState::FinalizeAssets);
|
||||
}
|
||||
|
||||
fn finalize_biome_painter(
|
||||
mut commands: Commands,
|
||||
mut next_generator_state: ResMut<NextState<GeneratorState>>,
|
||||
painter: Res<CurrentBiomePainter>,
|
||||
painter_load: Res<BiomePainterLoadState>,
|
||||
biome_load: Res<BiomeAssetLoadState>,
|
||||
biome_painters: Res<Assets<BiomePainterAsset>>,
|
||||
biome_assets: Res<Assets<BiomeAsset>>,
|
||||
biome_painter: Res<BiomePainterAsset>,
|
||||
biomes: Res<Assets<BiomeAsset>>,
|
||||
) {
|
||||
if !painter_load.is_all_loaded() || !biome_load.is_all_loaded() {
|
||||
return;
|
||||
}
|
||||
|
||||
let painter_asset = biome_painters.get(painter.handle.id()).unwrap();
|
||||
let biome_painter = painter_asset.build(&biome_assets);
|
||||
let biome_painter = biome_painter.build(&biomes);
|
||||
commands.insert_resource(biome_painter);
|
||||
next_generator_state.set(GeneratorState::GenerateHeightmap);
|
||||
println!("Finalize Biome");
|
||||
}
|
||||
|
||||
fn finalize_texture(
|
||||
@@ -180,6 +146,7 @@ fn finalize_texture(
|
||||
mut chunk_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
|
||||
mut next_load_state: ResMut<NextState<AssetLoadState>>,
|
||||
) {
|
||||
println!("Finalize Tex");
|
||||
let image = images.get_mut(atlas.handle.id()).unwrap();
|
||||
|
||||
let array_layers = image.height() / image.width();
|
||||
|
||||
@@ -1,7 +1,2 @@
|
||||
use bevy::prelude::*;
|
||||
use world_generation::biome_painter::BiomePainterAsset;
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct CurrentBiomePainter {
|
||||
pub handle: Handle<BiomePainterAsset>,
|
||||
}
|
||||
|
||||
@@ -19,14 +19,6 @@ pub struct PhosGamePlugin;
|
||||
|
||||
impl Plugin for PhosGamePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins((
|
||||
PhosCameraPlugin,
|
||||
MapInitPlugin,
|
||||
RenderDistancePlugin,
|
||||
// BuildingPugin,
|
||||
DespawnPuglin,
|
||||
));
|
||||
|
||||
app.insert_state(AssetLoadState::Loading);
|
||||
app.insert_state(MenuState::Loading);
|
||||
app.insert_state(GameplayState::Waiting);
|
||||
@@ -35,6 +27,14 @@ impl Plugin for PhosGamePlugin {
|
||||
LoadingState::new(AssetLoadState::Loading).continue_to_state(AssetLoadState::FinalizeAssets),
|
||||
);
|
||||
|
||||
app.add_plugins((
|
||||
PhosCameraPlugin,
|
||||
MapInitPlugin,
|
||||
RenderDistancePlugin,
|
||||
// BuildingPugin,
|
||||
DespawnPuglin,
|
||||
));
|
||||
|
||||
//Systems - Startup
|
||||
app.add_systems(Startup, init_game);
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::shader_extensions::water_material::WaterMaterial;
|
||||
|
||||
#[derive(AssetCollection, Resource, Default)]
|
||||
pub struct ChunkAtlas {
|
||||
#[asset(path = "textures/world/Terra.png")]
|
||||
#[asset(key = "chunk_atlas")]
|
||||
pub handle: Handle<Image>,
|
||||
pub chunk_material_handle: Handle<ExtendedMaterial<StandardMaterial, ChunkMaterial>>,
|
||||
pub water_material: Handle<ExtendedMaterial<StandardMaterial, WaterMaterial>>,
|
||||
|
||||
Reference in New Issue
Block a user