diff --git a/game/buildings/src/assets/building_asset.rs b/game/buildings/src/assets/building_asset.rs index 5dbbee6..f65f09c 100644 --- a/game/buildings/src/assets/building_asset.rs +++ b/game/buildings/src/assets/building_asset.rs @@ -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>, + pub components: Option>, } impl BuildingAsset @@ -48,87 +51,101 @@ impl BuildingAsset nodes: &Assets, ) -> Option { - 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, - // nodes: &Assets, - // commands: &mut ChildBuilder, - // parent: &String, - // ) -> Option { - // 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, + nodes: &Assets, + commands: &mut RelatedSpawnerCommands, + parent: &String, + ) -> Option + { + 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!( diff --git a/game/buildings/src/buildings/basic_building.rs b/game/buildings/src/buildings/basic_building.rs index 1b435f9..b15879e 100644 --- a/game/buildings/src/buildings/basic_building.rs +++ b/game/buildings/src/buildings/basic_building.rs @@ -1,3 +1,6 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] -pub struct BasicBuildingInfo {} +pub struct BasicBuildingInfo +{ + health: u32, +} diff --git a/game/main/Cargo.toml b/game/main/Cargo.toml index 3c62276..19d83e4 100644 --- a/game/main/Cargo.toml +++ b/game/main/Cargo.toml @@ -32,6 +32,7 @@ hex = { path = "../../engine/hex" } # bevy_lunex = "0.2.4" [features] +editor = [] tracing = [ "bevy/trace_tracy", "world_generation/tracing", diff --git a/game/main/src/phos.rs b/game/main/src/phos.rs index c1501d3..33d34b5 100644 --- a/game/main/src/phos.rs +++ b/game/main/src/phos.rs @@ -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, ) { - 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()), diff --git a/game/main/src/utlis/mod.rs b/game/main/src/utlis/mod.rs index 8fc95da..25e4ba8 100644 --- a/game/main/src/utlis/mod.rs +++ b/game/main/src/utlis/mod.rs @@ -1,4 +1,5 @@ pub mod chunk_utils; pub mod debug_plugin; +#[cfg(feature = "editor")] pub mod editor_plugin; pub mod tile_selection_plugin; diff --git a/game/shared/src/component_defination.rs b/game/shared/src/component_defination.rs index 4bbd724..25ef84d 100644 --- a/game/shared/src/component_defination.rs +++ b/game/shared/src/component_defination.rs @@ -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, } - -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); } } -} \ No newline at end of file +} diff --git a/game/shared/src/lib.rs b/game/shared/src/lib.rs index fc1eb1d..0cb5f49 100644 --- a/game/shared/src/lib.rs +++ b/game/shared/src/lib.rs @@ -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 diff --git a/game/shared/src/prefab_defination.rs b/game/shared/src/prefab_defination.rs index 5c07e66..0247050 100644 --- a/game/shared/src/prefab_defination.rs +++ b/game/shared/src/prefab_defination.rs @@ -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>, } -impl PrefabDefination { - pub fn spawn_recursive(&self, gltf: &Gltf, commands: &mut ChildBuilder, meshes: &Assets) { +impl PrefabDefination +{ + pub fn spawn_recursive( + &self, + gltf: &Gltf, + commands: &mut RelatedSpawnerCommands, + meshes: &Assets, + ) + { 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, Handle); } -impl UnpackGltfMesh for GltfMesh { - fn unpack(&self) -> (Handle, Handle) { +impl UnpackGltfMesh for GltfMesh +{ + fn unpack(&self) -> (Handle, Handle) + { let p = &self.primitives[0]; let mut mat: Handle = 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()); diff --git a/rustfmt.toml b/rustfmt.toml index cca1e7a..fb9eaa6 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,4 +1,3 @@ hard_tabs = true max_width = 120 brace_style = "AlwaysNextLine" -control_brace_style = "AlwaysNextLine" \ No newline at end of file