gdritter repos outlander / a5b9f0e
More work on the component-entity system Getty Ritter 7 years ago
1 changed file(s) with 46 addition(s) and 1 deletion(s). Collapse all Expand all
1 use std::collections::HashMap;
2
13 // An `Entity` is an abstract 64-bit quantity. Nothing outside this
24 // module should care what an entity is or how it works.
3 #[derive(Debug, Copy, Clone)]
5 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
46 pub struct Entity {
57 idx: u64,
68 }
9496 self.generation[e.index() as usize] == e.generation()
9597 }
9698 }
99
100 pub struct System<Component> {
101 map: HashMap<Entity, Component>,
102 }
103
104 impl<Component> System<Component> {
105 pub fn new() -> System<Component> {
106 System { map: HashMap::new() }
107 }
108
109 pub fn add(&mut self, e: Entity, c: Component) {
110 self.map.insert(e, c);
111 }
112
113 pub fn each<F>(&mut self, mut callback: F)
114 where F: FnMut(&Entity, &mut Component)
115 {
116 for (e, c) in self.map.iter_mut() {
117 callback(e, c);
118 }
119 }
120 }
121
122 #[derive(Debug)]
123 pub struct Position {
124 x: f32,
125 y: f32,
126 }
127
128 #[derive(Debug)]
129 pub struct DebugInfo {
130 name: String,
131 }
132
133 #[derive(Debug)]
134 pub struct Mesh {
135 mesh: (),
136 }
137
138 pub struct SystemState {
139 positions: System<Position>,
140 debug_info: System<DebugInfo>,
141 }