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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,12 +1,13 @@
use bevy::{
ecs::system::{EntityCommand, EntityCommands},
ecs::{relationship::RelatedSpawnerCommands, system::EntityCommands},
gltf::{Gltf, GltfMesh},
math::{Quat, Vec3},
prelude::*,
};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct PrefabDefination {
pub struct PrefabDefination
{
pub path: String,
pub pos: Vec3,
pub rot: Vec3,
@@ -14,38 +15,54 @@ pub struct PrefabDefination {
pub animations: Option<Vec<AnimationComponent>>,
}
impl PrefabDefination {
pub fn spawn_recursive(&self, gltf: &Gltf, commands: &mut ChildBuilder, meshes: &Assets<GltfMesh>) {
impl PrefabDefination
{
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()];
if let Some(gltf_mesh) = meshes.get(mesh_handle.id()) {
let (m, mat) = gltf_mesh.unpack();
let mut entity = commands.spawn((
Mesh3d(m),
MeshMaterial3d(mat),
Transform::from_translation(self.pos).with_rotation(Quat::from_euler(
bevy::math::EulerRot::XYZ,
self.rot.x,
self.rot.y,
self.rot.z,
)),
));
if let Some(children) = &self.children {
entity.with_children(|b| {
for child in children {
child.spawn_recursive(gltf, b, meshes);
}
});
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((
Mesh3d(mesh),
MeshMaterial3d(mat),
Transform::from_translation(self.pos).with_rotation(Quat::from_euler(
bevy::math::EulerRot::XYZ,
self.rot.x,
self.rot.y,
self.rot.z,
)),
));
if let Some(children) = &self.children {
entity.with_children(|b| {
for child in children {
child.spawn_recursive(gltf, b, meshes);
}
});
}
}
}
}
}
pub trait UnpackGltfMesh {
pub trait UnpackGltfMesh
{
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>);
}
impl UnpackGltfMesh for GltfMesh {
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>) {
impl UnpackGltfMesh for GltfMesh
{
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>)
{
let p = &self.primitives[0];
let mut mat: Handle<StandardMaterial> = default();
if let Some(mesh_material) = &p.material {
@@ -56,19 +73,23 @@ impl UnpackGltfMesh for GltfMesh {
}
#[derive(Serialize, Deserialize, Debug)]
pub enum AnimationComponent {
pub enum AnimationComponent
{
Rotation(RotationAnimation),
Slider,
}
#[derive(Serialize, Deserialize, Debug, Component, Clone, Copy)]
pub struct RotationAnimation {
pub struct RotationAnimation
{
pub axis: Vec3,
pub speed: f32,
}
impl AnimationComponent {
pub fn apply(&self, commands: &mut EntityCommands) {
impl AnimationComponent
{
pub fn apply(&self, commands: &mut EntityCommands)
{
match self {
AnimationComponent::Rotation(comp) => {
commands.insert(comp.clone());

View File

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