public void Set(int itemID, Vector3 pos) { var item = ItemMgr.GetItem(itemID); this.name.text = item.ToString(); m_TransFrom.position = pos; }
private void OnTouchItem(Slot slot) { var item = ItemMgr.GetItem(slot.itemUi.itemId); this.itemInfoUi.Set(slot.itemUi.itemId, slot.transform.position); this.itemInfoUi.Show(); this.moreInfoTextObj.GetComponent <Text>().text = item.ToString(); var sprite = MemoryMgr.GetSourceFromResources <Sprite>(item.SpritePath); Assert.IsTrue(sprite); this.moreInfoSpriteObj.GetComponent <Image>().sprite = sprite; }
private void InitItemList() { GameObject itemObj; foreach (var id in itemIdList) { itemObj = MemoryMgr.InstantiateGameObject(DirPath.LittleUiDir + this.itemDataPath, this.itemList); Assert.IsTrue(itemObj); var itemInfo = ItemMgr.GetItem <AbstractCommodity>(id); itemObj.transform.Find("Image_ItemAvator").GetComponent <Image>().sprite = MemoryMgr.GetSourceFromResources <Sprite>(itemInfo.SpritePath); itemObj.transform.Find("Tmp_Price").GetComponent <TextMeshProUGUI>().text = itemInfo.price.ToString(); this.items.Add(itemObj); } }
/// <summary> /// 从fromIndex寻找第一个没有装满的id相同的物品 /// </summary> /// <param name="itemId"></param> /// <param name="fromIndex"></param> /// <returns></returns> private Slot FindSlotNotFill(int itemId, int fromIndex = 0) { for (int i = fromIndex; i < this.slots.Length; i++) { if (!slots[i].IsEmpty && slots[i].itemUi.itemId == itemId) { if (slots[i].ItemCount == ItemMgr.GetItem(slots[i].itemUi.itemId).Capacity) { continue; } else { return(slots[i]); } } } return(null); }
/// <summary> /// 移除物品 /// </summary> /// <param name="itemId"></param> /// <param name="count"></param> public void RemoveItem(int itemId, int count) { var slot = FindSlot(itemId); if (null == slot) { Debug.Log("没有那么多的" + ItemMgr.GetItem(itemId).Name); } else { if (count <= slot.ItemCount) { slot.RemoveItem(count); } else { TryRemoveManyItem(itemId, count); } } }
private bool TryStoreManyItem(int itemId, int count) { int num = count; int capacity = ItemMgr.GetItem(itemId).Capacity; Assert.IsTrue(count > capacity); while (num > capacity) { var slot = FindEmptySlot(); if (slot) { slot.AddItem(itemId, capacity); num -= capacity; } else { return(false); } } if (num == 0) { return(true); } else { var slot = FindEmptySlot(); if (slot) { slot.AddItem(itemId, num); return(true); } { return(false); } } }
/// <summary> /// 添加物品 /// </summary> /// <param name="itemId"></param> /// <param name="count"></param> public void AddItem(int itemId, int count) { int capcity = ItemMgr.GetItem(itemId).Capacity; //查看是否存有同种物品 var slot = FindSlotNotFill(itemId); if (slot) { //判断是否超过单格最大容量 if (slot.ItemCount + count > capcity) { Debug.Log("超量!"); var okCount = capcity - slot.ItemCount; slot.AddItem(okCount); //寻找新格子存放剩余物品 var newSlot = FindEmptySlot(); if (newSlot == null) { Debug.Log("无法存储更多物品"); } else { var leftCount = count - okCount; //剩余数量是否仍然超量 if (leftCount >= capcity) { if (!TryStoreManyItem(itemId, leftCount)) { Debug.Log("无法存储更多物品"); } } else { newSlot.AddItem(itemId, leftCount); } } } else//不会超过单格最大容量 { Debug.Log("没有超量"); slot.AddItem(count); } } else//没有同种物品 { var newSlot = FindEmptySlot(); if (newSlot == null) { Debug.Log("无法存储更多物品"); } else { //数量是否超量 if (count > capcity) { if (!TryStoreManyItem(itemId, count)) { Debug.Log("无法存储更多物品"); } } else { newSlot.AddItem(itemId, count); } } } }