/// <summary> /// Takes a stack from a container slot. /// </summary> /// <param name="slot">The slot to take from</param> /// <returns>The stack taken (may be null)</returns> public StackingModule Take(int slot) { StackingModule current = Storage[slot]; Storage[slot] = null; return(current); }
/// <summary> /// Combines a stack into this one and returns a leftover stack if there is any. /// </summary> /// <param name="other">The stack to combine</param> /// <returns>Leftover stack (may be null)</returns> public StackingModule CombineStack(StackingModule other) { // If either stack is maxed out there's nothing to be done if (IsMaxed || other.IsMaxed) { return(other); } // Combine the two stacks Amount += other.Amount; if (Amount > MaxAmount) { // Cap the amount to the value in MaxAmount and spill it over back to the original stack other.Amount = Amount - MaxAmount; Amount = MaxAmount; } else { // Remove the other entity if the stack was completely merged other.Owner.Remove(); other = null; } return(other); }
/// <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); }
public bool StacksMatch(StackingModule other) { if (other == null) { return(false); } return(Owner.Name == other.Owner.Name); }
/// <summary> /// Takes half the units from a stack in a container slot. /// </summary> /// <param name="slot">The slot to take from</param> /// <returns>The units taken (may be null)</returns> public StackingModule TakeHalf(int slot) { StackingModule current = Storage[slot]; if (current != null) { if (current.Amount > 1) { return(current.TakeFromStack(current.Amount / 2)); } } return(Take(slot)); }
public StackingModule TakeFromStack(int amount) { if (amount >= Amount) { return(this); } Entity clone = Owner.Clone(); Amount -= amount; StackingModule stack = clone.Stacking; stack.Amount = amount; return(stack); }
public void DropAll() { Vector2 drop = Vector2.right; for (int i = 0; i < Storage.Length; ++i) { StackingModule item = Storage[i]; if (item != null) { item.Owner.SetParent(null); item.Owner.Position = Storage[i].Owner.Position + drop; item.Owner.Hide(false); drop = Quaternion.Euler(0, 0, -15) * drop; } } }
/// <summary> /// Searches for a stack and removes it from this inventory if found. /// Use this only when the stack is being moved out of the inventory externally to "clean up" its inventory slot. /// </summary> /// <param name="item">The stack to remove</param> /// <returns>True if the stack was found in the storage and removed (or if it was null), false otherwise.</returns> public bool Remove(StackingModule item) { if (item == null) { return(true); } for (int i = 0; i < Storage.Length; ++i) { if (Storage[i] == item) { Storage[i] = null; return(true); } } 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); }