WIP loading meshes for prefabs
This commit is contained in:
17
engine/prefabs/src/builders.rs
Normal file
17
engine/prefabs/src/builders.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub mod builders;
|
||||
pub mod components;
|
||||
pub mod prefab;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,23 +1,32 @@
|
||||
use bevy::{ecs::entity_disabling::Disabled, prelude::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
use crate::builders::PrefabMesh;
|
||||
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub struct PrefabAsset {
|
||||
pub components: Vec<Box<dyn ComponentBuilder>>,
|
||||
pub children: Vec<PrefabAsset>,
|
||||
pub mesh: Option<PrefabMesh>,
|
||||
}
|
||||
|
||||
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);
|
||||
entity.add_children(&children[..]);
|
||||
for builder in &self.components {
|
||||
builder.insert(&mut entity);
|
||||
builder.build(&mut entity, asset_server);
|
||||
}
|
||||
|
||||
return entity.id();
|
||||
}
|
||||
}
|
||||
|
||||
#[typetag::serde(tag = "type")]
|
||||
pub trait ComponentBuilder: Send {
|
||||
fn insert(&self, entity: &mut EntityCommands);
|
||||
fn build(&self, entity: &mut EntityCommands, asset_server: &AssetServer);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use bevy::{
|
||||
ecs::{component::Component, system::EntityCommands, world::World},
|
||||
math::Vec3,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::prefab::{ComponentBuilder, PrefabAsset};
|
||||
@@ -12,7 +9,7 @@ struct Name {
|
||||
}
|
||||
#[typetag::serde]
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -24,7 +21,7 @@ struct Position {
|
||||
|
||||
#[typetag::serde]
|
||||
impl ComponentBuilder for Position {
|
||||
fn insert(&self, _entity: &mut EntityCommands) {
|
||||
fn build(&self, _entity: &mut EntityCommands, _: &AssetServer) {
|
||||
assert_eq!(self.pos, Vec3::X, "Position");
|
||||
}
|
||||
}
|
||||
@@ -38,6 +35,7 @@ fn round_trip_test() {
|
||||
name: "Test".to_string(),
|
||||
}),
|
||||
],
|
||||
..default()
|
||||
};
|
||||
|
||||
let serialized = ron::to_string(&prefab);
|
||||
@@ -52,13 +50,4 @@ fn round_trip_test() {
|
||||
prefab.components.len(),
|
||||
"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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user