Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/swiiz/ecs

Simple Easy to use 🧰 Entity Component System in Rust 🦀
https://github.com/swiiz/ecs

Last synced: 18 days ago
JSON representation

Simple Easy to use 🧰 Entity Component System in Rust 🦀

Awesome Lists containing this project

README

        

# Swiiz's ECS

> An Entity Component System (ECS) is a design pattern in game development where entities are data containers, components hold data, and systems define behavior. This separation enhances flexibility, reusability, and performance in complex simulations.

## How to use ?

```rust
// examples/walkthrough.rs

use ecs::{Entities, Entity, Query};

pub struct Age(u8);

fn main() {
let mut entities = Entities::new();

let mut a = entities.spawn();
a.set("Entity A");

assert!(a.get::<&str>().is_some());
assert!(a.get::().is_none());

entities.spawn().set("Entity B").set(Age(10));

let mut c = entities.spawn();
c.set(Age(20));

assert!(c.get::<&str>().is_none());
assert!(c.get::().is_some());

let unamed_entity_id = c.id();

// "c" is dropped here

for _ in 0..5 {
age_entities(&mut entities);
}

print_entities(&mut entities);

let unamed_entity = entities.edit(unamed_entity_id).unwrap();
print!(
"Unamed entity age: {}",
unamed_entity.get::().unwrap().0
);
}

fn age_entities(entities: &mut Entities) {
for entity in entities.with::().iter() {
entity.get_mut::().unwrap().0 += 1;
}
}

fn print_entities(entities: &mut Entities) {
for entity in entities.with::<&str>().iter() {
let name = entity.get::<&str>().unwrap();

let age = entity
.get::()
.map(|x| x.0.to_string())
.unwrap_or("Unknown".to_string());

println!("{} has age {}", name, age);
}
}

```