use bevy::prelude::*;
use bevy_kindly::*;
#[derive(EntityKind, Clone, Copy, PartialEq, Eq)]
#[default_components(Friends)]
#[components(Name, Age)]
struct Person(Entity);
#[derive(Component, Default)]
struct Friends(Vec<Person>);
#[derive(Component, Clone)]
struct Age(u32);
trait PersonCommands {
fn add_friend(self, friend: Person);
}
impl PersonCommands for &mut EntityKindCommands<'_, '_, '_, Person> {
fn add_friend(self, friend: Person) {
let person = self.get();
self.commands().add(move |world: &mut World| {
world.get_mut::<Friends>(person.entity()).unwrap().0.push(friend);
world.get_mut::<Friends>(friend.entity()).unwrap().0.push(person);
});
}
}
fn main() {
use bevy_kindly::utils::Execute;
let mut world = World::new();
let alice: Person = world.execute(|_, mut commands| {
commands.spawn_with_kind::<Person>(("Alice".into(), Age(25))).get()
});
let bob: Person = world.execute(|_, mut commands| {
commands.spawn_with_kind::<Person>(("Bob".into(), Age(30))).get()
});
world.execute(|_, mut commands| {
commands.with_kind(&alice).add_friend(bob);
});
assert!(world.get::<Friends>(alice.entity()).unwrap().0.contains(&bob));
assert!(world.get::<Friends>(bob.entity()).unwrap().0.contains(&alice));
}