more "cleanup"

This commit is contained in:
Llywelwyn 2023-10-05 04:24:20 +01:00
parent 9c1298df6b
commit 139e718fd1
3 changed files with 66 additions and 38 deletions

View file

@ -5,23 +5,14 @@ use super::TILESIZE;
use crate::{ Fonts, camera::get_offset }; use crate::{ Fonts, camera::get_offset };
use super::{ items, Filter, print_options, ItemType }; use super::{ items, Filter, print_options, ItemType };
pub fn draw_inventory(ecs: &World, draw: &mut Draw, font: &Fonts, x: i32, y: i32) { pub enum Location {
let inv = items(ecs, Filter::Backpack); All,
let offsets = crate::camera::get_offset(); Backpack,
print_options( Equipped,
ecs,
draw,
font,
&inv,
((x as f32) + (offsets.x as f32)) * TILESIZE,
((y as f32) + (offsets.y as f32)) * TILESIZE
);
} }
pub fn draw_all(ecs: &World, draw: &mut Draw, font: &Fonts, x: f32, y: f32) { pub fn all_itemtypes() -> Vec<ItemType> {
let mut y = y; vec![
let itemtypes = vec![
ItemType::Weapon, ItemType::Weapon,
ItemType::Armour, ItemType::Armour,
ItemType::Comestible, ItemType::Comestible,
@ -31,10 +22,26 @@ pub fn draw_all(ecs: &World, draw: &mut Draw, font: &Fonts, x: f32, y: f32) {
ItemType::Wand, ItemType::Wand,
ItemType::Amulet, ItemType::Amulet,
ItemType::Ring ItemType::Ring
]; ]
}
pub fn draw_items(
ecs: &World,
draw: &mut Draw,
font: &Fonts,
x: f32,
y: f32,
loc: Location,
itemtypes: Vec<ItemType>
) {
let mut y = y;
for itemtype in itemtypes { 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() { if inv.is_empty() {
continue; 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; 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());
}

View file

@ -365,7 +365,7 @@ pub fn draw_ui2(ecs: &World, draw: &mut Draw, atlas: &HashMap<String, Texture>,
.size(FONTSIZE); .size(FONTSIZE);
} }
// Equipment // 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::<Renderable>(); /*let renderables = ecs.read_storage::<Renderable>();
let mut equipment: Vec<(String, RGB, RGB, FontCharType)> = Vec::new(); let mut equipment: Vec<(String, RGB, RGB, FontCharType)> = Vec::new();
let entities = ecs.entities(); let entities = ecs.entities();
@ -981,7 +981,7 @@ pub fn print_options(
EquipmentSlot::Melee | EquipmentSlot::Shield => "being held", EquipmentSlot::Melee | EquipmentSlot::Shield => "being held",
_ => "being worn", _ => "being worn",
}; };
draw.text(&font.ib(), &format!(" ({})", text)) draw.text(&font.b(), &format!(" ({})", text))
.position(x, y) .position(x, y)
.color(Color::WHITE) .color(Color::WHITE)
.size(FONTSIZE); .size(FONTSIZE);
@ -1355,10 +1355,9 @@ pub struct InventorySlot {
pub type PlayerInventory = HashMap<UniqueInventoryItem, InventorySlot>; pub type PlayerInventory = HashMap<UniqueInventoryItem, InventorySlot>;
pub enum Filter { pub enum Filter {
All, All(Option<ItemType>),
Backpack, Backpack(Option<ItemType>),
Equipped, Equipped,
Category(ItemType),
} }
macro_rules! includeitem { macro_rules! includeitem {
@ -1382,15 +1381,33 @@ pub fn items(ecs: &World, filter: Filter) -> HashMap<UniqueInventoryItem, Invent
let mut inv: HashMap<UniqueInventoryItem, InventorySlot> = HashMap::new(); let mut inv: HashMap<UniqueInventoryItem, InventorySlot> = HashMap::new();
match filter { match filter {
Filter::All => { Filter::All(itemtype) => {
for (e, k) in (&entities, &keys).join() { if itemtype.is_some() {
includeitem!(inv, ecs, e, k); let items = ecs.read_storage::<Item>();
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::<InBackpack>(); let backpack = ecs.read_storage::<InBackpack>();
for (e, k, _b) in (&entities, &keys, &backpack).join() { if itemtype.is_some() {
includeitem!(inv, ecs, e, k); let items = ecs.read_storage::<Item>();
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 => { Filter::Equipped => {
@ -1399,14 +1416,6 @@ pub fn items(ecs: &World, filter: Filter) -> HashMap<UniqueInventoryItem, Invent
includeitem!(inv, ecs, e, k); includeitem!(inv, ecs, e, k);
} }
} }
Filter::Category(itemtype) => {
let items = ecs.read_storage::<Item>();
for (e, k, _i) in (&entities, &keys, &items)
.join()
.filter(|e| e.2.category == itemtype) {
includeitem!(inv, ecs, e, k);
}
}
} }
inv inv

View file

@ -550,11 +550,15 @@ fn draw(_app: &mut App, gfx: &mut Graphics, gs: &mut State) {
} }
RunState::ShowInventory => { RunState::ShowInventory => {
corner_text("Use what? [aA-zZ]/[Esc.]", &mut draw, &gs.font); 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 => { RunState::ShowDropItem => {
corner_text("Drop what? [aA-zZ]/[Esc.]", &mut draw, &gs.font); 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);
} }
_ => {} _ => {}
} }