I find "serious" ECS to be an overkill for turn-based game logic,
so I've created this simple library that does only one thing:
stores your components.
#[macro_use]
extern crate zcomponents;
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, Default)]
pub struct Id(i32);
#[derive(Clone, Debug)]
pub struct A {
value: i32,
}
#[derive(Clone, Debug)]
pub struct B {
value: i32,
}
#[derive(Clone, Debug)]
pub struct C;
zcomponents_storage!(Storage<Id>: {
a: A,
b: B,
c: C,
});
fn main() {
let mut storage = Storage::new();
assert_eq!(storage.ids().count(), 0);
let id0 = storage.alloc_id();
assert_eq!(storage.ids().count(), 1);
assert!(!storage.is_exist(id0));
storage.a.insert(id0, A { value: 0 });
assert!(storage.is_exist(id0));
let id1 = storage.alloc_id();
assert_eq!(storage.ids().count(), 2);
storage.a.insert(id1, A { value: 1 });
storage.b.insert(id1, B { value: 1 });
for id in storage.ids_collected() {
if let Some(a) = storage.a.get_opt_mut(id) {
a.value += 1;
}
if let Some(b) = storage.b.get_opt_mut(id) {
b.value += 1;
storage.c.insert(id, C);
}
}
for id in storage.a.ids_collected() {
storage.a.get_mut(id).value += 1;
}
storage.a.remove(id0);
storage.remove(id0);
assert!(!storage.is_exist(id0));
}