WIP prefabs loader

This commit is contained in:
2025-07-20 13:58:37 -04:00
parent e3191e6827
commit babb74b2f7
37 changed files with 7099 additions and 407 deletions

12
engine/prefabs/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "prefabs"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = "0.16.1"
serde = "1.0.219"
typetag = "0.2.20"
[dev-dependencies]
ron = "0.10.1"

View File

@@ -0,0 +1,4 @@
use bevy::prelude::*;
#[derive(Component)]
pub struct PrefabId(pub Entity);

14
engine/prefabs/src/lib.rs Normal file
View File

@@ -0,0 +1,14 @@
use bevy::app::Plugin;
pub mod components;
pub mod prefab;
#[cfg(test)]
mod tests;
pub struct PrefabPlugin;
impl Plugin for PrefabPlugin {
fn build(&self, app: &mut bevy::app::App) {
todo!()
}
}

View File

@@ -0,0 +1,23 @@
use bevy::{ecs::entity_disabling::Disabled, prelude::*};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct PrefabAsset {
pub components: Vec<Box<dyn ComponentBuilder>>,
}
impl PrefabAsset {
pub fn build_prefab(&self, commands: &mut Commands) -> Entity {
let mut entity = commands.spawn(Disabled);
for builder in &self.components {
builder.insert(&mut entity);
}
return entity.id();
}
}
#[typetag::serde(tag = "type")]
pub trait ComponentBuilder {
fn insert(&self, entity: &mut EntityCommands);
}

View File

@@ -0,0 +1,52 @@
use bevy::{
ecs::{component::Component, system::EntityCommands},
math::{Vec3, VectorSpace},
render::view::PostProcessWrite,
};
use serde::{Deserialize, Serialize};
use crate::prefab::{ComponentBuilder, PrefabAsset};
#[derive(Component, Serialize, Deserialize)]
struct Name {
pub name: String,
}
#[typetag::serde]
impl ComponentBuilder for Name {
fn insert(&self, entity: &mut EntityCommands) {
todo!()
}
}
#[derive(Component, Serialize, Deserialize)]
struct Position {
pub pos: Vec3,
}
#[typetag::serde]
impl ComponentBuilder for Position {
fn insert(&self, entity: &mut EntityCommands) {
todo!()
}
}
#[test]
fn serialize_test() {
let prefab = PrefabAsset {
components: vec![
Box::new(Position { pos: Vec3::X }),
Box::new(Name {
name: "Test".to_string(),
}),
],
};
let serialized = ron::to_string(&prefab);
assert!(serialized.is_ok());
let deserialize = ron::from_str::<PrefabAsset>(&serialized.unwrap());
assert!(deserialize.is_ok());
let loaded_asset = deserialize.unwrap();
assert!(loaded_asset.components.len() == prefab.components.len());
}