public static void OnItemStackSplit(MyEntityInventoryComponent inv, Vector2I index, float percentage, MyEntityInventoryComponent newInv, Vector2I newIndex) { var s1 = inv.GetItemStackAt(index); var s2 = newInv.GetItemStackAt(newIndex); if (s1.NumItems == 1) { OnItemStackMoved(inv, index, newInv, newIndex); return; } if (s2 == null) { s2 = OnItemStackCreated(s1.DefinitionId.Id, 0); } if (CanMoveToItemStack(s1, s2)) { MoveItemsToStack(s1, s2, Mathf.FloorToInt(s1.NumItems * percentage)); } if (s1 != null && s1.NumItems != 0) { inv.AddItemStackAt(s1, index); } if (s2 != null && s2.NumItems != 0) { newInv.AddItemStackAt(s2, newIndex); } }
private void DrawInventoryAtPosition(MyEntityInventoryComponent c, Vector2 screenPosition, string name) { var overlay = Instantiate(UIObjects[0].Prefab, this.transform); overlay.transform.localPosition = screenPosition; overlay.name = string.Format("Inventory: {0}", name); for (int x = 0; x < c.InvSize_x; x++) { for (int y = 0; y < c.InvSize_y; y++) { var drop = Instantiate(UIObjects[1].Prefab, overlay.transform); drop.transform.localPosition = Utility.IndexToWorldSpacePosition(x, y, GraphicSize, c.InvSize_x, c.InvSize_y); drop.name = string.Format("{0}:{1}", x, y); var d = drop.GetComponentInParent <Dropzone>() as Dropzone; d.Inventory = c; d.Index = new Vector2I(x, y); d.PointerEnter += OnPointerEnter; d.PointerExit += OnPointerExit; if (c.IsItemStackAt(x, y)) { var s = c.GetItemStackAt(x, y) as Stack; var graphic = Instantiate(UIObjects[2].Prefab, drop.transform); graphic.transform.localPosition = Vector2.zero; graphic.name = string.Format(s.DefinitionId.ToString()); var i = graphic.GetComponentInChildren <Interfacable>(); i.PointerDown += OnPointerDown; i.BeginDrag += OnBeginDrag; i.EndDrag += OnEndDrag; graphic.GetComponentInChildren <Image>().sprite = Sprites[s.DefinitionId.Id]; graphic.GetComponentInChildren <Text>().text = c.GetItemStackAt(x, y).NumItems.ToString(); } } } }
public static void OnItemStackMoved(MyEntityInventoryComponent inv, Vector2I index, MyEntityInventoryComponent newInv, Vector2I newIndex) { if (inv.IsItemStackAt(index) == false || (inv == newInv && index == newIndex)) { return; } var s1 = inv.RemoveItemStackFrom(index); var s2 = newInv.RemoveItemStackFrom(newIndex); if (CanMergeItemStacks(s1, s2, 1F)) { MoveItemsToStack(s2, s1, s2.NumItems); } else if (CanMoveToItemStack(s1, s2)) { MoveItemsToStack(s2, s1, s1.ItemAddability); } if (s1 != null && s1.NumItems != 0) { newInv.AddItemStackAt(s1, newIndex); } if (s2 != null && s2.NumItems != 0) { inv.AddItemStackAt(s2, index); } }