roguelike-game/src/gui/identify_menu.rs

68 lines
2.3 KiB
Rust
Raw Normal View History

2022-02-02 11:52:19 -05:00
use ::rltk::prelude::*;
use ::rltk::Rltk;
2022-02-02 09:45:19 -05:00
use ::specs::prelude::*;
2022-02-02 11:52:19 -05:00
use super::{get_item_display_name, item_result_menu, ItemMenuResult};
2022-02-02 09:45:19 -05:00
use crate::components::{Equipped, InBackpack, Item, Name, ObfuscatedName};
2022-02-02 11:52:19 -05:00
use crate::{MasterDungeonMap, State};
2022-02-02 09:45:19 -05:00
pub fn identify_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
2022-02-02 11:52:19 -05:00
let mut draw_batch = DrawBatch::new();
2022-02-02 09:45:19 -05:00
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();
2022-02-02 11:52:19 -05:00
let item_components = gs.ecs.read_storage::<Item>();
2022-02-02 09:45:19 -05:00
let names = gs.ecs.read_storage::<Name>();
let dm = gs.ecs.fetch::<MasterDungeonMap>();
let obfuscated = gs.ecs.read_storage::<ObfuscatedName>();
2022-02-02 11:52:19 -05:00
let mut items: Vec<(Entity, String)> = Vec::new();
(&entities, &item_components)
.join()
.filter(|(item_entity, _item)| {
2022-02-02 09:45:19 -05:00
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
})
2022-02-02 11:52:19 -05:00
.for_each(|item| items.push((item.0, get_item_display_name(&gs.ecs, item.0))));
2022-02-02 09:45:19 -05:00
2022-02-02 11:52:19 -05:00
let result = item_result_menu(
&mut draw_batch,
2022-02-02 09:45:19 -05:00
"Identify Which Item?",
2022-02-02 11:52:19 -05:00
items.len(),
&items,
ctx.key,
2022-02-02 09:45:19 -05:00
);
2022-02-02 11:52:19 -05:00
draw_batch
.submit(6000)
.expect("Failed to batch render Identify menu");
2022-02-02 09:45:19 -05:00
2022-02-02 11:52:19 -05:00
result
2022-02-02 09:45:19 -05:00
}