prefab loading
Building prefab loading
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::f32::consts::E;
|
||||
|
||||
use bevy::{ecs::world::CommandQueue, prelude::*, window::PrimaryWindow};
|
||||
use bevy::{ecs::world::CommandQueue, gltf::GltfMesh, prelude::*, window::PrimaryWindow};
|
||||
use bevy_asset_loader::loading_state::{
|
||||
config::{ConfigureLoadingState, LoadingStateConfig},
|
||||
LoadingStateAppExt,
|
||||
@@ -87,7 +87,6 @@ fn hq_placement(
|
||||
mut build_queue: ResMut<BuildQueue>,
|
||||
mut next_state: ResMut<NextState<GameplayState>>,
|
||||
) {
|
||||
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
let positions = map.hex_select(&contact.tile, 3, true, |pos, h, _| pos.to_world(h));
|
||||
show_indicators(positions, &mut commands, &indicator);
|
||||
@@ -122,6 +121,8 @@ fn process_build_queue(
|
||||
mut commands: Commands,
|
||||
db: Res<BuildingDatabase>,
|
||||
building_assets: Res<Assets<BuildingAsset>>,
|
||||
gltf_assets: Res<Assets<Gltf>>,
|
||||
gltf_meshes: Res<Assets<GltfMesh>>,
|
||||
mut building_map: ResMut<BuildingMap>,
|
||||
heightmap: Res<Map>,
|
||||
) {
|
||||
@@ -130,16 +131,14 @@ fn process_build_queue(
|
||||
if let Some(building) = building_assets.get(handle.id()) {
|
||||
let h = heightmap.sample_height(&item.pos);
|
||||
println!("Spawning {} at {}", building.name, item.pos);
|
||||
let e = commands.spawn((
|
||||
SceneBundle {
|
||||
scene: building.prefab.clone(),
|
||||
transform: Transform::from_translation(item.pos.to_world(h)),
|
||||
..Default::default()
|
||||
},
|
||||
Building,
|
||||
));
|
||||
|
||||
building_map.add_building(BuildingEntry::new(item.pos, e.id()));
|
||||
if let Some(gltf) = gltf_assets.get(building.prefab.id()) {
|
||||
let e = building.spawn(item.pos.to_world(h), Quat::IDENTITY, gltf, &mut commands, &gltf_meshes);
|
||||
if let Some(b) = e {
|
||||
building_map.add_building(BuildingEntry::new(item.pos, b));
|
||||
}
|
||||
} else {
|
||||
warn!("Failed to spawn building");
|
||||
}
|
||||
}
|
||||
}
|
||||
queue.queue.clear();
|
||||
|
||||
Reference in New Issue
Block a user