/// <summary> /// Adds a stack to the first available slot in the container. /// </summary> /// <param name="stack">The stack to add</param> /// <returns>False if there's no room left in the container, true otherwise</returns> public bool Add(StackingModule stack) { // First attempt to combine with an existing stack for (int i = 0; i < Storage.Length; ++i) { if (stack.StacksMatch(Storage[i])) { // Merge stacks stack = Storage[i].CombineStack(stack); if (stack == null) { // Stack was completely merged return(true); } } } // If there's still leftover items, place them in the first empty slot for (int i = 0; i < Storage.Length; ++i) { if (Storage[i] == null) { // Put this entity in storage Storage[i] = stack; stack.Owner.SetParent(Owner); stack.Owner.Hide(); return(true); } } // Container is out of space return(false); }
/// <summary> /// Places a stack in a container slot and returns the stack it replaced, if any. /// </summary> /// <param name="stack">The stack to place</param> /// <param name="slot">The slot to place in</param> /// <returns>The stack that was previously in the slot (may be null)</returns> public StackingModule Place(StackingModule stack, int slot) { StackingModule current = Storage[slot]; if (stack.StacksMatch(current)) { // Merge stacks stack = Storage[slot].CombineStack(stack); // Return whatever is left over from the merge return(stack); } // Put entity in storage, replacing whatever was there Storage[slot] = stack; stack.Owner.SetParent(Owner); stack.Owner.Hide(); return(current); }