blizzard_engine/ecs/
mod.rs1use blizzard_id::Uid;
6use std::collections::HashMap;
7use std::vec::Vec;
8
9pub trait World<I> {
13 fn new() -> Self;
14 fn run_systems(&mut self, input: I);
15}
16
17#[derive(Debug, Clone)]
19pub struct EntityManager {
20 entities: HashMap<u32, bool>,
21}
22
23impl EntityManager {
24 pub fn new() -> Self {
26 Self {
27 entities: HashMap::new(),
28 }
29 }
30
31 pub fn create_entity(&mut self) -> u32 {
33 let mut id = Uid::new_numerical(4);
34 if self.entities.contains_key(&id) {
35 id = self.create_entity();
36 } else {
37 self.entities.insert(id, false);
38 }
39 id
40 }
41
42 pub fn create_n_entities(&mut self, n: i32) -> Vec<u32> {
44 let mut entities = vec![];
45 for _ in 0..n {
46 let entity = self.create_entity();
47 entities.push(entity);
48 }
49 entities
50 }
51
52 pub fn get_one(&self, entity: u32) -> Option<u32> {
54 match self.entities.get(&entity) {
55 Some(_) => Some(entity),
56 None => None,
57 }
58 }
59
60 pub fn get_all(&self) -> &HashMap<u32, bool> {
62 &self.entities
63 }
64
65 pub fn mark_remove(&mut self, entity: u32) {
67 self.entities.insert(entity, true);
68 }
69
70 pub fn remove_entity(&mut self, entity: u32) {
72 self.entities.remove_entry(&entity);
73 }
74
75 pub fn remove_entities(&mut self) {
77 let entities: Vec<u32> = self
78 .entities
79 .iter()
80 .filter(|(_, remove)| **remove)
81 .map(|(id, _)| *id)
82 .collect();
83 entities.iter().for_each(|id| {
84 self.remove_entity(*id);
85 });
86 }
87}
88
89pub trait ComponentRegistry<T: Copy> {
92 fn new() -> Self;
93 fn add(&mut self, entity: u32, component: T);
94 fn add_many(&mut self, entities: &Vec<u32>, component: T);
95 fn remove(&mut self, entity: u32);
96 fn get(&self, entity: u32) -> Option<&T>;
97}