building spawning update
Some checks failed
Rust / build (push) Failing after 4s

This commit is contained in:
2026-03-18 15:15:11 -04:00
parent 8bf58bb04c
commit ebae1599f1
9 changed files with 160 additions and 120 deletions

View File

@@ -1,17 +1,20 @@
use asset_loader::create_asset_loader; use asset_loader::create_asset_loader;
use bevy::{ use bevy::{
ecs::relationship::RelatedSpawnerCommands,
gltf::{GltfMesh, GltfNode}, gltf::{GltfMesh, GltfNode},
prelude::*, prelude::*,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use shared::identifiers::ResourceIdentifier; use shared::{component_defination::ComponentDefination, identifiers::ResourceIdentifier};
use crate::{ use crate::{
buildings::{ buildings::{
conduit_building::ResourceConduitInfo, factory_building::FactoryBuildingInfo, basic_building::BasicBuildingInfo, conduit_building::ResourceConduitInfo,
resource_gathering::ResourceGatheringBuildingInfo, factory_building::FactoryBuildingInfo, resource_gathering::ResourceGatheringBuildingInfo,
tech_building::TechBuildingInfo,
}, },
footprint::BuildingFootprint, footprint::BuildingFootprint,
prelude::Building,
}; };
#[derive(Asset, TypePath, Debug, Serialize, Deserialize)] #[derive(Asset, TypePath, Debug, Serialize, Deserialize)]
@@ -32,7 +35,7 @@ pub struct BuildingAsset
pub health: u32, pub health: u32,
pub building_type: BuildingType, pub building_type: BuildingType,
// pub components: Option<Vec<ComponentDefination>>, pub components: Option<Vec<ComponentDefination>>,
} }
impl BuildingAsset impl BuildingAsset
@@ -48,87 +51,101 @@ impl BuildingAsset
nodes: &Assets<GltfNode>, nodes: &Assets<GltfNode>,
) -> Option<Entity> ) -> Option<Entity>
{ {
todo!("Update building spawning"); let base_node = &gltf.named_nodes[&self.base_mesh_path.clone().into_boxed_str()];
// let base_node = &gltf.named_nodes[&self.base_mesh_path.clone().into_boxed_str()]; if let Some(node) = nodes.get(base_node.id()) {
// if let Some(node) = nodes.get(base_node.id()) { if let Some(mesh_handle) = &node.mesh {
// if let Some(mesh_handle) = &node.mesh { if let Some(gltf_mesh) = meshes.get(mesh_handle.id()) {
// if let Some(gltf_mesh) = meshes.get(mesh_handle.id()) { if let Some(primitive) = gltf_mesh.primitives.first() {
// let (mesh, mat) = gltf_mesh.unpack(); let mesh = primitive.mesh.clone();
// let mut entity = commands.spawn(( let mat = primitive
// Mesh3d(mesh), .material
// MeshMaterial3d(mat), .clone()
// Transform::from_translation(pos).with_rotation(rot), .expect(format!("Mesh '{}' does not have a meterial", primitive.name.as_str()).as_str());
// Building, let mut entity = commands.spawn((
// )); Mesh3d(mesh),
// entity.with_children(|b| { MeshMaterial3d(mat),
// for child in &node.children { Transform::from_translation(pos).with_rotation(rot),
// let child_node = nodes.get(child.id()); Building,
// if child_node.is_none() { ));
// continue; entity.with_children(|b| {
// } for child in &node.children {
// self.process_node(child_node.unwrap(), meshes, nodes, b, &node.name); let child_node = nodes.get(child.id());
// } if child_node.is_none() {
// }); continue;
// if let Some(component) = self.get_component_def(&format!("/{0}", &node.name)) { }
// component.apply(&mut entity); self.process_node(child_node.unwrap(), meshes, nodes, b, &node.name);
// } }
// return Some(entity.id()); });
// } if let Some(component) = self.get_component_def(&format!("/{0}", &node.name)) {
// } component.apply(&mut entity);
// } }
// return None; return Some(entity.id());
}
}
}
}
return None;
} }
// fn process_node( fn process_node(
// &self, &self,
// node: &GltfNode, node: &GltfNode,
// meshes: &Assets<GltfMesh>, meshes: &Assets<GltfMesh>,
// nodes: &Assets<GltfNode>, nodes: &Assets<GltfNode>,
// commands: &mut ChildBuilder, commands: &mut RelatedSpawnerCommands<ChildOf>,
// parent: &String, parent: &String,
// ) -> Option<Entity> { ) -> Option<Entity>
// let path = format!("{0}/{1}", parent, node.name); {
// if let Some(mesh) = &node.mesh { let path = format!("{0}/{1}", parent, node.name);
// if let Some(gltf_mesh) = meshes.get(mesh.id()) { if let Some(mesh) = &node.mesh {
// let (mesh, mat) = gltf_mesh.unpack(); if let Some(gltf_mesh) = meshes.get(mesh.id()) {
// let mut entity = commands.spawn((Mesh3d(mesh), MeshMaterial3d(mat), node.transform, Building)); if let Some(primitive) = gltf_mesh.primitives.first() {
// entity.with_children(|b| { let mesh = primitive.mesh.clone();
// for child in &node.children { let mat = primitive
// let child_node = nodes.get(child.id()); .material
// if child_node.is_none() { .clone()
// continue; .expect(format!("Mesh '{}' does not have a meterial", primitive.name.as_str()).as_str());
// } let mut entity = commands.spawn((Mesh3d(mesh), MeshMaterial3d(mat), node.transform, Building));
// self.process_node(child_node.unwrap(), meshes, nodes, b, &path); entity.with_children(|b| {
// } for child in &node.children {
// }); let child_node = nodes.get(child.id());
// if let Some(component) = self.get_component_def(&path) { if child_node.is_none() {
// component.apply(&mut entity); continue;
// } }
// return Some(entity.id()); self.process_node(child_node.unwrap(), meshes, nodes, b, &path);
// } }
// } });
// return None; if let Some(component) = self.get_component_def(&path) {
// } component.apply(&mut entity);
}
return Some(entity.id());
}
}
}
return None;
}
// fn get_component_def(&self, path: &String) -> Option<&ComponentDefination> { fn get_component_def(&self, path: &String) -> Option<&ComponentDefination>
// if let Some(components) = &self.components { {
// for c in components { if let Some(components) = &self.components {
// if c.path.ends_with(path) { for c in components {
// return Some(c); if c.path.ends_with(path) {
// } return Some(c);
// } }
// } }
// return None; }
// } return None;
}
} }
#[derive(Serialize, Deserialize, Debug, TypePath)] #[derive(Serialize, Deserialize, Debug, TypePath)]
pub enum BuildingType pub enum BuildingType
{ {
Basic, Basic(BasicBuildingInfo),
Gathering(ResourceGatheringBuildingInfo), Gathering(ResourceGatheringBuildingInfo),
FactoryBuildingInfo(FactoryBuildingInfo), FactoryBuildingInfo(FactoryBuildingInfo),
ResourceConduit(ResourceConduitInfo), ResourceConduit(ResourceConduitInfo),
Tech(TechBuildingInfo),
} }
create_asset_loader!( create_asset_loader!(

View File

@@ -1,3 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct BasicBuildingInfo {} pub struct BasicBuildingInfo
{
health: u32,
}

View File

@@ -32,6 +32,7 @@ hex = { path = "../../engine/hex" }
# bevy_lunex = "0.2.4" # bevy_lunex = "0.2.4"
[features] [features]
editor = []
tracing = [ tracing = [
"bevy/trace_tracy", "bevy/trace_tracy",
"world_generation/tracing", "world_generation/tracing",

View File

@@ -2,7 +2,6 @@ use crate::camera_system::components::PhosCamera;
use crate::map_rendering::map_init::MapInitPlugin; use crate::map_rendering::map_init::MapInitPlugin;
use crate::map_rendering::render_distance_system::RenderDistancePlugin; use crate::map_rendering::render_distance_system::RenderDistancePlugin;
use crate::ui::build_ui::BuildUIPlugin; use crate::ui::build_ui::BuildUIPlugin;
use crate::utlis::editor_plugin::EditorPlugin;
use crate::utlis::tile_selection_plugin::TileSelectionPlugin; use crate::utlis::tile_selection_plugin::TileSelectionPlugin;
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin}; use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
use bevy::diagnostic::{EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin}; use bevy::diagnostic::{EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin};
@@ -41,8 +40,8 @@ impl Plugin for PhosGamePlugin
// UnitsPlugin, // UnitsPlugin,
DespawnPuglin, DespawnPuglin,
TileSelectionPlugin, TileSelectionPlugin,
// #[cfg(debug_assertions)] #[cfg(feature = "editor")]
// EditorPlugin, crate::utlis::editor_plugin::EditorPlugin,
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
DebugPlugin, DebugPlugin,
)); ));
@@ -142,8 +141,7 @@ fn spawn_sphere(
mat: Res<SphereMat>, mat: Res<SphereMat>,
) )
{ {
if keyboard_input.just_pressed(KeyCode::KeyF) if keyboard_input.just_pressed(KeyCode::KeyF) {
{
commands.spawn(( commands.spawn((
Mesh3d(meshes.add(Sphere::new(0.3))), Mesh3d(meshes.add(Sphere::new(0.3))),
MeshMaterial3d(mat.0.clone()), MeshMaterial3d(mat.0.clone()),

View File

@@ -1,4 +1,5 @@
pub mod chunk_utils; pub mod chunk_utils;
pub mod debug_plugin; pub mod debug_plugin;
#[cfg(feature = "editor")]
pub mod editor_plugin; pub mod editor_plugin;
pub mod tile_selection_plugin; pub mod tile_selection_plugin;

View File

@@ -1,18 +1,18 @@
use bevy::{ use bevy::{ecs::system::EntityCommands, prelude::*};
ecs::system::EntityCommands, math::{Quat, Vec3}, prelude::*
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::prefab_defination::AnimationComponent; use crate::prefab_defination::AnimationComponent;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ComponentDefination { pub struct ComponentDefination
{
pub path: String, pub path: String,
pub animations: Vec<AnimationComponent>, pub animations: Vec<AnimationComponent>,
} }
impl ComponentDefination
impl ComponentDefination { {
pub fn apply(&self, commands: &mut EntityCommands){ pub fn apply(&self, commands: &mut EntityCommands)
{
for c in &self.animations { for c in &self.animations {
c.apply(commands); c.apply(commands);
} }

View File

@@ -1,18 +1,18 @@
use bevy::reflect::Reflect; use bevy::reflect::Reflect;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub mod animation_plugin;
pub mod building; pub mod building;
pub mod component_defination;
pub mod coords; pub mod coords;
pub mod despawn; pub mod despawn;
pub mod events; pub mod events;
pub mod identifiers; pub mod identifiers;
pub mod prefab_defination;
pub mod resources; pub mod resources;
pub mod sets; pub mod sets;
pub mod states; pub mod states;
pub mod tags; pub mod tags;
// pub mod prefab_defination;
// pub mod animation_plugin;
// pub mod component_defination;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub enum Tier pub enum Tier

View File

@@ -1,12 +1,13 @@
use bevy::{ use bevy::{
ecs::system::{EntityCommand, EntityCommands}, ecs::{relationship::RelatedSpawnerCommands, system::EntityCommands},
gltf::{Gltf, GltfMesh}, gltf::{Gltf, GltfMesh},
math::{Quat, Vec3}, math::{Quat, Vec3},
prelude::*, prelude::*,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct PrefabDefination { pub struct PrefabDefination
{
pub path: String, pub path: String,
pub pos: Vec3, pub pos: Vec3,
pub rot: Vec3, pub rot: Vec3,
@@ -14,13 +15,25 @@ pub struct PrefabDefination {
pub animations: Option<Vec<AnimationComponent>>, pub animations: Option<Vec<AnimationComponent>>,
} }
impl PrefabDefination { impl PrefabDefination
pub fn spawn_recursive(&self, gltf: &Gltf, commands: &mut ChildBuilder, meshes: &Assets<GltfMesh>) { {
pub fn spawn_recursive(
&self,
gltf: &Gltf,
commands: &mut RelatedSpawnerCommands<ChildOf>,
meshes: &Assets<GltfMesh>,
)
{
let mesh_handle = &gltf.named_meshes[&self.path.clone().into_boxed_str()]; let mesh_handle = &gltf.named_meshes[&self.path.clone().into_boxed_str()];
if let Some(gltf_mesh) = meshes.get(mesh_handle.id()) { if let Some(gltf_mesh) = meshes.get(mesh_handle.id()) {
let (m, mat) = gltf_mesh.unpack(); if let Some(primitive) = gltf_mesh.primitives.first() {
let mesh = primitive.mesh.clone();
let mat = primitive
.material
.clone()
.expect(format!("Mesh '{}' does not have a meterial", primitive.name.as_str()).as_str());
let mut entity = commands.spawn(( let mut entity = commands.spawn((
Mesh3d(m), Mesh3d(mesh),
MeshMaterial3d(mat), MeshMaterial3d(mat),
Transform::from_translation(self.pos).with_rotation(Quat::from_euler( Transform::from_translation(self.pos).with_rotation(Quat::from_euler(
bevy::math::EulerRot::XYZ, bevy::math::EulerRot::XYZ,
@@ -38,14 +51,18 @@ impl PrefabDefination {
} }
} }
} }
}
} }
pub trait UnpackGltfMesh { pub trait UnpackGltfMesh
{
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>); fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>);
} }
impl UnpackGltfMesh for GltfMesh { impl UnpackGltfMesh for GltfMesh
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>) { {
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>)
{
let p = &self.primitives[0]; let p = &self.primitives[0];
let mut mat: Handle<StandardMaterial> = default(); let mut mat: Handle<StandardMaterial> = default();
if let Some(mesh_material) = &p.material { if let Some(mesh_material) = &p.material {
@@ -56,19 +73,23 @@ impl UnpackGltfMesh for GltfMesh {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub enum AnimationComponent { pub enum AnimationComponent
{
Rotation(RotationAnimation), Rotation(RotationAnimation),
Slider, Slider,
} }
#[derive(Serialize, Deserialize, Debug, Component, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, Component, Clone, Copy)]
pub struct RotationAnimation { pub struct RotationAnimation
{
pub axis: Vec3, pub axis: Vec3,
pub speed: f32, pub speed: f32,
} }
impl AnimationComponent { impl AnimationComponent
pub fn apply(&self, commands: &mut EntityCommands) { {
pub fn apply(&self, commands: &mut EntityCommands)
{
match self { match self {
AnimationComponent::Rotation(comp) => { AnimationComponent::Rotation(comp) => {
commands.insert(comp.clone()); commands.insert(comp.clone());

View File

@@ -1,4 +1,3 @@
hard_tabs = true hard_tabs = true
max_width = 120 max_width = 120
brace_style = "AlwaysNextLine" brace_style = "AlwaysNextLine"
control_brace_style = "AlwaysNextLine"