WIP loading meshes for prefabs
Some checks failed
CI / Tests (push) Successful in 4m6s
CI / Bevy lints (push) Failing after 4m50s
CI / Clippy lints (push) Failing after 14m16s

This commit is contained in:
2025-07-22 22:56:57 -04:00
parent 6f15c9e9aa
commit 2616e32674
4 changed files with 36 additions and 20 deletions

View File

@@ -0,0 +1,17 @@
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use crate::prefab::ComponentBuilder;
#[derive(Serialize, Deserialize, Clone)]
pub struct PrefabMesh {
pub glb_path: String,
pub mesh_path: String,
}
#[typetag::serde]
impl ComponentBuilder for PrefabMesh {
fn build(&self, _entity: &mut EntityCommands, asset_server: &AssetServer) {
let glb: Handle<Scene> = asset_server.load(&self.glb_path);
}
}

View File

@@ -1,5 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
pub mod builders;
pub mod components; pub mod components;
pub mod prefab; pub mod prefab;
#[cfg(test)] #[cfg(test)]

View File

@@ -1,23 +1,32 @@
use bevy::{ecs::entity_disabling::Disabled, prelude::*}; use bevy::{ecs::entity_disabling::Disabled, prelude::*};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)] use crate::builders::PrefabMesh;
#[derive(Serialize, Deserialize, Default)]
pub struct PrefabAsset { pub struct PrefabAsset {
pub components: Vec<Box<dyn ComponentBuilder>>, pub components: Vec<Box<dyn ComponentBuilder>>,
pub children: Vec<PrefabAsset>,
pub mesh: Option<PrefabMesh>,
} }
impl PrefabAsset { impl PrefabAsset {
pub fn build_prefab(&self, commands: &mut Commands) -> Entity { pub fn build_prefab(&self, commands: &mut Commands, asset_server: &AssetServer) -> Entity {
let children: Vec<_> = self
.children
.iter()
.map(|c| c.build_prefab(commands, asset_server))
.collect();
let mut entity = commands.spawn(Disabled); let mut entity = commands.spawn(Disabled);
entity.add_children(&children[..]);
for builder in &self.components { for builder in &self.components {
builder.insert(&mut entity); builder.build(&mut entity, asset_server);
} }
return entity.id(); return entity.id();
} }
} }
#[typetag::serde(tag = "type")] #[typetag::serde(tag = "type")]
pub trait ComponentBuilder: Send { pub trait ComponentBuilder: Send {
fn insert(&self, entity: &mut EntityCommands); fn build(&self, entity: &mut EntityCommands, asset_server: &AssetServer);
} }

View File

@@ -1,7 +1,4 @@
use bevy::{ use bevy::prelude::*;
ecs::{component::Component, system::EntityCommands, world::World},
math::Vec3,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::prefab::{ComponentBuilder, PrefabAsset}; use crate::prefab::{ComponentBuilder, PrefabAsset};
@@ -12,7 +9,7 @@ struct Name {
} }
#[typetag::serde] #[typetag::serde]
impl ComponentBuilder for Name { impl ComponentBuilder for Name {
fn insert(&self, _entity: &mut EntityCommands) { fn build(&self, _entity: &mut EntityCommands, _: &AssetServer) {
assert_eq!(self.name, "Test".to_string(), "Name"); assert_eq!(self.name, "Test".to_string(), "Name");
} }
} }
@@ -24,7 +21,7 @@ struct Position {
#[typetag::serde] #[typetag::serde]
impl ComponentBuilder for Position { impl ComponentBuilder for Position {
fn insert(&self, _entity: &mut EntityCommands) { fn build(&self, _entity: &mut EntityCommands, _: &AssetServer) {
assert_eq!(self.pos, Vec3::X, "Position"); assert_eq!(self.pos, Vec3::X, "Position");
} }
} }
@@ -38,6 +35,7 @@ fn round_trip_test() {
name: "Test".to_string(), name: "Test".to_string(),
}), }),
], ],
..default()
}; };
let serialized = ron::to_string(&prefab); let serialized = ron::to_string(&prefab);
@@ -52,13 +50,4 @@ fn round_trip_test() {
prefab.components.len(), prefab.components.len(),
"Prefab's components count do not match" "Prefab's components count do not match"
); );
let mut world = World::new();
let e = world.spawn_empty().id();
let mut commands = world.commands();
let mut ec = commands.entity(e);
for component in loaded_asset.components {
component.insert(&mut ec);
}
} }