diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/equipment.rs | 13 | ||||
| -rw-r--r-- | src/components/inventory.rs | 7 | ||||
| -rw-r--r-- | src/item.rs | 2 | ||||
| -rw-r--r-- | src/resources/ground.rs | 4 | ||||
| -rw-r--r-- | src/systems/take.rs | 5 | ||||
| -rw-r--r-- | src/systems/view.rs | 30 |
6 files changed, 47 insertions, 14 deletions
diff --git a/src/components/equipment.rs b/src/components/equipment.rs index ab573d8..e7c9734 100644 --- a/src/components/equipment.rs +++ b/src/components/equipment.rs @@ -5,6 +5,9 @@ use specs::{ Component, HashMapStorage }; +use crate::{ + Sprite +}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -46,7 +49,8 @@ impl Stat { #[derive(Debug, Clone, PartialEq)] pub struct Equippable { pub slot: Slot, - pub stats: HashMap<Stat, i64> + pub stats: HashMap<Stat, i64>, + pub sprite: Option<Sprite> } impl Equippable { @@ -60,7 +64,10 @@ impl Equippable { .map(|(k, v)| Some((Stat::from_str(k.as_str())?, v.as_i64()?)) ) - .collect::<Option<HashMap<Stat, i64>>>()? + .collect::<Option<HashMap<Stat, i64>>>()?, + sprite: if let Some(spr) = val.get("sprite") { + Some(Sprite{name: spr.as_str()?.to_string()}) + } else {None} }) } } @@ -101,7 +108,7 @@ mod tests { fn equippable_from_json() { assert_eq!( Equippable::from_json(&json!({"slot": "hand", "stats": {"strength": 10}})), - Some(Equippable {slot: Slot::Hand, stats: hashmap!(Stat::Strength => 10)}) + Some(Equippable {slot: Slot::Hand, stats: hashmap!(Stat::Strength => 10), sprite: None}) ); } diff --git a/src/components/inventory.rs b/src/components/inventory.rs index fa65b03..dc308bf 100644 --- a/src/components/inventory.rs +++ b/src/components/inventory.rs @@ -5,7 +5,8 @@ use crate::{ ItemId, item::{Item, ItemAction}, components::equipment::{Stat, Equippable}, - Encyclopedia + Encyclopedia, + Sprite }; #[derive(Debug, Clone)] @@ -58,4 +59,8 @@ impl Inventory { } bonuses } + + pub fn equipment_sprites(&self) -> Vec<Sprite> { + self.equipped().iter().filter_map(|e| e.sprite.clone()).collect() + } } diff --git a/src/item.rs b/src/item.rs index 0e8661a..88fd583 100644 --- a/src/item.rs +++ b/src/item.rs @@ -73,7 +73,7 @@ mod tests { fn equip_from_json() { assert_eq!( ItemAction::from_json(&json!(["equip", {"slot": "hand", "stats": {"strength": 10}}])), - Some(ItemAction::Equip(Equippable {slot: Slot::Hand, stats: hashmap!(Stat::Strength => 10)})) + Some(ItemAction::Equip(Equippable {slot: Slot::Hand, stats: hashmap!(Stat::Strength => 10), sprite: Option::None})) ); assert_eq!( ItemAction::from_json(&json!(["equip", {"slot": "hand", "stats": {"attack": 50}}])), diff --git a/src/resources/ground.rs b/src/resources/ground.rs index 7868be9..46473b8 100644 --- a/src/resources/ground.rs +++ b/src/resources/ground.rs @@ -57,12 +57,12 @@ impl Ground { nearby_components } - pub fn by_height(&self, pos: &Pos, visibles: &ReadStorage<Visible>, ignore: &Entity) -> Vec<Entity> { + pub fn by_height(&self, pos: &Pos, visibles: &ReadStorage<Visible>) -> Vec<Entity> { let mut entities: Vec<Entity> = self.cells .get(&pos).unwrap_or(&HashSet::new()) .iter() .cloned() - .filter(|e| e != ignore && visibles.contains(*e)) + .filter(|e| visibles.contains(*e)) .collect(); entities.sort_by(|a, b| visibles.get(*b).unwrap().height.partial_cmp(&visibles.get(*a).unwrap().height).unwrap() 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<Entity> = 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<Sprite>)> = 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<Visible>, inventories: &ReadStorage<Inventory>) -> Option<Sprite> { + 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<Sprite> { visibles.sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap()); visibles.iter().map(|vis| vis.sprite.clone()).collect() |
