A performant, zero-dependency ECS library with a nice API written in Rust.
```toml
[dependecies] kiwi-ecs = "1.0" ```
rust
// lib.rs
use kiwi_ecs::*;
To start, create a new World
. This is the starting point of the ecs.
The program can have multiple independent worlds.
rust
pub fn main() {
let mut world = World::new();
}
Components are defined as follows:
```rust
struct Position { x: u32, y: u32 } ```
To spawn a new entity with the given ids:
rust
// spawn_entity macro accepts the world as the first parameter, and the
// components to add to the entity as the other parameters
let id = spawn_entity!(world, Pos { x: 0, y: 0 });
There are two ways to define systems.
system
macro:```rust // immutable system
fn print_positions(world: &World) { println!("{:?}", pos); }
// mutable system
fn move_entities(world: &mut World) { pos.x += vel.x; pos.y += vel.y }
// query entity ids as well
/// prints all entities ids having the position component fn printentityids(world: &World) { println!("{id}"); }
pub fn main() { let mut world = World::new();
//--snip
// Call the systems moveentities(&mut world); printpositions(&world); } ```
To create a mutable system, the function should contain world: &mut World
,
for an immutable one, add world: &World
.
The function can contain any number of arguments you can pass to it when calling.
query
and query_mut
macros:```rust pub fn main() { let mut world = World::new();
//--snip
let components: Vec<&Position> = query!(world, Position);
let components: (Vec<mut Position>, Vec<mut Vel>) = unsafe { query_mut!(world, Position, Vel) };
let components: (Vec
// You can now loop over the components } ```
Note on safety: the query_mut
macro is unsafe, because it can cause undefined behaviour
if two of the same component types are passed in.
Licensed under the MIT license.