diff options
| author | troido <troido@protonmail.com> | 2020-02-24 12:38:23 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-24 12:38:23 +0100 |
| commit | 715c9106dbff4524f3fdf5d23f762e5e6518e0cb (patch) | |
| tree | cf5dc0c5d30fc9ba7e38bdff40907fe4a96261a2 /src/resources/ground.rs | |
| parent | 4718cfdc7c2bf67d2389ca18ab035fe5a0887ff0 (diff) | |
healing works now too, the first time based system
Diffstat (limited to 'src/resources/ground.rs')
| -rw-r--r-- | src/resources/ground.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/resources/ground.rs b/src/resources/ground.rs new file mode 100644 index 0000000..b8c6b5a --- /dev/null +++ b/src/resources/ground.rs @@ -0,0 +1,43 @@ + +use std::collections::{HashMap, HashSet}; + +use specs::{ + ReadStorage, + Component, + Entity +}; + +use crate::{ + components::{Visible, Removed}, + Pos +}; + +#[derive(Default)] +pub struct Ground { + pub cells: HashMap<Pos, HashSet<Entity>> +} + +impl Ground { + pub fn components_on<'a, C: Component>(&self, pos: Pos, component_type: &'a ReadStorage<C>, removals: &'a ReadStorage<Removed>) -> Vec<&'a C> { + self.cells + .get(&pos) + .unwrap_or(&HashSet::new()) + .iter() + .filter(|e| !removals.contains(**e)) + .filter_map(|e| component_type.get(*e)) + .collect() + } + + pub fn by_height(&self, pos: &Pos, visibles: &ReadStorage<Visible>, ignore: &Entity) -> Vec<Entity> { + let mut entities: Vec<Entity> = self.cells + .get(&pos).unwrap_or(&HashSet::new()) + .iter() + .cloned() + .filter(|e| e != ignore && visibles.contains(*e)) + .collect(); + entities.sort_by(|a, b| + visibles.get(*b).unwrap().height.partial_cmp(&visibles.get(*a).unwrap().height).unwrap() + ); + entities + } +} |
