roguelike-game/src/gui/identify_menu.rs

113 lines
3.5 KiB
Rust
Raw Normal View History

2022-02-02 09:45:19 -05:00
use ::rltk::{FontCharType, Rltk, VirtualKeyCode};
use ::specs::prelude::*;
use super::enums::*;
use super::{get_item_color, get_item_display_name};
use crate::components::{Equipped, InBackpack, Item, Name, ObfuscatedName};
use crate::{colors, MasterDungeonMap, State};
pub fn identify_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
use ::rltk::to_cp437;
let player_entity = gs.ecs.fetch::<Entity>();
let equipped = gs.ecs.read_storage::<Equipped>();
let backpack = gs.ecs.read_storage::<InBackpack>();
let entities = gs.ecs.entities();
let items = gs.ecs.read_storage::<Item>();
let names = gs.ecs.read_storage::<Name>();
let dm = gs.ecs.fetch::<MasterDungeonMap>();
let obfuscated = gs.ecs.read_storage::<ObfuscatedName>();
let build_cursed_iterator = || {
(&entities, &items).join().filter(|(item_entity, _item)| {
let mut keep = false;
if let Some(bp) = backpack.get(*item_entity) {
if bp.owner == *player_entity {
if let Some(name) = names.get(*item_entity) {
if obfuscated.get(*item_entity).is_some()
&& !dm.identified_items.contains(&name.name)
{
keep = true;
}
}
}
}
// It's equipped, so we know it's cursed
if let Some(equip) = equipped.get(*item_entity) {
if equip.owner == *player_entity {
if let Some(name) = names.get(*item_entity) {
if obfuscated.get(*item_entity).is_some()
&& !dm.identified_items.contains(&name.name)
{
keep = true;
}
}
}
}
keep
})
};
let count = build_cursed_iterator().count();
let mut y = (25 - (count / 2)) as i32;
ctx.draw_box(
15,
y - 2,
31,
(count + 3) as i32,
colors::WHITE,
colors::BLACK,
);
ctx.print_color(
18,
y - 2,
colors::YELLOW,
colors::BLACK,
"Identify Which Item?",
);
ctx.print_color(
18,
y + count as i32 + 1,
colors::YELLOW,
colors::BLACK,
"ESCAPE to cancel",
);
let mut equippable: Vec<Entity> = Vec::new();
for (j, (entity, _item)) in build_cursed_iterator().enumerate() {
ctx.set(17, y, colors::WHITE, colors::BLACK, to_cp437('('));
ctx.set(18, y, colors::YELLOW, colors::BLACK, 97 + j as FontCharType);
ctx.set(19, y, colors::WHITE, colors::BLACK, to_cp437(')'));
ctx.print_color(
21,
y,
get_item_color(&gs.ecs, entity),
colors::BLACK,
&get_item_display_name(&gs.ecs, entity),
);
equippable.push(entity);
y += 1;
}
match ctx.key {
None => (ItemMenuResult::NoResponse, None),
Some(key) => match key {
VirtualKeyCode::Escape => (ItemMenuResult::Cancel, None),
_ => {
let selection = ::rltk::letter_to_option(key);
if selection > -1 && selection < count as i32 {
return (
ItemMenuResult::Selected,
Some(equippable[selection as usize]),
);
}
(ItemMenuResult::NoResponse, None)
}
},
}
}