prefab loading

Building prefab loading
This commit is contained in:
2024-11-18 22:35:18 -05:00
parent 4cfe2d9079
commit 953650e394
6 changed files with 121 additions and 25 deletions

View File

@@ -1,14 +1,15 @@
use asset_loader::create_asset_loader;
use bevy::prelude::*;
use bevy::{gltf::GltfMesh, prelude::*};
use serde::{Deserialize, Serialize};
use shared::identifiers::ResourceIdentifier;
use shared::{identifiers::ResourceIdentifier, prefab_defination::*};
use crate::{
buildings::{
basic_building::BasicBuildingInfo, conduit_building::ResourceConduitInfo,
factory_building::FactoryBuildingInfo, resource_gathering::ResourceGatheringBuildingInfo,
conduit_building::ResourceConduitInfo, factory_building::FactoryBuildingInfo,
resource_gathering::ResourceGatheringBuildingInfo,
},
footprint::BuildingFootprint,
prelude::Building,
};
#[derive(Asset, TypePath, Debug, Serialize, Deserialize)]
@@ -18,7 +19,8 @@ pub struct BuildingAsset {
pub footprint: BuildingFootprint,
pub prefab_path: String,
#[serde(skip)]
pub prefab: Handle<Scene>,
pub prefab: Handle<Gltf>,
pub base_mesh_path: String,
pub cost: Vec<ResourceIdentifier>,
pub consumption: Vec<ResourceIdentifier>,
@@ -27,7 +29,44 @@ pub struct BuildingAsset {
pub health: u32,
pub building_type: BuildingType,
pub animations: Vec<AnimationComponent>,
pub children: Option<Vec<PrefabDefination>>,
}
impl BuildingAsset {
pub fn spawn(
&self,
pos: Vec3,
rot: Quat,
gltf: &Gltf,
commands: &mut Commands,
meshes: &Assets<GltfMesh>,
) -> Option<Entity> {
let keys: Vec<_> = gltf.named_meshes.keys().collect();
info!("{keys:#?}");
let mesh_handle = &gltf.named_meshes[&self.base_mesh_path.clone().into_boxed_str()];
if let Some(gltf_mesh) = meshes.get(mesh_handle.id()) {
let (mesh, mat) = gltf_mesh.unpack();
let mut entity = commands.spawn((
PbrBundle {
mesh,
material: mat,
transform: Transform::from_translation(pos)
.with_rotation(Quat::from_rotation_arc(Vec3::NEG_Z, Vec3::Y) * rot),
..Default::default()
},
Building,
));
if let Some(children) = &self.children {
entity.with_children(|b| {
for child in children {
child.spawn_recursive(gltf, b, meshes);
}
});
}
return Some(entity.id());
}
return None;
}
}
#[derive(Serialize, Deserialize, Debug, TypePath)]
@@ -38,11 +77,6 @@ pub enum BuildingType {
ResourceConduit(ResourceConduitInfo),
}
#[derive(Serialize, Deserialize, Debug, Reflect)]
pub enum AnimationComponent {
Rotation,
Slider,
}
create_asset_loader!(
BuildingAssetPlugin,
BuildingAssetLoader,