From 715c9106dbff4524f3fdf5d23f762e5e6518e0cb Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 24 Feb 2020 12:38:23 +0100 Subject: healing works now too, the first time based system --- src/resources/ground.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/resources/ground.rs (limited to 'src/resources/ground.rs') 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> +} + +impl Ground { + pub fn components_on<'a, C: Component>(&self, pos: Pos, component_type: &'a ReadStorage, removals: &'a ReadStorage) -> 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, ignore: &Entity) -> Vec { + let mut entities: Vec = 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 + } +} -- cgit