From 37ad98cf725aa22f1c793b66102c99c9a76a4ec2 Mon Sep 17 00:00:00 2001 From: troido Date: Tue, 22 Sep 2020 15:36:58 +0200 Subject: can base sprite on equipped items --- src/systems/take.rs | 5 +++-- src/systems/view.rs | 30 +++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) (limited to 'src/systems') diff --git a/src/systems/take.rs b/src/systems/take.rs index eedb83c..b055220 100644 --- a/src/systems/take.rs +++ b/src/systems/take.rs @@ -6,7 +6,8 @@ use specs::{ WriteStorage, System, Join, - Write + Write, + Entity }; use crate::components::{ @@ -41,7 +42,7 @@ impl <'a> System<'a> for Take { for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).join(){ match &controller.control { Control::Take(rank) if inventory.items.len() < inventory.capacity => { - let mut ents = ground.by_height(&position.pos, &visibles, &ent); + let mut ents: Vec = ground.by_height(&position.pos, &visibles).into_iter().filter(|e| *e != ent).collect(); if let Some(idx) = rank { if *idx >= ents.len() { return diff --git a/src/systems/view.rs b/src/systems/view.rs index 5ea7ed8..604878f 100644 --- a/src/systems/view.rs +++ b/src/systems/view.rs @@ -1,5 +1,4 @@ - use specs::{ ReadStorage, WriteStorage, @@ -7,7 +6,8 @@ use specs::{ Write, System, Join, - Entities + Entities, + Entity }; use crate::{ @@ -38,8 +38,15 @@ impl <'a> System<'a> for View { let changes: Vec<(Pos, Vec)> = ground.changes .iter() - .map(|pos| (*pos, cell_sprites(ground.components_on(*pos, &visible)))) - .collect(); + .map(|pos| + ( + *pos, + ground.by_height(pos, &visible).into_iter() + .filter_map(|e| + entity_sprite(e, &visible, &inventories) + ).collect() + ) + ).collect(); let has_changed: bool = !changes.is_empty(); output.output.clear(); @@ -71,8 +78,9 @@ impl <'a> System<'a> for View { } updates.ground = Some( ground - .by_height(&pos.pos, &visible, &ent) + .by_height(&pos.pos, &visible) .into_iter() + .filter(|e| *e != ent) .map(|ent| visible.get(ent).unwrap().name.clone()) .collect() ); @@ -84,6 +92,18 @@ impl <'a> System<'a> for View { } } +fn entity_sprite(ent: Entity, visibles: &ReadStorage, inventories: &ReadStorage) -> Option { + if let Some(inventory) = inventories.get(ent) { + if let Some(sprite) = inventory.equipment_sprites().into_iter().next() { + return Some(sprite); + } + } + if let Some(visible) = visibles.get(ent) { + return Some(visible.sprite.clone()); + } + None +} + fn cell_sprites(mut visibles: Vec<&Visible>) -> Vec { visibles.sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap()); visibles.iter().map(|vis| vis.sprite.clone()).collect() -- cgit