public bool addItem(InventoryItem item, int count) { check(); if (!hasSpaceFor(item, count)) { return(false); } for (int i = 0; i < items.Count(); i++) { if (items[i] == null || (items[i].item == item && items[i].count + count < item.getMaxStackSize())) { if (items[i] == null) { items[i] = new ItemInInventory(item, count); items[i].slot = i; } else { items[i].count += count; } check(); return(true); } } throw new Exception("What happened? We got through the for loop without returning?"); }
public void MoveItem(int fromX, int fromY, int toX, int toY) { Debug.Log("Move item"); ItemInInventory itemToMove = null; foreach (var item in inventory) { if (item.x == fromX && item.y == fromY) { itemToMove = item; } } Debug.Log("1"); if (itemToMove == null) { return; } //check space bool haveSpace = CanPuteHere(toX, toY, itemToMove.item.width, itemToMove.item.height); if (haveSpace) { foreach (var item in inventory) { if (item == itemToMove) { Debug.Log("finded item"); item.x = toX; item.y = toY; } } } CreateInventory(); }
public void render(Graphics g, Point p) { int rows = (int)Math.Ceiling((double)inventory.items.Count() / 10); Rectangle rectangle = new Rectangle(p, new Size(255, (22 * rows) + 10)); lastRenderedRectangle = rectangle; g.FillRectangle(MainForm.createBrush(Reference.guiColor), rectangle); g.DrawRectangle(MainForm.createPen(Color.FromArgb(127, 127, 127)), rectangle); int column = 0; int row = 0; for (int i = 0; i < inventory.items.Count(); i++) { ItemInInventory item = inventory.items[i]; Point renderLocation = Util.addPoints(p, new Point(column * 25, row * 25)); renderItem(item, renderLocation, g, i); lastRenderedPositions[i] = renderLocation; column++; if (column >= 10) { column = 0; row++; } } }
/// <summary> /// Get the <code>ItemInInventory</code> in the <code>InventoryDrawer</code>. /// </summary> /// <param name="p">The top-left of where the inventory would normall be rendered.</param> /// <returns>The <code>ItemInInventory</code>, or null if it is not within this inventory.</returns> public int getItemAtLocation(Point p) { /*int rows = (int)Math.Ceiling((double)inventory.items.Count() / 10); * int column = 0; * int row = 0; * foreach (ItemInInventory item in inventory.items) { * Point pointToCheck = Util.addPoints(p, new Point(column * 25, row * 25)); * Rectangle rect = new Rectangle(pointToCheck, new Size(25, 25)); * if (rect.Contains(p)) { * return item; * } * column++; * if (column >= 10) { * column = 0; * row++; * } * } * return null;*/ for (int i = 0; i < inventory.items.Count(); i++) { ItemInInventory item = inventory.items[i]; try { int index = i; Rectangle rect = new Rectangle(lastRenderedPositions[index], new Size(25, 25)); if (rect.Contains(p)) { return(i); } } catch (KeyNotFoundException e) { continue; } } return(-1); }
public void OnBeginDrag(PointerEventData eventData) { fromX = Mathf.RoundToInt(GetComponent <RectTransform>().localPosition.x - 10); fromX /= 20; fromY = Mathf.RoundToInt(GetComponent <RectTransform>().localPosition.y + 10); fromY /= -20; foreach (var item in player.inventory) { if (item.x <= fromX && item.x + item.item.width > fromX && item.y <= fromY && item.y + item.item.height > fromY) { itemHeight = item.item.height; itemWidth = item.item.width; itemToMove = item; Debug.Log(itemToMove.item.name + ". w/h: " + itemWidth.ToString() + ":" + itemHeight.ToString()); break; } } image = GetComponent <Image>(); GameObject _border = Instantiate(border); _border.transform.SetParent(this.gameObject.transform); _border.transform.localScale = new Vector3(1, 1, 1); _border.transform.localPosition = new Vector3(0, 0, 0); //itemHeight = Mathf.RoundToInt(GetComponent<RectTransform>().sizeDelta.y / 20); //itemWidth = Mathf.RoundToInt(GetComponent<RectTransform>().sizeDelta.x / 20); }
public void Resize(int size) { collection = new ItemInInventory[size]; for (int i = 0; i < collection.Length; i++) { collection[i] = new ItemInInventory(ScriptableObject.CreateInstance <Item>()); } }
public void MoveItem(ItemInInventory itemToMove, int toX, int toY) { bool haveSpace = CanPuteHere(itemToMove, toX, toY); itemToMove.x = toX; itemToMove.y = toY; CreateInventory(); }
public InventoryHandler(int size, Action <itmAction> refresh) { Refresh += refresh; collection = new ItemInInventory[size]; for (int i = 0; i < collection.Length; i++) { collection[i] = new ItemInInventory(ScriptableObject.CreateInstance <Item>()); } }
private bool AddItem(Inventory.ItemPickup ip, ItemInInventory ii) { if (ip.item != null && ip.item.itemWeight <= ii.placementWear) { ii.placementWear = -ip.item.itemWeight; return(true); } return(false); }
public void Init(ItemInInventory sead) { this.Id = sead.Id; this.ItemPrice = sead.ItemPrice; this.ItemType = sead.ItemType; this.SpritePath = sead.SpritePath; this.ItemName = sead.ItemName; this.ItemCount = sead.ItemCount; this.ItemId = sead.ItemId; }
public int indexOf(ItemInInventory item) { for (int i = 0; i < items.Count(); i++) { if (items[i] == item) { return(i); } } return(-1); }
public int GetChargesForType() { ItemInInventory item = m_unit.Inventory.GetItem(ChargeItemType); if (item == null) { Debug.LogWarning("COULDNT FIND ITEM FOR TYPE " + ChargeItemType); return(0); } return(item.GetCount()); }
public void Init(ItemInInventory fert) { this.Id = fert.Id; this.ItemPrice = fert.ItemPrice; this.ItemType = fert.ItemType; this.SpritePath = fert.SpritePath; this.ItemName = fert.ItemName; this.ItemCount = fert.ItemCount; this.ItemId = fert.ItemId; this.TimeFactor = PlantList.ferts[fert.Id - 1].timeFactor; }
public Item TryGetItem(Item item) { foreach (var ItemInInventory in ItemInventory) { if (ItemInInventory.GetType() == item.GetType()) { return(ItemInInventory); } } return(null); }
public Inventory getParentInventory(ItemInInventory item) { foreach (Inventory inventory in inventories) { if (inventory.items.Contains(item)) { return(inventory); } } return(null); }
public void RemoveAt(int pos) { if (collection[pos].cant <= 1) { collection[pos] = new ItemInInventory(ScriptableObject.CreateInstance <Item>()); } else { collection[pos].cant--; } Rfsh(new itmAction(pos, itmAction.Act.elim)); }
private bool AddItem(Inventory.ItemPickup ip, ItemInInventory ii, out GameObject ipAddPlaceList, out GameObject parentPlace) { if (ip.item.itemWeight <= ii.placementWear) { ii.placementWear = -ip.item.itemWeight; ipAddPlaceList = ip.gameObject; parentPlace = ii.placement; return(true); } ipAddPlaceList = null; parentPlace = null; return(false); }
void Add(Item i, int c) { ItemInInventory it = FindByID(i.GetID()); if (it != null) { it.count += c; } else { inventory.Add(new ItemInInventory(i, c)); } }
public override void OnInspectorGUI() { DrawDefaultInspector(); var control = (Inventory)target; OpenFoldout = EditorGUILayout.Foldout(OpenFoldout,"Start Up Items" ); if(OpenFoldout) { for (int i = 0; i < control.StartUpItems.Count; i++) { var itemInfo = GameManager.Instance.itemDatabase.Get(control.StartUpItems[i].itemType,control.StartUpItems[i].Index); EditorGUILayout.LabelField("Item Name",itemInfo.ItemName); control.StartUpItems[i].itemType = (ItemType)EditorGUILayout.EnumPopup("itemType" ,(Enum)control.StartUpItems[i].itemType); //EditorGUILayout.IntField("Stack Amount",itemInfo.StackAmount); control.StartUpItems[i].Index = EditorGUILayout.IntSlider("Index",control.StartUpItems[i].Index,0,GameManager.Instance.itemDatabase.NumberOfItems(control.StartUpItems[i].itemType) - 1); if(GUILayout.Button("Remove Item")) { control.StartUpItems.RemoveAt(i); } EditorGUILayout.Separator(); } } EditorGUILayout.Separator(); EditorGUILayout.LabelField("Add Your Item below"); EditorGUILayout.Separator(); itemType = (ItemType)EditorGUILayout.EnumPopup("itemType" ,(Enum)itemType); Index = EditorGUILayout.IntSlider("Index",Index,0,GameManager.Instance.itemDatabase.NumberOfItems(itemType) - 1); var MoreItemInfo = GameManager.Instance.itemDatabase.Get(itemType,Index); EditorGUILayout.LabelField("Item Name",MoreItemInfo.ItemName); if(GUILayout.Button("Add Item")) { ItemInInventory newItem; newItem = new ItemInInventory(); newItem.Index = Index; newItem.itemType = itemType; control.StartUpItems.Add(newItem); } }
public void renderItem(ItemInInventory item, Point location, Graphics g, bool forceRender, int index) { if (item != null) { lastRenderedPositions[/*Util.indexOf(item, inventory.items)*/ index] = location; } if ((item == null || item == MainForm.getInstance().movingItem) && !forceRender) { //Don't render it! return; } g.DrawImage(item.getItem().getImage(), Util.addPoints(location, new Point(5, 5))); g.DrawString(item.count.ToString(), MainForm.getNormalFont(10), MainForm.createBrush(Color.LightGray), (PointF)location); }
bool Remove(string id, int c) { ItemInInventory toRemove = FindByID(id); if (toRemove != null && toRemove.count >= c) { toRemove.count -= c; if (toRemove.count == 0) { inventory.Remove(toRemove); } return(true); } return(false); }
public virtual void use(ItemInInventory item) { if (!(item.item is BlockPrototype)) { throw new ArgumentException("The provided item is not a BlockPrototype."); } BlockPrototype b = (BlockPrototype)item.item; /*Point p = MainForm.getInstance().getCursorBlockLocation(); * if (MainForm.getInstance().world.blocks[p.X][p.Y].prototype == BlockPrototype.air) { * Point pos = MainForm.getInstance().getTotalCursorPos(); * MainForm.getInstance().world.blocks[p.X][p.Y] = Block.createNewBlock(this, pos); * item.useUp(1); * }*/ Player player = MainForm.getInstance().player; MainForm mainform = MainForm.getInstance(); World w = mainform.world; Point cursorBlock = mainform.getCursorBlockLocation();//new Point(mainform.player.blockX, mainform.player.blockY);//getCursorBlockLocation(); Block block = w.getBlockAt(cursorBlock.X, cursorBlock.Y); if (block == null) { return; } if (Util.distanceBetween(block.location, mainform.player.location) > 200) { return; } if (block.prototype.id == "OpenTerraria:Air") { w.blocks[cursorBlock.X][cursorBlock.Y].prepareForRemoval(); w.blocks[cursorBlock.X][cursorBlock.Y] = Block.createNewBlock(this, new Point(cursorBlock.X * 20, cursorBlock.Y * 20)); w.updateSkyLightForColumn(cursorBlock.X); } Inventory inventory = MainForm.getInstance().getParentInventory(item); inventory.removeAmount(this, 1); LightingEngine.doFullLightingUpdate(false); }
public bool CanPuteHere(ItemInInventory item, int toX, int toY) { if (toX + item.item.width - 1 > cols) { return(false); } if (toX < 0) { return(false); } if (toY + item.item.height - 1 > rows) { return(false); } if (toY < 0) { return(false); } int[,] map = CreateItemMap(); //temperally delete item from inventory for (int y = item.y; y < item.y + item.item.height; y++) { for (int x = item.x; x < item.x + item.item.width; x++) { map[x, y] = 0; } } for (int y = toY; y < (toY + item.item.height); y++) { for (int x = toX; x < (toX + item.item.width); x++) { if (map[x, y] != 0) { return(false); } } } return(true); }
void MainForm_MouseClick(object sender, MouseEventArgs e) { //See if we're doing the initial locating click if (waitingForInitialClick) { Point p = Util.subtractPoints(MousePosition, this.DesktopLocation); waitingForInitialClick = false; cursorOffset = Util.absoluteValueOf(Util.subtractPoints(p, e.Location)); //Get the difference between what we thought it was and what it acutally was //Util.absoluteValueOf(Util.subtractPoints(e.Location, this.DesktopLocation)); return; } //See if the owner of the click is an Inventory bool foundIt = false; foreach (Inventory inventory in inventories) { //ItemInInventory item = inventory.drawer.getItemAtLocation(e.Location); if (!inventory.drawer.lastRenderedRectangle.Contains(getCursorPos())) { continue; } int index = inventory.drawer.getItemAtLocation(e.Location); if (index != -1) { //We found it if (movingItem == null) { movingItem = inventory.items[index]; } else { try { Inventory movingParent = getParentInventory(movingItem); ItemInInventory item = inventory.items[index]; inventory.items[index] = movingItem; int slot = movingItem.slot; movingItem = null; movingParent.items[slot] = item; } catch (Exception ex) { //Lose the item movingItem = null; } } foundIt = true; break; } } if (foundIt) { return; } if (movingItem != null) { movingItem.use(); return; } if (player.hotbar.items[player.hotbarSelectedIndex] != null) { player.hotbar.items[player.hotbarSelectedIndex].use(); return; } }
public bool contains(ItemInInventory i) { return(indexOf(i) != -1); }
public void renderItem(ItemInInventory item, Point location, Graphics g, int index) { renderItem(item, location, g, false, index); }