Resolves #1253 #1562 This makes the Commands apis consistent with World apis. This moves to a "type state" pattern (like World) where the "current entity" is stored in an `EntityCommands` builder. In general this tends to cuts down on indentation and line count. It comes at the cost of needing to type `commands` more and adding more semicolons to terminate expressions. I also added `spawn_bundle` to Commands because this is a common enough operation that I think its worth providing a shorthand.
57 lines
1.4 KiB
Rust
57 lines
1.4 KiB
Rust
use bevy_asset::Handle;
|
|
use bevy_ecs::{
|
|
entity::Entity,
|
|
system::{Command, Commands},
|
|
world::World,
|
|
};
|
|
use bevy_transform::hierarchy::ChildBuilder;
|
|
|
|
use crate::{Scene, SceneSpawner};
|
|
|
|
pub struct SpawnScene {
|
|
scene_handle: Handle<Scene>,
|
|
}
|
|
|
|
impl Command for SpawnScene {
|
|
fn write(self: Box<Self>, world: &mut World) {
|
|
let mut spawner = world.get_resource_mut::<SceneSpawner>().unwrap();
|
|
spawner.spawn(self.scene_handle);
|
|
}
|
|
}
|
|
|
|
pub trait SpawnSceneCommands {
|
|
fn spawn_scene(&mut self, scene: Handle<Scene>);
|
|
}
|
|
|
|
impl<'a> SpawnSceneCommands for Commands<'a> {
|
|
fn spawn_scene(&mut self, scene_handle: Handle<Scene>) {
|
|
self.add(SpawnScene { scene_handle });
|
|
}
|
|
}
|
|
|
|
pub struct SpawnSceneAsChild {
|
|
scene_handle: Handle<Scene>,
|
|
parent: Entity,
|
|
}
|
|
|
|
impl Command for SpawnSceneAsChild {
|
|
fn write(self: Box<Self>, world: &mut World) {
|
|
let mut spawner = world.get_resource_mut::<SceneSpawner>().unwrap();
|
|
spawner.spawn_as_child(self.scene_handle, self.parent);
|
|
}
|
|
}
|
|
|
|
pub trait SpawnSceneAsChildCommands {
|
|
fn spawn_scene(&mut self, scene: Handle<Scene>) -> &mut Self;
|
|
}
|
|
|
|
impl<'a, 'b> SpawnSceneAsChildCommands for ChildBuilder<'a, 'b> {
|
|
fn spawn_scene(&mut self, scene_handle: Handle<Scene>) -> &mut Self {
|
|
self.add_command(SpawnSceneAsChild {
|
|
scene_handle,
|
|
parent: self.parent_entity(),
|
|
});
|
|
self
|
|
}
|
|
}
|