🥚 Moonshine Spawn
A lightweight spawn utility for Bevy.
Overview
In Bevy, complex hierarchies of entities are typically spawned using ChildBuilder pattern:
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 which arguably makes the code harder to read and maintain.
There is also no mechanism to statically define an entity hierarchy.
This crate aims to solve both of this issues by introducing statically defined spawnables and allowing bundles to spawn children:
use *;
// Define a static spawn key for Chickens
spawn_key!;
Features
- Bundles with children
- Statically defined spawnables
- Custom functional spawnables
- Lightweight implementation with minimal boilerplate
Usage
Spawnables
This crate introduces two new traits: Spawn and SpawnOnce:
SpawnOnce is implemented by default for all Bevy bundles. Spawn is implemented for all types which implement SpawnOnce and can be cloned. This means all bundles which allow cloning also implement Spawn.
Any type which implements either of these traits is a "spawnable".
The output of a spawn is always a bundle, which is then inserted into the given entity at the end of spawn process.
You may use these traits to define functional spawnables:
;
Bundles + Children
To spawn bundles with children, use the WithChildren trait:
Or use the SpawnChildren component:
Static Spawnables
You can define a static spawnable using a SpawnKey:
const CHICKEN: SpawnKey = spawn_key;
Spawn keys must be unique within the scope of a World and are registered using the RegisterSpawnable extension trait.
Use this to register your spawnables during app initialization:
app.register_spawnable;
You may also use the spawn_key! macro to conveniently define one or more spawn keys that match their string representation:
spawn_key!;
spawn_key!;