More work on the component-entity system
Getty Ritter
7 years ago
| 1 | use std::collections::HashMap; | |
| 2 | ||
| 1 | 3 | // An `Entity` is an abstract 64-bit quantity. Nothing outside this |
| 2 | 4 | // module should care what an entity is or how it works. |
| 3 |
#[derive(Debug, Copy, Clone |
|
| 5 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | |
| 4 | 6 | pub struct Entity { |
| 5 | 7 | idx: u64, |
| 6 | 8 | } |
| 94 | 96 | self.generation[e.index() as usize] == e.generation() |
| 95 | 97 | } |
| 96 | 98 | } |
| 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 | } | |