Skip to content

Commit

Permalink
feat: allow accessing entity builder children
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Dec 20, 2024
1 parent af418c9 commit 55e4b68
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
36 changes: 31 additions & 5 deletions src/entity/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ use crate::{
use alloc::{boxed::Box, vec::Vec};

type ModifyFunc = Box<dyn FnOnce(Entity, &mut EntityBuilder) + Send + Sync>;
struct Child {

/// Attached child builder with relation
pub struct ChildEntityBuilder {
builder: EntityBuilder,
modify: ModifyFunc,
}

impl core::fmt::Debug for Child {
impl core::ops::DerefMut for ChildEntityBuilder {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.builder
}
}

impl core::ops::Deref for ChildEntityBuilder {
type Target = EntityBuilder;

fn deref(&self) -> &Self::Target {
&self.builder
}
}

impl core::fmt::Debug for ChildEntityBuilder {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.builder.fmt(f)
}
}

impl Child {
impl ChildEntityBuilder {
fn spawn(mut self, world: &mut World, parent: Entity) -> Entity {
(self.modify)(parent, &mut self.builder);
self.builder.spawn(world)
Expand Down Expand Up @@ -48,7 +64,7 @@ impl Child {
/// ```
pub struct EntityBuilder {
buffer: ComponentBuffer,
children: Vec<Child>,
children: Vec<ChildEntityBuilder>,
}

impl EntityBuilder {
Expand Down Expand Up @@ -126,7 +142,7 @@ impl EntityBuilder {
value: T,
other: impl Into<Self>,
) -> &mut Self {
self.children.push(Child {
self.children.push(ChildEntityBuilder {
builder: other.into(),
modify: Box::new(move |parent, builder| {
builder.set(relation.of(parent), value);
Expand Down Expand Up @@ -204,6 +220,16 @@ impl EntityBuilder {
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}

/// Returns the attached child builders
pub fn children(&self) -> &[ChildEntityBuilder] {
&self.children
}

/// Returns the attached child builders
pub fn children_mut(&mut self) -> &mut Vec<ChildEntityBuilder> {
&mut self.children
}
}

impl Default for EntityBuilder {
Expand Down
4 changes: 2 additions & 2 deletions src/serialize/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct SerializeContext {
}

impl SerializeContext {
/// Construct a a new serializer context
/// Construct a new serializer context
pub fn builder() -> SerializeBuilder {
SerializeBuilder::new()
}
Expand All @@ -128,7 +128,7 @@ impl SerializeContext {
}

/// Serialize a single entity
pub fn serialize_entity<'a>(&'a self, entity: &'a EntityRef) -> EntitySerializer {
pub fn serialize_entity<'a>(&'a self, entity: &'a EntityRef) -> EntitySerializer<'a> {
EntitySerializer {
slot: entity.loc.slot,
arch: entity.arch,
Expand Down

0 comments on commit 55e4b68

Please sign in to comment.