/// <summary> /// Remove an item from the stack and /// prepare it for the scene- this will /// activate the object and clear it from /// the stack. /// </summary> /// <param name="index">The position in the stack to retrieve /// the item</param> /// <returns>A new `Stackable` stack</returns> private Stackable Get(int index) { Stackable stack = Stack[index]; stack.gameObject.SetActive(true); Stack.RemoveAt(index); return(stack); }
/// <summary> /// Creates a `StackableSplitter` directly on /// top of this stack and sets its parent to /// the canvas. /// </summary> /// <param name="stack">The `Stackable` to create a `StacakbleSplitter` for</param> public static void Create(Stackable stack) { GameObject stackableSplitterResource = Resources.Load(stackableSplitterPath) as GameObject; GameObject stackableSplitterObject = GameObject.Instantiate(stackableSplitterResource, stack.transform.position, Quaternion.identity) as GameObject; stackableSplitterObject.transform.SetParent(CanvasHelper.GetCanvas().transform); StackableSplitter stackableSplitter = stackableSplitterObject.GetComponent <StackableSplitter>(); stackableSplitter.Stack = stack; }
/// <summary> /// Returns true if otherStack can be stacked /// onto this stack /// </summary> /// <param name="otherStack">The `Stackable` that may or may not /// be able to be stacked</param> /// <returns>True if passed `Stackable` can be stacked /// on this `Stackable`</returns> public bool CanStack(Stackable otherStack) { if (otherStack.GetType() != GetType()) { return false; } if (Size() >= max - 1 || Size() + otherStack.Size() >= max - 1) { return false; } return true; }
/// <summary> /// Returns true if otherStack can be stacked /// onto this stack /// </summary> /// <param name="otherStack">The `Stackable` that may or may not /// be able to be stacked</param> /// <returns>True if passed `Stackable` can be stacked /// on this `Stackable`</returns> public bool CanStack(Stackable otherStack) { if (otherStack.GetType() != GetType()) { return(false); } if (Size() >= max - 1 || Size() + otherStack.Size() >= max - 1) { return(false); } return(true); }
/// <summary> /// Listens for presses to `KeyCode.Return` or /// `KeyCode.KeypadEnter` and will then /// attempt to retrieve the entered number of /// items from the `Stackable` and kick-off /// the `OnBeginDrag` event on it. /// </summary> public void Update() { // if they lose focus on the splitter, // destroy it if (!inputField.isFocused) { Destroy(gameObject); } // also destroy it if they hit escape if (Input.GetKeyDown(KeyCode.Escape)) { Destroy(gameObject); } // `Enter` or `Return` has been pressed, // get the value of what was entered and // retrieve that number of items from the // current stack if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)) { int numberToRemove = int.Parse(inputField.text); if (numberToRemove == 0) { return; } Stackable newStack = Stack.Remove(numberToRemove); Slot parentSlot = Stack.GetParentSlot(); Draggable newDraggableStack = newStack.GetComponent <Draggable>(); newDraggableStack.OnBeginDrag(null); // if the item that was asked to be split is not // being dragged, put it back in it's parent // slot. Otherwise, it will also start being // dragged Draggable thisDraggable = Stack.GetComponent <Draggable>(); if (!thisDraggable.IsBeingDragged()) { parentSlot.Item = thisDraggable; } } }
/// <summary> /// Add an item to this stack.If a `Stackable` item /// is added that has several children, will iterate /// through the children first and add them. Then, /// it will add the container of the stack. /// If the size of both stacks is larger than the max /// allowed, will throw a `NotStackableException`. /// </summary> /// <param name="stackable">The `Stackable` item to stack</param> public void Add(Stackable stackable) { if (stackable.GetType() != GetType()) { throw new NotStackableException("Unable to stack, these items are not of the same type"); } if (Size() >= max - 1 || Size() + stackable.Size() >= max - 1) { throw new NotStackableException("Unable to stack: " + this + " with " + stackable); } foreach(Stackable s in stackable.Stack) { Add(s); } stackable.transform.SetParent(transform); stackable.gameObject.SetActive(false); stackable.Stack.Clear(); Stack.Add(stackable); UpdateCountLabel(); }
/// <summary> /// Add an item to this stack.If a `Stackable` item /// is added that has several children, will iterate /// through the children first and add them. Then, /// it will add the container of the stack. /// If the size of both stacks is larger than the max /// allowed, will throw a `NotStackableException`. /// </summary> /// <param name="stackable">The `Stackable` item to stack</param> public void Add(Stackable stackable) { if (stackable.GetType() != GetType()) { throw new NotStackableException("Unable to stack, these items are not of the same type"); } if (Size() >= max - 1 || Size() + stackable.Size() >= max - 1) { throw new NotStackableException("Unable to stack: " + this + " with " + stackable); } foreach (Stackable s in stackable.Stack) { Add(s); } stackable.transform.SetParent(transform); stackable.gameObject.SetActive(false); stackable.Stack.Clear(); Stack.Add(stackable); UpdateCountLabel(); }
/// <summary> /// Remove the specified number of items /// from this stack.Will return `this` if /// count requested is larger (or equal to) /// current size. /// /// If not, will pop off the top of the stack /// and create a new `Stackable` that will then /// be returned. /// </summary> /// <param name="requestedCount">The number of `Stackable` /// items to return</param> /// <returns>A new `Stackable`</returns> public Stackable Remove(int requestedCount) { if (requestedCount - 1 >= Size()) { return(this); } Stackable baseStackable = Get(0); if (requestedCount > 1) { List <Stackable> otherStackables = Stack.GetRange(0, requestedCount - 1); Stack.RemoveRange(0, requestedCount - 1); foreach (Stackable s in otherStackables) { baseStackable.Add(s); } } UpdateCountLabel(); baseStackable.UpdateCountLabel(); return(baseStackable); }
/// <summary> /// Returns true if otherStack is of same /// type. This should eventually be replaced /// by a custom comparator /// </summary> /// <param name="otherStack">The `Stackable` to compare types with</param> /// <returns>True if passed `Stackable` is of same type</returns> public bool IsSameType(Stackable otherStack) { return otherStack.GetType() == GetType(); }
/// <summary> /// Returns true if otherStack is of same /// type. This should eventually be replaced /// by a custom comparator /// </summary> /// <param name="otherStack">The `Stackable` to compare types with</param> /// <returns>True if passed `Stackable` is of same type</returns> public bool IsSameType(Stackable otherStack) { return(otherStack.GetType() == GetType()); }