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