Well, i'm trying to accomplish an ECS, Entity Component System.
I "invented" this system where each component changes the entire world including itself.
Example:
Entity Fox tries to kill Entity Bunny if finds it
Rust pseudo code:
trait Components {
fn call_every_turn(api) {}
}
impl Components for Hostile {
fn call_every_turn(api) {
// if hostile finds bunny, kills it.
if let some((index_bunny, _))) = api.get_entity::<bunny>() {
self.remove_ent(index_bunny);
}
}
}
trait Entity {
fn new(api) {}
}
// so Hostile is inside Fox.
impl Entity for Fox {
fn new(api){
api.add_comp(Hostile)
api.add_comp(Life)
....
}
}
struct Api {
entities_vec = Vec<(Entity, Vec<dyn Components)
// Entities and Components
}
impl Api {
fn get_entity<Entity: 'static>(&self, ent_id) -> (ent_id, Entity) {
for (id, entity) in self.entities[ent_id].0. enumerate() {
if let some(entity) (entity.dowcast to comp)
return (id, entity)
}
}
fn remove_ent(id) { self.entities.remove(i) } // "Kills" it
fn add_components(comp) ...
fn call_every_turn(&mut self) {
for ent in self.entities { // iterate entities
for comp in ent.1 { // iterate components
comp.call_every_turn(self); // problem
// can't borrow mutable twice
}
}
}
So there is no way to make this system work?