Fixed me stupidly deleting the fork

This commit is contained in:
Freyja-moth 2025-05-03 17:55:46 +01:00
parent 2affecdb07
commit a94bb1137c
2 changed files with 69 additions and 2 deletions

View File

@ -89,7 +89,7 @@ pub mod prelude {
apply_deferred, common_conditions::*, ApplyDeferred, Condition, IntoScheduleConfigs,
IntoSystemSet, Schedule, Schedules, SystemSet,
},
spawn::{Spawn, SpawnRelated},
spawn::{Spawn, SpawnIter, SpawnRelated, SpawnWith, WithOneRelated, WithRelated},
system::{
Command, Commands, Deferred, EntityCommand, EntityCommands, In, InMut, InRef,
IntoSystem, Local, NonSend, NonSendMut, ParamSet, Populated, Query, ReadOnlySystem,

View File

@ -133,6 +133,73 @@ impl<R: Relationship, F: FnOnce(&mut RelatedSpawner<R>) + Send + Sync + 'static>
}
}
/// A [`SpawnableList`] that adds entities using an iterator of [`Entity`]:
///
/// ```
/// # use bevy_ecs::hierarchy::Children;
/// # use bevy_ecs::spawn::{Spawn, WithRelated, SpawnRelated};
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::world::World;
/// let mut world = World::new();
///
/// let child2 = world.spawn(Name::new("Child2")).id();
/// let child3 = world.spawn(Name::new("Child3")).id();
///
/// world.spawn((
/// Name::new("Root"),
/// Children::spawn((
/// Spawn(Name::new("Child1")),
/// WithRelated([child2, child3].into_iter()),
/// )),
/// ));
/// ```
pub struct WithRelated<I>(pub I);
impl<R: Relationship, I: Iterator<Item = Entity>> SpawnableList<R> for WithRelated<I> {
fn spawn(self, world: &mut World, entity: Entity) {
world
.entity_mut(entity)
.add_related::<R>(&self.0.collect::<Vec<_>>());
}
fn size_hint(&self) -> usize {
self.0.size_hint().0
}
}
/// A wrapper over an [`Entity`] indicating that an entity should be added.
/// This is intended to be used for hierarchical spawning via traits like [`SpawnableList`] and [`SpawnRelated`].
///
/// Also see the [`children`](crate::children) and [`related`](crate::related) macros that abstract over the [`Spawn`] API.
///
/// ```
/// # use bevy_ecs::hierarchy::Children;
/// # use bevy_ecs::spawn::{Spawn, WithOneRelated, SpawnRelated};
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::world::World;
/// let mut world = World::new();
///
/// let child1 = world.spawn(Name::new("Child1")).id();
///
/// world.spawn((
/// Name::new("Root"),
/// Children::spawn((
/// WithOneRelated(child1),
/// )),
/// ));
/// ```
pub struct WithOneRelated(pub Entity);
impl<R: Relationship> SpawnableList<R> for WithOneRelated {
fn spawn(self, world: &mut World, entity: Entity) {
world.entity_mut(entity).add_one_related::<R>(self.0);
}
fn size_hint(&self) -> usize {
1
}
}
macro_rules! spawnable_list_impl {
($($list: ident),*) => {
#[expect(
@ -286,7 +353,7 @@ pub trait SpawnRelated: RelationshipTarget {
/// Returns a [`Bundle`] containing this [`RelationshipTarget`] component. It also spawns a [`SpawnableList`] of entities, each related to the bundle's entity
/// via [`RelationshipTarget::Relationship`]. The [`RelationshipTarget`] (when possible) will pre-allocate space for the related entities.
///
/// See [`Spawn`], [`SpawnIter`], and [`SpawnWith`] for usage examples.
/// See [`Spawn`], [`SpawnIter`], [`SpawnWith`], [`WithRelated`] and [`WithOneRelated`] for usage examples.
fn spawn<L: SpawnableList<Self::Relationship>>(
list: L,
) -> SpawnRelatedBundle<Self::Relationship, L>;