Filipe Rodrigues 4aaca0032d Changed the predicate iterator to pass a bool that may be set if the entity should be removed
Changed documentation in `Entity::{add, remove}` to reflect the new feature
Updated examples to reflect the new feature.
Removed unecessary unsafe transmutes in `world/iter.rs`
Changed `World::iter_mut_all` to `World::iter_all_mut` and deprecated the old name
Improved documentation of most items in `world.rs`
Changed `World::from_components` to accept any iterator, in a non-breaking way.
Added `Index` and `IndexMut` support to `World`
Fixed serde deserialization problems with lifetimes.
Updated crate to 0.2.0 due to breaking change of new features.
2020-01-25 12:18:04 +00:00
2020-01-23 18:31:46 +00:00
2020-01-23 18:07:30 +00:00

MEcs

Ecs library with a focus on iteration performance

Example

The following is a small example on how to use the ecs library:

use mecs::{World};

/// Position component
#[derive(PartialEq, Clone, Copy, Debug)]
struct Position(pub f32, pub f32);

/// Velocity component
#[derive(PartialEq, Clone, Copy, Debug)]
struct Velocity(pub f32, pub f32);

mecs::impl_enum_storage! {
	/// All of our components
	enum Components {
		Position(Position),
		Velocity(Velocity),
	}
}

fn main()
{
	let mut world: World<Components> = World::new();
	
	world.add( mecs::entity![
		Components::Position( Position(1.0, 10.0) ),
		Components::Velocity( Velocity(0.1, -1.0) ),
	]);
	
	let pred_id = world.add_pred(|entity| entity.has::<Position>() && entity.has::<Velocity>());
	
	for i in 0..10 {
		let mut pred_iter = world.iter_pred_mut(pred_id).unwrap();
		while let Some( (entity, id) ) = pred_iter.next()
		{
			let vel:      Velocity = *entity.get    ().unwrap();
			let pos: &mut Position =  entity.get_mut().unwrap();
			
			pos.0 += vel.0;
			pos.1 += vel.1;
			
			println!("{:?}", pos);
			
			pred_iter.world.remove(id);
		}
	}
}

Will print

Position { 1.1, 9.0 }
Position { 1.2, 8.0 }
Position { 1.3, 7.0 }
...
Position { 2.0, 0.0 }

Nightly

This library currently uses features only available on the nightly channel, particularly the Fn family of traits. In the future, once these are stabilized, the library will be able to be used on stable.

Description
Rust Ecs system with a focus on iteration performance
Readme MIT 55 KiB
Languages
Rust 95.8%
RenderScript 4.2%