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/systems/view.rs | |
| parent | a24e17014f37318d08fc8224d38e4062c959f34e (diff) | |
can now pick up specific object from ground
Diffstat (limited to 'src/systems/view.rs')
| -rw-r--r-- | src/systems/view.rs | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/systems/view.rs b/src/systems/view.rs index 6e53eb1..7d41820 100644 --- a/src/systems/view.rs +++ b/src/systems/view.rs @@ -1,5 +1,5 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{HashSet}; use specs::{ ReadStorage, @@ -7,8 +7,7 @@ use specs::{ Write, System, Join, - Entities, - Entity + Entities }; use crate::{Pos, Sprite}; @@ -53,14 +52,14 @@ impl <'a> System<'a> for View { let has_changed: bool = !changed.is_empty(); let mut changes: Vec<(Pos, Vec<Sprite>)> = Vec::new(); for pos in changed { - changes.push((pos, cell_sprites(ground.cells.get(&pos).unwrap_or(&HashSet::new()), &visible))); + changes.push((pos, cell_sprites(ground.components_on(pos, &visible)))); } output.output.clear(); for (ent, player, pos) in (&entities, &players, &positions).join() { let mut updates = WorldMessage::default(); if new.get(ent).is_some() { - let (values, mapping) = draw_room(&ground.cells, (size.width, size.height), &visible); + let (values, mapping) = draw_room(&ground, (size.width, size.height), &visible); let field = FieldMessage{ width: size.width, height: size.height, @@ -77,6 +76,13 @@ impl <'a> System<'a> for View { if let Some(health) = healths.get(ent){ updates.health = Some((health.health, health.maxhealth)); } + updates.ground = Some( + ground + .by_height(&pos.pos, &visible, &ent) + .into_iter() + .map(|ent| visible.get(ent).unwrap().name.clone()) + .collect() + ); updates.pos = Some(pos.pos); if !updates.is_empty() { output.output.insert(player.id.clone(), updates); @@ -85,23 +91,19 @@ impl <'a> System<'a> for View { } } -fn cell_sprites(entities: &HashSet<Entity>, visible: &ReadStorage<Visible>) -> Vec<Sprite> { - let mut visibles: Vec<&Visible> = entities.iter().filter_map(|ent| visible.get(*ent)).collect(); +fn cell_sprites(mut visibles: Vec<&Visible>) -> Vec<Sprite> { visibles.sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap()); visibles.iter().map(|vis| vis.sprite.clone()).collect() } -fn draw_room(ground: &HashMap<Pos, HashSet<Entity>>, (width, height): (i64, i64), visible: &ReadStorage<Visible>) -> (Vec<usize>, Vec<Vec<Sprite>>){ +fn draw_room(ground: &Read<Ground>, (width, height): (i64, i64), visible: &ReadStorage<Visible>) -> (Vec<usize>, Vec<Vec<Sprite>>){ let size = width * height; let mut values :Vec<usize> = Vec::with_capacity(size as usize); let mut mapping: Vec<Vec<Sprite>> = Vec::new(); for y in 0..height { for x in 0..width { - let sprites: Vec<Sprite> = match ground.get(&Pos{x, y}) { - Some(ents) => {cell_sprites(ents, visible)} - None => {vec![]} - }; + let sprites: Vec<Sprite> = cell_sprites(ground.components_on(Pos{x, y}, visible)); values.push( match mapping.iter().position(|x| x == &sprites) { Some(index) => { |
