makes keyhandling debug msgs optional

This commit is contained in:
Llywelwyn 2023-10-05 04:38:34 +01:00
parent bc4f960d88
commit a92f60bb15

View file

@ -20,7 +20,7 @@ use crate::invkeys::*;
pub struct KeyHandling {}
const DEBUG_KEYHANDLING: bool = true;
const DEBUG_KEYHANDLING: bool = false;
impl<'a> System<'a> for KeyHandling {
#[allow(clippy::type_complexity)]
@ -75,28 +75,40 @@ impl<'a> System<'a> for KeyHandling {
),
);
if stacks {
if DEBUG_KEYHANDLING {
console::log(&format!("KEYHANDLING: Item is stackable."));
}
let maybe_key = item_exists(&unique);
if maybe_key.is_some() {
if DEBUG_KEYHANDLING {
console::log(&format!("KEYHANDLING: Existing stack found for this item."));
}
let key = maybe_key.unwrap();
keys.insert(e, Key { idx: key }).expect("Unable to insert Key.");
if DEBUG_KEYHANDLING {
console::log(&format!("KEYHANDLING: Assigned key idx {} to item.", key));
}
handled = true;
}
}
if !handled {
if DEBUG_KEYHANDLING {
console::log(
&format!("KEYHANDLING: Item is not stackable, or no existing stack found.")
);
}
if let Some(idx) = assign_next_available() {
if DEBUG_KEYHANDLING {
console::log(
&format!("KEYHANDLING: Assigned next available index {} to item.", idx)
);
}
keys.insert(e, Key { idx }).expect("Unable to insert Key.");
register_stackable(stacks, unique, idx);
} else {
if DEBUG_KEYHANDLING {
console::log(&format!("KEYHANDLING: No more keys available."));
}
gamelog::Logger
::new()
.append(messages::NO_MORE_KEYS)
@ -113,37 +125,47 @@ impl<'a> System<'a> for KeyHandling {
}
// If the item is *not* stackable, then we can just remove the key and clear the index.
if let None = stackable.get(e) {
if DEBUG_KEYHANDLING {
console::log(
&format!("KEYHANDLING: Item is not stackable, clearing index {}.", idx)
);
}
clear_idx(idx);
keys.remove(e);
continue;
}
// If the item *is* stackable, then we need to check if there are any other items that
// share this key assignment, before clearing the index.
if DEBUG_KEYHANDLING {
console::log(
&format!(
"KEYHANDLING: Item is stackable, checking if any other items share this key."
)
);
}
let mut sole_item_with_key = true;
for (entity, key) in (&entities, &keys).join() {
if entity != e && key.idx == idx {
if DEBUG_KEYHANDLING {
console::log(&format!("KEYHANDLING: Another item shares index {}", idx));
}
sole_item_with_key = false;
break;
}
}
// If no other items shared this key, free up the index.
if sole_item_with_key {
if DEBUG_KEYHANDLING {
console::log(
&format!("KEYHANDLING: No other items found, clearing index {}.", idx)
);
}
clear_idx(idx);
}
// Either way, remove the key component from this item, because we're dropping it.
if DEBUG_KEYHANDLING {
console::log(&format!("KEYHANDLING: Removing key component from item."));
}
keys.remove(e);
}