@tool extends RefCounted class_name InventorySlot @export_storage var held_item : Item: set(value): if held_item == null and value != null: held_item = value amount += 1 return held_item = value get: return held_item @export_storage var amount : int: set(value): if value <= 0: held_item = null amount = 0 return if held_item == null: return if value > held_item.stack_size: amount = held_item.stack_size return amount = value get: return amount @export_storage var filter : Comparable ## Get some amount of items from slot. Returns null if slot is empty func extract_stack(extract_amount: int) -> Stack: if amount == 0: return null var return_stack : Stack if extract_amount > amount: return_stack = Stack.new(held_item,amount) else: return_stack = Stack.new(held_item,extract_amount) self.amount = 0 return return_stack ## Extract entire stack from slot. Returns null if slot is empty func extract() -> Stack: if amount == 0: return null var return_stack : Stack = Stack.new(held_item,amount) self.amount = 0 return return_stack func as_stack() -> Stack: if amount == 0: return null var return_stack : Stack = Stack.new(held_item,amount) return return_stack func can_be_merged(item : Item) -> bool: if filter != null: return filter.is_equal(item) if amount == 0: return true return item == held_item ## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching func merge_stack(stack: Stack) -> Stack: if stack == null or stack.held_item == null or stack.amount == 0: return stack if held_item == null: if filter != null and filter.is_equal(stack.held_item) == false: return stack held_item = stack.held_item amount = stack.amount return null if stack.held_item != held_item: return null var return_stack_amount = max(0,stack.amount-amount) amount += stack.amount - return_stack_amount if return_stack_amount == 0: return null return Stack.new(held_item,return_stack_amount)