Easily define behavior for sets of components when using the hecs ECS library.
``` use hecscomponentprovider::{ defaulttraitimpl, gentuplequerycomponentproviders, ComponentProvider, ComponentProviderMut, QueryComponentProvider };
struct Position(f32, f32); struct Velocity(f32, f32); struct Enemy { shot_count: i32 };
// implement a behavior for all entities that provide the required components
trait ApplyVelocity: ComponentProviderMut
let mut world = hecs::World::new(); world.spawn((Position(1.0, 2.0), Velocity(0.7, 0.8)));
// prepare a query that returns entities with the components required for the behavior gentuplequerycomponentproviders!( Query1, (&mut Position, &Velocity) );
let dt = 0.1;
for (, mut entity) in world.querymut::
let position = entity.0;
assert_eq!(position.0, 1.07);
assert_eq!(position.1, 2.08);
}
// behaviors can depend on one another
trait EnemyBehaviors: ApplyVelocity + ComponentProviderMut
fn shoot(&mut self) {
let enemy: &mut Enemy = self.get_mut();
enemy.shot_count += 1;
}
}
world.spawn((Enemy { shot_count: 0 }, Position(2.0, 3.0), Velocity(-0.7, -0.8)));
// queries can be prepared using structs instead of tuples
struct Query2<'a> { enemy: &'a mut Enemy, position: &'a mut Position, velocity: &'a Velocity, optional_health: Option<&'a i32> }
let dt = 0.1;
for (, mut entity) in world.querymut::
assert_eq!(entity.enemy.shot_count, 1);
assert_eq!(entity.position.0, 1.93);
assert_eq!(entity.position.1, 2.92);
assert_eq!(entity.optional_health, None);
} ```