From 139e718fd142f1f8dfe7f0bc8e8b6efb52ec1580 Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Thu, 5 Oct 2023 04:24:20 +0100 Subject: [PATCH] more "cleanup" --- src/gui/inventory.rs | 49 +++++++++++++++++++++++++++++--------------- src/gui/mod.rs | 47 +++++++++++++++++++++++++----------------- src/main.rs | 8 ++++++-- 3 files changed, 66 insertions(+), 38 deletions(-) diff --git a/src/gui/inventory.rs b/src/gui/inventory.rs index 628ed58..1b9af9e 100644 --- a/src/gui/inventory.rs +++ b/src/gui/inventory.rs @@ -5,23 +5,14 @@ use super::TILESIZE; use crate::{ Fonts, camera::get_offset }; use super::{ items, Filter, print_options, ItemType }; -pub fn draw_inventory(ecs: &World, draw: &mut Draw, font: &Fonts, x: i32, y: i32) { - let inv = items(ecs, Filter::Backpack); - let offsets = crate::camera::get_offset(); - print_options( - ecs, - draw, - font, - &inv, - ((x as f32) + (offsets.x as f32)) * TILESIZE, - ((y as f32) + (offsets.y as f32)) * TILESIZE - ); +pub enum Location { + All, + Backpack, + Equipped, } -pub fn draw_all(ecs: &World, draw: &mut Draw, font: &Fonts, x: f32, y: f32) { - let mut y = y; - - let itemtypes = vec![ +pub fn all_itemtypes() -> Vec { + vec![ ItemType::Weapon, ItemType::Armour, ItemType::Comestible, @@ -31,10 +22,26 @@ pub fn draw_all(ecs: &World, draw: &mut Draw, font: &Fonts, x: f32, y: f32) { ItemType::Wand, ItemType::Amulet, ItemType::Ring - ]; + ] +} +pub fn draw_items( + ecs: &World, + draw: &mut Draw, + font: &Fonts, + x: f32, + y: f32, + loc: Location, + itemtypes: Vec +) { + let mut y = y; for itemtype in itemtypes { - let inv = items(ecs, Filter::Category(itemtype)); + let filter = match loc { + Location::All => Filter::All(Some(itemtype)), + Location::Backpack => Filter::Backpack(Some(itemtype)), + Location::Equipped => Filter::Equipped, + }; + let inv = items(ecs, filter); if inv.is_empty() { continue; } @@ -43,3 +50,11 @@ pub fn draw_all(ecs: &World, draw: &mut Draw, font: &Fonts, x: f32, y: f32) { y = print_options(ecs, draw, font, &inv, x, y) + TILESIZE; } } + +pub fn draw_all_items(ecs: &World, draw: &mut Draw, font: &Fonts, x: f32, y: f32) { + draw_items(ecs, draw, font, x, y, Location::All, all_itemtypes()); +} + +pub fn draw_backpack_items(ecs: &World, draw: &mut Draw, font: &Fonts, x: f32, y: f32) { + draw_items(ecs, draw, font, x, y, Location::Backpack, all_itemtypes()); +} diff --git a/src/gui/mod.rs b/src/gui/mod.rs index efe9674..7e50298 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -365,7 +365,7 @@ pub fn draw_ui2(ecs: &World, draw: &mut Draw, atlas: &HashMap, .size(FONTSIZE); } // Equipment - draw_all(ecs, draw, font, ((VIEWPORT_W + 3) as f32) * TILESIZE, TILESIZE); + draw_all_items(ecs, draw, font, ((VIEWPORT_W + 3) as f32) * TILESIZE, TILESIZE); /*let renderables = ecs.read_storage::(); let mut equipment: Vec<(String, RGB, RGB, FontCharType)> = Vec::new(); let entities = ecs.entities(); @@ -981,7 +981,7 @@ pub fn print_options( EquipmentSlot::Melee | EquipmentSlot::Shield => "being held", _ => "being worn", }; - draw.text(&font.ib(), &format!(" ({})", text)) + draw.text(&font.b(), &format!(" ({})", text)) .position(x, y) .color(Color::WHITE) .size(FONTSIZE); @@ -1355,10 +1355,9 @@ pub struct InventorySlot { pub type PlayerInventory = HashMap; pub enum Filter { - All, - Backpack, + All(Option), + Backpack(Option), Equipped, - Category(ItemType), } macro_rules! includeitem { @@ -1382,15 +1381,33 @@ pub fn items(ecs: &World, filter: Filter) -> HashMap = HashMap::new(); match filter { - Filter::All => { - for (e, k) in (&entities, &keys).join() { - includeitem!(inv, ecs, e, k); + Filter::All(itemtype) => { + if itemtype.is_some() { + let items = ecs.read_storage::(); + for (e, k, _i) in (&entities, &keys, &items) + .join() + .filter(|e| e.2.category == itemtype.unwrap()) { + includeitem!(inv, ecs, e, k); + } + } else { + for (e, k) in (&entities, &keys).join() { + includeitem!(inv, ecs, e, k); + } } } - Filter::Backpack => { + Filter::Backpack(itemtype) => { let backpack = ecs.read_storage::(); - for (e, k, _b) in (&entities, &keys, &backpack).join() { - includeitem!(inv, ecs, e, k); + if itemtype.is_some() { + let items = ecs.read_storage::(); + for (e, k, _i, _b) in (&entities, &keys, &items, &backpack) + .join() + .filter(|e| e.2.category == itemtype.unwrap()) { + includeitem!(inv, ecs, e, k); + } + } else { + for (e, k, _b) in (&entities, &keys, &backpack).join() { + includeitem!(inv, ecs, e, k); + } } } Filter::Equipped => { @@ -1399,14 +1416,6 @@ pub fn items(ecs: &World, filter: Filter) -> HashMap { - let items = ecs.read_storage::(); - for (e, k, _i) in (&entities, &keys, &items) - .join() - .filter(|e| e.2.category == itemtype) { - includeitem!(inv, ecs, e, k); - } - } } inv diff --git a/src/main.rs b/src/main.rs index 15ec604..7548e5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -550,11 +550,15 @@ fn draw(_app: &mut App, gfx: &mut Graphics, gs: &mut State) { } RunState::ShowInventory => { corner_text("Use what? [aA-zZ]/[Esc.]", &mut draw, &gs.font); - gui::draw_inventory(&gs.ecs, &mut draw, &gs.font, 1, 3); + let offset = crate::camera::get_offset(); + let (x, y) = (((1 + offset.x) as f32) * TILESIZE, ((3 + offset.y) as f32) * TILESIZE); + gui::draw_backpack_items(&gs.ecs, &mut draw, &gs.font, x, y); } RunState::ShowDropItem => { corner_text("Drop what? [aA-zZ]/[Esc.]", &mut draw, &gs.font); - gui::draw_inventory(&gs.ecs, &mut draw, &gs.font, 1, 3); + let offset = crate::camera::get_offset(); + let (x, y) = (((1 + offset.x) as f32) * TILESIZE, ((3 + offset.y) as f32) * TILESIZE); + gui::draw_backpack_items(&gs.ecs, &mut draw, &gs.font, x, y); } _ => {} }