diff options
| author | troido <troido@protonmail.com> | 2020-02-22 12:55:10 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-22 12:55:10 +0100 |
| commit | 2b0cc677f4092d94b31e95f3e9961ec6ed91327b (patch) | |
| tree | 5c5e4683f5b8c9973ccfb260a876f88b599b0326 /src/resources.rs | |
| parent | a24e17014f37318d08fc8224d38e4062c959f34e (diff) | |
can now pick up specific object from ground
Diffstat (limited to 'src/resources.rs')
| -rw-r--r-- | src/resources.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/resources.rs b/src/resources.rs index 6e4bffb..0e9a1e3 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -1,6 +1,6 @@ use std::collections::{HashMap, HashSet}; -use specs::Entity; +use specs::{Entity, ReadStorage, Component}; use crate::{ pos::Pos, @@ -11,7 +11,8 @@ use crate::{ PlayerId, RoomId, util::Result, - template::Template + template::Template, + components::Visible }; @@ -40,6 +41,25 @@ pub struct Spawn { 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>) -> Vec<&'a C> { + self.cells.get(&pos).unwrap_or(&HashSet::new()).iter().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 + } +} + #[derive(Default)] pub struct NewEntities { |
