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

873
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,58 +1,3 @@
[package]
name = "space-game"
version = "0.1.0"
edition = "2024"
[dependencies]
# avian3d = { version = "0.3.0", features = [] }
bevy = { version = "0.16.1", features = [] }
# bevy_rapier3d = { version = "0.29.0", features = ["simd-stable", "parallel"] }
bevy-inspector-egui = "0.31.0"
bevy_asset_loader = "0.23.0"
bevy_rapier3d = "0.30.0"
[features]
default = ["dev"]
dev = [
"bevy/bevy_dev_tools",
"bevy/bevy_ui_debug",
"bevy/track_location",
"bevy/file_watcher",
"bevy/embedded_watcher",
"dev-viz"
]
dev-viz = []
dev-phys = []
[lints.clippy]
# Bevy supplies arguments to systems via dependency injection, so it's natural for systems to
# request more than 7 arguments, which would undesirably trigger this lint.
too_many_arguments = "allow"
# Queries may access many components, which would undesirably trigger this lint.
type_complexity = "allow"
# Make sure macros use their standard braces, such as `[]` for `bevy_ecs::children!`.
nonstandard_macro_braces = "warn"
needless_return = "allow"
# Enable a small amount of optimization in debug mode
[profile.dev]
opt-level = 1
# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
[profile.dev.package."*"]
opt-level = 3
[profile.release]
codegen-units = 1
lto = "thin"
# Optimize for build time in CI.
[profile.ci]
inherits = "dev"
opt-level = 0
debug = "line-tables-only"
codegen-units = 4
[profile.ci.package."*"]
opt-level = 0
[workspace]
resolver = "2"
members = [ "engine/prefabs","game/main"]

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());
}

6408
game/main/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

59
game/main/Cargo.toml Normal file
View File

@@ -0,0 +1,59 @@
[package]
name = "space-game"
version = "0.1.0"
edition = "2024"
[dependencies]
# avian3d = { version = "0.3.0", features = [] }
bevy = { version = "0.16.1", features = [] }
# bevy_rapier3d = { version = "0.29.0", features = ["simd-stable", "parallel"] }
bevy-inspector-egui = "0.31.0"
bevy_asset_loader = "0.23.0"
bevy_rapier3d = "0.30.0"
prefabs = { path = "../../engine/prefabs" }
[features]
default = ["dev"]
dev = [
"bevy/bevy_dev_tools",
"bevy/bevy_ui_debug",
"bevy/track_location",
"bevy/file_watcher",
"bevy/embedded_watcher",
"dev-viz"
]
dev-viz = []
dev-phys = []
[lints.clippy]
# Bevy supplies arguments to systems via dependency injection, so it's natural for systems to
# request more than 7 arguments, which would undesirably trigger this lint.
too_many_arguments = "allow"
# Queries may access many components, which would undesirably trigger this lint.
type_complexity = "allow"
# Make sure macros use their standard braces, such as `[]` for `bevy_ecs::children!`.
nonstandard_macro_braces = "warn"
needless_return = "allow"
# Enable a small amount of optimization in debug mode
[profile.dev]
opt-level = 1
# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
[profile.dev.package."*"]
opt-level = 3
[profile.release]
codegen-units = 1
lto = "thin"
# Optimize for build time in CI.
[profile.ci]
inherits = "dev"
opt-level = 0
debug = "line-tables-only"
codegen-units = 4
[profile.ci.package."*"]
opt-level = 0