use bevy::prelude::*;
use bevy_kindly::*;
#[derive(EntityKind, Debug)]
#[default_components(Friends)]
#[bundle(PersonBundle)]
struct Person(Entity);
impl From<Person> for Agent {
fn from(Person(entity): Person) -> Self {
unsafe { Agent::from_entity_unchecked(entity) }
}
}
#[derive(Bundle)]
struct PersonBundle {
name: Name,
age: Age,
agent: KindBundle<Agent>,
}
#[derive(Component, Default)]
struct Friends(Vec<Person>);
#[derive(Component, Clone)]
struct Age(u32);
#[derive(Debug, EntityKind)]
#[default_components(Position)]
#[components(Speed, Clearance)]
struct Agent(Entity);
#[derive(Component)]
struct Speed(f64);
#[derive(Component)]
struct Clearance(usize);
#[derive(Component, Default, Debug)]
struct Position(Vec2);
#[derive(Component)]
struct Destination(Position);
trait NavigateTo {
fn navigate_to(self, position: Position);
}
impl NavigateTo for &mut EntityKindCommands<'_, '_, '_, Agent> {
fn navigate_to(self, position: Position) {
self.insert(Destination(position));
}
}
fn spawn_person(mut commands: Commands) {
let person: Person = commands.spawn_with_kind::<Person>(PersonBundle {
name: "Alice".into(),
age: Age(25),
agent: KindBundle::new((Speed(10.0), Clearance(2))),
}).get();
info!("{:?} spawned", person);
let agent = person.into();
commands.with_kind(&agent).navigate_to(Position(Vec2::new(5.0, 10.0)));
}
fn update_navigation(
mut query: Query<(
EntityWithKind<Agent>,
&Speed,
&Clearance,
&Destination,
&mut Position,
)>,
) {
for (agent, _speed, _clearance, destination, _position) in &mut query {
info!("TODO: {:?} is navigating to {:?} ...", agent, destination.0);
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, spawn_person)
.add_systems(Update, update_navigation)
.run();
}