Entity-Component-System for the Magma3D engine
  • Rust 99.2%
  • Nix 0.8%
Find a file
2025-10-17 02:22:47 +02:00
benches benchmarks 2025-10-12 16:04:33 +02:00
macros performance rework using a BitSet for entities 2025-10-17 02:22:47 +02:00
src performance rework using a BitSet for entities 2025-10-17 02:22:47 +02:00
tests Add methods for better specifying system deps 2025-10-11 14:58:35 +02:00
.gitignore performance rework using a BitSet for entities 2025-10-17 02:22:47 +02:00
Cargo.lock performance rework using a BitSet for entities 2025-10-17 02:22:47 +02:00
Cargo.toml performance rework using a BitSet for entities 2025-10-17 02:22:47 +02:00
flake.lock add Res and ResMut system params 2025-10-05 17:53:19 +02:00
flake.nix system_params 2025-09-20 18:47:09 +02:00
LICENSE.md Create LICENSE.md 2023-03-08 09:09:21 +01:00
README.md usability fixes 2025-10-08 17:14:33 +02:00

Magma-ECS

Magma-ECS is the Entity-component-system developed for the Magma3D engine. It aims to be simple to use and lightweight. It also provides an easy way for parallel execution of systems.

Even though this is intended for the magma_api it is easily possible to use on it's own.

Features

  • Efficient component storage using hi_sparse_bitset.
  • Multithreading using AtomicRefCell and rayon's parallel iterators.
  • Easy and ergonimic querying.
  • Creating system dispatchers that allow for parallel execution of systems, with the possibility to depend on other systems.
  • Support for global and system local resources.
  • Event system

Usage

Add the crate to your Cargo.toml:

[dependencies]
magma_ecs = "0.5.0-alpha.3"

Entity-Component-System

Entity: An entity is just an index into the component storage.
Component: A component holds some type of data. Entities can have components assigned to them.
System: A system is a piece of code (usually a function), that reads and modifies the data.

Another way to think about this would be Identifier-Data-Logic.

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, vec![]);

    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,
}

Cargo Features

none

Disclaimer

This is still in developement and not production ready.