๐Ÿ“ฆ Zeenobit / moonshine_spawn

A lightweight spawn utility for Bevy

โ˜… 12 stars โ‘‚ 0 forks ๐Ÿ‘ 12 watching โš–๏ธ MIT License
๐Ÿ“ฅ Clone https://github.com/Zeenobit/moonshine_spawn.git
HTTPS git clone https://github.com/Zeenobit/moonshine_spawn.git
SSH git clone git@github.com:Zeenobit/moonshine_spawn.git
CLI gh repo clone Zeenobit/moonshine_spawn
Zeenobit Zeenobit RIP ๐Ÿชฆ 7e13e2a 1 years ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“ src
๐Ÿ“„ .gitattributes
๐Ÿ“„ .gitignore
๐Ÿ“„ Cargo.toml
๐Ÿ“„ LICENSE
๐Ÿ“„ README.md
๐Ÿ“„ README.md

๐Ÿฅš Moonshine Spawn

crates.io downloads docs.rs license stars

Collection of tools for spawning entities in Bevy.

โš ๏ธ Deprecated

This crate is deprecated in favor of the upcoming BSN changes to Bevy.

In the meantime, prefer to use i-can't-believe-its-not-bsn as it is a closer implementation to BSN and will make future migration easier.

The only feature of this library that is not covered by BSN are spawn keys. Implementation of spawn keys on user code should be trivial, especially with ๐Ÿท๏ธ Moonshine Tag.

Overview

In Bevy, complex hierarchies of entities are typically spawned using the ChildBuilder:

use bevy::prelude::*;

fn spawn_chicken(commands: &mut Commands) -> Entity {
    // Spawn logic is spread between this function and the bundle
    commands.spawn(ChickenBundle { /* Components */ }).with_children(|chicken| {
        // Children
    })
    .id()
}

#[derive(Bundle)]
struct ChickenBundle {
    // ...
}

While this pattern works for most cases, it tends to spread out the logic of entity spawning between the bundle and the function which builds the entity hierarchy. Arguably, this makes the code less modular and harder to read and maintain.

Additionally, there is no built-in functionality to reference a predefined entity hierarchy (i.e. a "prefab").

This crate aims to solve some of these issues by providing tools to make spawning more ergonomic:

use bevy::prelude::*;
use moonshine_spawn::prelude::*;

let mut app = App::new();
// Make sure `SpawnPlugin` is added to your `App`:
app.add_plugins((DefaultPlugins, SpawnPlugin));

// Register spawnables during initialization:
let chicken_key: SpawnKey = app.add_spawnable("chicken", Chicken);

// Spawn a spawnable with a key:
let chicken = app.world_mut().spawn_key(chicken_key); // .spawn_key("chicken") also works!

#[derive(Component)]
struct Chicken;

impl Spawn for Chicken {
    type Output = (Chicken, SpawnChildren);

    // Spawn logic is now unified:
    fn spawn(&self, world: &World, entity: Entity) -> Self::Output {
        // Components
        (Chicken,
        spawn_children(|chicken| {
            // Children
        }))
    }
}

Usage

Spawnables

A type is a "spawnable" if it implements either [Spawn] or [SpawnOnce]:

``rust,ignore trait Spawn { type Output; fn spawn(&self, world: &World, entity: Entity) -> Self::Output; } trait SpawnOnce { type Output; fn spawn_once(self, world: &World, entity: Entity) -> Self::Output; } %%CODEBLOCK2%%rust use bevy::prelude::*; use moonshine_spawn::prelude::*; #[derive(Resource)] struct DefaultChickenName(Name); struct Egg; impl SpawnOnce for Egg { type Output = ChickenBundle; fn spawn_once(self, world: &World, entity: Entity) -> Self::Output { let DefaultChickenName(name) = world.resource::<DefaultChickenName>(); ChickenBundle::new(name.clone()) } } #[derive(Bundle)] struct ChickenBundle { chicken: Chicken, name: Name, } impl ChickenBundle { fn new(name: Name) -> Self { Self { chicken: Chicken, name, } } } #[derive(Component)] struct Chicken; fn open_egg(egg: Egg, commands: &mut Commands) -> Entity { commands.spawn_once_with(egg).id() } %%CODEBLOCK3%%rust use bevy::prelude::*; use moonshine_spawn::prelude::*; #[derive(Component)] struct Chicken; fn chicken() -> impl SpawnOnce { Chicken.with_children(|chicken| { // ... }) } %%CODEBLOCK4%%rust use bevy::prelude::*; use moonshine_spawn::prelude::*; #[derive(Bundle)] struct ChickenBundle { chicken: Chicken, children: SpawnChildren, } #[derive(Component)] struct Chicken; fn chicken() -> impl SpawnOnce { ChickenBundle { chicken: Chicken, children: spawn_children(|chicken| { // ... }) } } %%CODEBLOCK5%%rust,ignore app.add_spawnable("chicken", chicken()); %%CODEBLOCK6%%rust,ignore commands.spawn_with_key("chicken"); %%CODEBLOCK7%%rust,ignore fn chicken() -> impl SpawnOnce { ChickenBundle { chicken: Chicken, children: spawn_children(|chicken| { chicken.spawn_with_key("chicken_head"); }) } } %%CODEBLOCK8%%rust use bevy::prelude::*; use moonshine_spawn::{prelude::*, force_spawn_children}; let mut app = App::new(); // This system spawns a chicken during setup: fn spawn_chicken(mut commands: Commands) { commands.spawn_once_with(chicken()); } // This system depends on children of Chicken: fn update_chicken_head(query: Query<(Entity, &Chicken, &Children)>, mut head: Query<&mut ChickenHead>) { for (entity, chicken, children) in query.iter() { if let Ok(mut head) = head.get_mut(children[0]) { // ... } } } #[derive(Component)] struct Chicken; fn chicken() -> impl SpawnOnce { Chicken.with_children(|chicken| { chicken.spawn(ChickenHead); }) } #[derive(Component)] struct ChickenHead; let mut app = App::new(); app.add_plugins((DefaultPlugins, SpawnPlugin)) .add_systems(Startup, spawn_chicken) // Without forcespawnchildren, chicken head would only be updated on the second update cycle after spawn. // With forcespawnchildren, chicken head would be updated in the same update cycle. .add_systems(Startup, force_spawn_children()) .add_systems(Update, update_chicken_head); ` ## Support Please [post an issue](https://github.com/Zeenobit/moonshine_spawn/issues/new) for any bugs, questions, or suggestions. You may also contact me on the official [Bevy Discord](https://discord.gg/bevy) server as **@Zeenobit**. [World]:(https://docs.rs/bevy/latest/bevy/ecs/world/struct.World.html) [Component]:(https://docs.rs/bevy/latest/bevy/ecs/component/trait.Component.html) [First]:(https://docs.rs/bevy/latest/bevy/app/struct.First.html) [Spawn]:(https://docs.rs/moonshine-spawn/latest/moonshine_spawn/trait.Spawn.html) [SpawnOnce]:(https://docs.rs/moonshine-spawn/latest/moonshine_spawn/trait.SpawnOnce.html) [SpawnKey]:(https://docs.rs/moonshine-spawn/latest/moonshine_spawn/struct.SpawnKey.html) [AddSpawnable]:(https://docs.rs/moonshine-spawn/latest/moonshine_spawn/trait.AddSpawnable.html) [SpawnChildren`]:(https://docs.rs/moonshine-spawn/latest/moonshine_spawn/struct.SpawnChildren.html)