Expand description
This crate provides the Entity-Component-System of the Magma3D-Engine.
The crate provides a World struct with Resources and Entities.
An entity is just an index into the component storage.
A resource is like a global component, independent of the Entities.
§Usage Example
use magma_ecs::World;
use magma_ecs::component::Component;
use magma_ecs::systems::SystemGraph;
use magma_ecs::query::{Query, With};
use magma_ecs::resources::Res;
use std::any::Any;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut world = World::new();
// Register component types.
world.register_component::<Name>();
world.register_component::<Health>();
world.register_component::<Npc>();
// Add a resource.
world.add_resource(String::from("This is an example resource!"))?;
// create a couple of entities with registered components
for _ in 0..10 {
world.create_entity((Name("Enemy".to_owned()), Health(20), Npc::Enemy))?;
}
let mut system_graph = SystemGraph::new();
system_graph.add_system(sys_a);
let mut dispatcher = system_graph.into_dispatcher(&mut world).unwrap();
dispatcher.dispatch(&mut world);
Ok(())
}
fn sys_a(resource: Res<String>, query: Query<&Name, (With<Health>, With<Npc>)>) {
for name in query {
println!("{} Entity: {}", *resource, name.0);
}
}
// Component can be derived for any type implementing `Send + Sync`.
#[derive(Component)]
struct Name(String);
#[derive(Component)]
struct Health(u32);
#[derive(Component)]
enum Npc {
Ally,
Enemy,
}Re-exports§
pub use rayon;
Modules§
- component
- Component related dunctionality
- entities
- Provides the
Entitiesstruct and functionality. - error
- Error types Error types
- events
- Event API
- query
- Query API
- resources
- Provides the
Resourcesstruct. - systems
- Provides the
SystemGraphstruct, from which aDispatchercan be created.
Structs§
- Unsafe
World Mut - An unsafe pointer to a
Worldfor accessing it’s fields mutably in a concurrent context. - World
- The
Worldstruct holds all the data of a world.