-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started
First of all, create toecs::World:
use toecs::prelude::*;
let mut world = World::default();Most of our game data will be in the world.
Resource is implemented for every type that is Send + Sync + 'static. They can be inserted into the world:
world.set_res(1usize);And can be borrowed from the World:
*world.res_mut::<usize>() += 10;
assert_eq!(*world.res::<usize>(), 11);World::res returns Res<T>, which is an immutable access to the resource of type T. World::res_mut returns ResMut<T>, which is a mutable access to the resource of type T.
Non-unique thing in a World is referred to as an Entity:
let entity = world.spawn_empty();Entity can be associated with any numbers of components. Component types need to be explicitly defined and registered:
use vek::Vec2;
#[derive(Component, Debug, Clone, Copy, PartialEq)]
pub struct Body {
pub pos: Vec2<i32>,
pub is_blocking: boool,
}
world.register::<Body>();Components can be inserted to Entity:
world.insert(entity, Body::default());Component pool can be borrowed from the world, just like a resource:
{
let mut body_pool = world.comp_mut::<Body>();
let body = body_pool.get_mut(entity).unwrap();
body.pos = [10, 10].into();
}
assert_eq!(world.comp::<Body>().pos.into(), [10, 10]);World data can be borrowed as Res<T>, ResMut<T>, Comp<T> or CompMut<T>. Let's make a function that takes some of them:
fn print_system(u: Res<usize>, body: Comp<Body>) {
println!("usize: {}", *u);
println!("bodies: {:?}", body.as_slice());
}They can be run with the World, without passing the arguments manually:
world.run(print_system);