fully implement bevy asset loader
This commit is contained in:
@@ -13,7 +13,11 @@ bevy_rapier3d = "0.27.0"
|
||||
serde = { version = "1.0.204", features = ["derive"] }
|
||||
asset_loader = { path = "../../engine/asset_loader" }
|
||||
serde_json = "1.0.120"
|
||||
|
||||
ron = "0.8.1"
|
||||
bevy_asset_loader = { version = "0.21.0", features = [
|
||||
"standard_dynamic_assets",
|
||||
"3d",
|
||||
] }
|
||||
|
||||
[features]
|
||||
tracing = []
|
||||
|
||||
@@ -23,8 +23,7 @@ create_asset_loader!(
|
||||
BuildingAssetPlugin,
|
||||
BuildingAssetLoader,
|
||||
BuildingAsset,
|
||||
BuildingAssetLoadState,
|
||||
&["building.json"],
|
||||
&["building", "building.ron"],
|
||||
prefab_path -> prefab
|
||||
;?
|
||||
);
|
||||
|
||||
@@ -1,28 +1,11 @@
|
||||
use asset_loader::create_asset_loader;
|
||||
use bevy::prelude::Resource;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use bevy::{asset::Handle, prelude::Resource};
|
||||
|
||||
use super::building_asset::BuildingAsset;
|
||||
use bevy::prelude::*;
|
||||
use bevy_asset_loader::prelude::*;
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct BuildingDatabase{
|
||||
pub handle: Handle<BuildingDatabaseAsset>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, TypePath, Asset)]
|
||||
pub struct BuildingDatabaseAsset {
|
||||
pub hq: u32,
|
||||
pub buildings_paths: Vec<String>,
|
||||
#[serde(skip)]
|
||||
#[derive(Resource, AssetCollection)]
|
||||
pub struct BuildingDatabase {
|
||||
#[asset(key = "buildings", collection(typed))]
|
||||
pub buildings: Vec<Handle<BuildingAsset>>,
|
||||
}
|
||||
|
||||
create_asset_loader!(
|
||||
BuildingDatabasePlugin,
|
||||
BuildingDatabaseLoader,
|
||||
BuildingDatabaseAsset,
|
||||
BuildingDatabaseState,
|
||||
&["buildings.db.json"],;
|
||||
buildings_paths -> buildings
|
||||
?
|
||||
);
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy_asset_loader::loading_state::{
|
||||
config::{ConfigureLoadingState, LoadingStateConfig},
|
||||
LoadingStateAppExt,
|
||||
};
|
||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
||||
use shared::{
|
||||
despawn::Despawn,
|
||||
@@ -8,10 +12,7 @@ use shared::{
|
||||
use world_generation::{hex_utils::HexCoord, map::map::Map};
|
||||
|
||||
use crate::{
|
||||
assets::{
|
||||
building_asset::BuildingAssetPlugin,
|
||||
building_database::{BuildingDatabase, BuildingDatabasePlugin},
|
||||
},
|
||||
assets::{building_asset::BuildingAssetPlugin, building_database::BuildingDatabase},
|
||||
build_queue::{BuildQueue, QueueEntry},
|
||||
};
|
||||
|
||||
@@ -20,7 +21,11 @@ pub struct BuildingPugin;
|
||||
impl Plugin for BuildingPugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(BuildQueue::default());
|
||||
app.add_plugins(BuildingAssetPlugin).add_plugins(BuildingDatabasePlugin);
|
||||
app.add_plugins(BuildingAssetPlugin);
|
||||
|
||||
app.configure_loading_state(
|
||||
LoadingStateConfig::new(AssetLoadState::Loading).load_collection::<BuildingDatabase>(),
|
||||
);
|
||||
|
||||
app.add_systems(Startup, init.run_if(in_state(AssetLoadState::Loading)));
|
||||
app.add_systems(Update, hq_placement.run_if(in_state(GameplayState::PlaceHQ)));
|
||||
@@ -32,19 +37,11 @@ impl Plugin for BuildingPugin {
|
||||
#[derive(Resource)]
|
||||
struct IndicatorCube(Handle<Mesh>, Handle<StandardMaterial>);
|
||||
|
||||
fn init(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
) {
|
||||
fn init(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>) {
|
||||
let cube = Cuboid::from_size(Vec3::splat(1.));
|
||||
let mesh_handle = meshes.add(cube);
|
||||
let mat_handle = materials.add(Color::WHITE);
|
||||
commands.insert_resource(IndicatorCube(mesh_handle, mat_handle));
|
||||
|
||||
let db = asset_server.load("buildings/buildings.db.json");
|
||||
commands.insert_resource(BuildingDatabase { handle: db });
|
||||
}
|
||||
|
||||
fn hq_placement(
|
||||
@@ -56,7 +53,7 @@ fn hq_placement(
|
||||
map: Res<Map>,
|
||||
indicator: Res<IndicatorCube>,
|
||||
mut build_queue: ResMut<BuildQueue>,
|
||||
next_state: ResMut<NextState<GameplayState>>,
|
||||
mut next_state: ResMut<NextState<GameplayState>>,
|
||||
) {
|
||||
let win = window.single();
|
||||
let (cam_transform, camera) = cam_query.single();
|
||||
@@ -87,6 +84,8 @@ fn hq_placement(
|
||||
building: 0.into(),
|
||||
pos: contact_coord,
|
||||
});
|
||||
|
||||
next_state.set(GameplayState::Playing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user