diff options
Diffstat (limited to 'src/systems')
| -rw-r--r-- | src/systems/take.rs | 20 | ||||
| -rw-r--r-- | src/systems/view.rs | 26 |
2 files changed, 27 insertions, 19 deletions
diff --git a/src/systems/take.rs b/src/systems/take.rs index 31634d2..99c66d5 100644 --- a/src/systems/take.rs +++ b/src/systems/take.rs @@ -1,5 +1,4 @@ -use std::collections::HashSet; use specs::{ Entities, @@ -15,7 +14,8 @@ use crate::components::{ Position, Removed, Inventory, - Item + Item, + Visible }; use crate::controls::{Control}; @@ -33,15 +33,21 @@ impl <'a> System<'a> for Take { WriteStorage<'a, Removed>, ReadStorage<'a, Item>, WriteStorage<'a, Inventory>, - Write<'a, NewEntities> + Write<'a, NewEntities>, + ReadStorage<'a, Visible> ); - fn run(&mut self, (entities, controllers, positions, ground, mut removed, items, mut inventories, mut new): Self::SystemData) { + fn run(&mut self, (entities, controllers, positions, ground, mut removed, items, mut inventories, mut new, visibles): Self::SystemData) { for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).join(){ match &controller.0 { - Control::Take(_rank) if inventory.items.len() < inventory.capacity => { - let mut ents = ground.cells.get(&position.pos).unwrap_or(&HashSet::new()).clone(); - ents.remove(&ent); + Control::Take(rank) if inventory.items.len() < inventory.capacity => { + let mut ents = ground.by_height(&position.pos, &visibles, &ent); + if let Some(idx) = rank { + if *idx >= ents.len() { + return + } + ents = vec!(ents[*idx]); + } for ent in ents { if let Some(item) = items.get(ent) { inventory.items.insert(0, item.clone()); 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) => { |
