use flax::{Component, ComponentBuffer, Debug, Entity, MetaData, World};
use glam::{vec2, Vec2};
use tracing_subscriber::{prelude::*, registry};
use tracing_tree::HierarchicalLayer;
fn main() -> anyhow::Result<()> {
registry().with(HierarchicalLayer::default()).init();
let mut world = World::new();
let position: Component<Vec2> = world.spawn_component("position", |desc| {
let mut buf = ComponentBuffer::new();
<Debug as MetaData<Vec2>>::attach(desc, &mut buf);
buf
});
let id = Entity::builder()
.set(position, vec2(1.0, 6.4))
.spawn(&mut world);
tracing::info!("world: {world:#?}");
world.despawn(position.id())?;
tracing::info!("world: {world:#?}");
world.despawn(id)?;
#[derive(Debug, Clone)]
struct RelationData {
distance: f32,
}
let child_of = world.spawn_relation::<RelationData>("child_of", |desc| {
let mut buf = ComponentBuffer::new();
<Debug as MetaData<RelationData>>::attach(desc, &mut buf);
buf
});
let parent = world.spawn();
let child = Entity::builder()
.set(child_of(parent), RelationData { distance: 1.0 })
.spawn(&mut world);
let data = world.get(child, child_of(parent))?;
tracing::info!("Relation distance: {:?}", data.distance);
drop(data);
world.despawn(parent)?;
assert!(world.get(child, child_of(parent)).is_err());
Ok(())
}