Skip to content

Commit

Permalink
Merge pull request #10 from MrGVSV/insertion
Browse files Browse the repository at this point in the history
Insert prototype data on existing entities
  • Loading branch information
MrGVSV authored Feb 24, 2022
2 parents 1b4a74a + e9dc175 commit 11b3373
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
7 changes: 6 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ fn spawn_person(mut commands: Commands, data: Res<ProtoData>, asset_server: Res<
// Spawn it again!
proto.spawn(&mut commands, &data, &asset_server);

// Insert on an existing entity!
let entity = commands.spawn().id();
let entity_cmds = commands.entity(entity);
proto.insert(entity_cmds, &data, &asset_server);

// Spawn in others!
for i in 1..4 {
for i in 2..=3 {
data.get_prototype(format!("Person Test {}", i).as_str())
.unwrap()
.spawn(&mut commands, &data, &asset_server);
Expand Down
54 changes: 52 additions & 2 deletions src/prototype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::slice::Iter;

use bevy::ecs::prelude::Commands;
use bevy::ecs::system::EntityCommands;
use bevy::prelude::{AssetServer, Res};
use bevy::prelude::{AssetServer, Entity, Res};
use indexmap::IndexSet;
use serde::{
de::{self, Error, SeqAccess, Visitor},
Expand Down Expand Up @@ -64,7 +64,7 @@ pub trait Prototypical: 'static + Send + Sync {
///
/// ```
/// use bevy::prelude::*;
/// use bevy_proto::{ProtoData, Prototype, Prototypical};
/// use bevy_proto::prelude::{ProtoData, Prototype, Prototypical};
///
/// fn setup_system(mut commands: Commands, data: Res<ProtoData>, asset_server: &Res<AssetServer>) {
/// let proto: Prototype = serde_yaml::from_str(r#"
Expand All @@ -89,6 +89,56 @@ pub trait Prototypical: 'static + Send + Sync {
asset_server: &Res<AssetServer>,
) -> EntityCommands<'w, 's, 'a> {
let entity = commands.spawn();
self.insert(entity, data, asset_server)
}

/// Inserts this prototype's component structure to the given entity
///
/// __Note:__ This _will_ override existing components of the same type.
///
/// # Arguments
///
/// * `entity`: The `EntityCommands` for a given entity
/// * `data`: The prototype data in this world
/// * `asset_server`: The asset server
///
/// returns: EntityCommands
///
/// # Examples
///
/// ```
/// use bevy::prelude::*;
/// use bevy_proto::prelude::{ProtoData, Prototype, Prototypical};
///
/// #[derive(Component, Default)]
/// struct Player(pub Entity);
///
/// fn setup_system(mut commands: Commands, data: Res<ProtoData>, asset_server: &Res<AssetServer>, player: Query<&Player>) {
/// let proto: Prototype = serde_yaml::from_str(r#"
/// name: My Prototype
/// components:
/// - type: SomeMarkerComponent
/// - type: SomeComponent
/// value:
/// - speed: 10.0
/// "#).unwrap();
///
/// // Get the EntityCommands for the player entity
/// let entity = commands.entity(player.single().0);
///
/// // Insert the new components
/// let entity = proto.insert(entity, &data, &asset_server).id();
///
/// // ...
/// }
///
/// ```
fn insert<'w, 's, 'a, 'p>(
&'p self,
entity: EntityCommands<'w, 's, 'a>,
data: &Res<ProtoData>,
asset_server: &Res<AssetServer>,
) -> EntityCommands<'w, 's, 'a> {
let mut proto_commands = self.create_commands(entity, data);

spawn_internal(
Expand Down

0 comments on commit 11b3373

Please sign in to comment.