Crate magma_ecs

Crate magma_ecs 

Source
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 Entities struct and functionality.
error
Error types Error types
events
Event API
query
Query API
resources
Provides the Resources struct.
systems
Provides the SystemGraph struct, from which a Dispatcher can be created.

Structs§

UnsafeWorldMut
An unsafe pointer to a World for accessing it’s fields mutably in a concurrent context.
World
The World struct holds all the data of a world.