Crate kudo

Source
Expand description

A simple and predictable Entity Component System.

An entity-component-system (ECS) is a data structure and organizational pattern often used by game-like code.

An ECS is made up of three parts:

  • Entities: IDs associated with various components
  • Components: Individual units of data associated with an entity.
  • Systems: Code that iterates over components
// First we create the world.
let mut world = World::new();

// Let's create a new entity with a name and a health component.
// Components are just plain structs.

// This will be our health component
struct Health(i32);

// Spawn the entity with a String component we'll use for the name and a Health component.
// Within the call to spawn we pass in a tuple that can have multiple components.
world.spawn(("Medusa".to_string(), Health(-2)));

// Query the world for entities that have a String component and a Health component.
// The '&' before each component requests read-only access to the component.
// Using '&mut' would request write/read access for that component.
let mut query = world.query::<(&String, &Health)>().unwrap();

// Iterate over all the components we found and check if their health is less than 0.
for (name, health) in query.iter() {
    if health.0 < 0 {
        println!("{} has perished!", name);
    }
}

Structs§

  • A handle to an entity within the world.
  • This entity has been despawned so operations can no longer be performed on it.
  • Query for entities with specific components.
  • Used to get a single immutable instance of a component from the world. If there are multiple of the component in the world an arbitrary instance is returned.
  • Used to get a single mutable instance of a component from the world. If there are multiple of the component in the world an arbitrary instance is returned.
  • The world holds all components and associated entities.

Enums§

Traits§

  • A bundle of components Used to spawn new
  • A member of a Query, like &A or &mut A
  • The parameters passed into a query. Like: (&bool, &String)
  • A function that can be run as system by pulling in queries from the world.
  • An empty trait used to indicate which queries can be constructed at the top level of a query.