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 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<ItemType> {
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<ItemType>
) {
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());
}

View file

@ -365,7 +365,7 @@ pub fn draw_ui2(ecs: &World, draw: &mut Draw, atlas: &HashMap<String, Texture>,
.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::<Renderable>();
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<UniqueInventoryItem, InventorySlot>;
pub enum Filter {
All,
Backpack,
All(Option<ItemType>),
Backpack(Option<ItemType>),
Equipped,
Category(ItemType),
}
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();
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::<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>();
for (e, k, _b) in (&entities, &keys, &backpack).join() {
includeitem!(inv, ecs, e, k);
if itemtype.is_some() {
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 => {
@ -1399,14 +1416,6 @@ pub fn items(ecs: &World, filter: Filter) -> HashMap<UniqueInventoryItem, Invent
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

View file

@ -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);
}
_ => {}
}