public virtual int OnBuy(BagInfo bag, int buy_num) { if (buy_num <= 0) { return(0); } if (bag.Money < BuyPrice * buy_num) { throw new System.Exception("金钱不足"); } int finallyBuy = 0; if (StackAble) { ItemInfo itemInBag = bag.itemList.Find(i => i.ItemID == ID); try { if (itemInBag != null) { finallyBuy = itemInBag.Quantity; bag.GetItem(this, buy_num); itemInBag = bag.itemList.Find(i => i.ItemID == ID); finallyBuy = Mathf.Abs(itemInBag.Quantity - finallyBuy); } else { //finallyBuy = MaxCount > buy_num ? buy_num : MaxCount; bag.GetItem(this, buy_num); itemInBag = bag.itemList.Find(i => i.ItemID == ID); if (itemInBag != null) { finallyBuy = itemInBag.Quantity; } } bag.LoseMoney(BuyPrice * finallyBuy); } catch (System.Exception ex) { throw new System.Exception(ex.Message); } } else { try { finallyBuy = bag.itemList.FindAll(i => i.ItemID == ID).Count; bag.GetItem(this, buy_num); finallyBuy = bag.itemList.FindAll(i => i.ItemID == ID).Count - finallyBuy; bag.LoseMoney(BuyPrice * finallyBuy); } catch (System.Exception ex) { throw new System.Exception(ex.Message); } } return(finallyBuy); }
public void Make(BagInfo bag, int num) { if (num <= 0) { return; } CheckMaxMake(bag); if (num > MaxMake) { throw new System.Exception("无法制作该数量的该物品"); } if (bag.Money < Cost * num) { throw new System.Exception("制作费用不足"); } if (bag.IsMax) { throw new System.Exception("行囊空间不足"); } if (bag.IsMaxWeight) { throw new System.Exception("超重状态无法制作"); } if ((bag.Current_Weight + Item.Weight * num) / bag.MaxWeight >= 1.5f) { throw new System.Exception("该物品太重了"); } Check(bag, num); if (!MakeAble) { throw new System.Exception("材料不足,无法制作"); } foreach (MaterialInfo minfo in Item.MaterialsList) { ItemBase find = bag.itemList.Find(m => m.ItemID == minfo.Material.ID).Item; if (find == null) { throw new System.Exception("找不到该材料"); } bag.LoseItem(find, minfo.Required * num); } bag.GetItem(Item, num); bag.LoseMoney(Cost * num); }
public int TakeOutItem(ItemInfo item, BagInfo bagInfo, int take_num) { if (item == null || take_num <= 0) { return(0); } if (Current_Size <= 0) { throw new System.Exception("仓库为空"); } ItemInfo tempitem = itemList.Find(i => i.Item == item.Item); if (tempitem == null) { throw new System.Exception("该物品未在仓库中"); } if (tempitem.Quantity <= 0) { throw new System.Exception("该物品为空"); } int finallyTake = tempitem.StackAble ? tempitem.Quantity - take_num > 0 ? take_num : tempitem.Quantity : 1; ItemInfo itemInBag = bagInfo.itemList.Find(i => i.ItemID == item.ItemID); int temp = 0; if (itemInBag != null) { temp = itemInBag.Quantity; } bagInfo.GetItem(item.Item, finallyTake); itemInBag = bagInfo.itemList.Find(i => i.ItemID == item.ItemID); temp = itemInBag.Quantity - temp; finallyTake = finallyTake > temp ? temp : finallyTake; tempitem.Quantity -= finallyTake; if (tempitem.Quantity <= 0) { Current_Size -= 1; itemList.Remove(tempitem); } if (Current_Size < MaxSize) { IsMax = false; } return(finallyTake); }
public void GetItem(ItemBase item, int get_num) { if (!isInit) { return; } if (bagInfo == null || item == null || get_num <= 0) { return; } int finallyGet = 0; if (item.StackAble) { finallyGet = bagInfo.itemList.Find(i => i.ItemID == item.ID) == null ? 0 : bagInfo.itemList.Find(i => i.ItemID == item.ID).Quantity; } List <ItemInfo> itemsBefore = bagInfo.itemList.FindAll(i => i.ItemID == item.ID); try { bagInfo.GetItem(item, get_num); } catch (System.Exception ex) { NotificationManager.Instance.NewNotification(ex.Message); return; } List <ItemInfo> itemsAfter = bagInfo.itemList.FindAll(i => i.ItemID == item.ID); List <ItemInfo> difference = new List <ItemInfo>(); if (itemsBefore.Count > 0) { foreach (ItemInfo info in itemsAfter) { if (itemsBefore.Find(i => i.Item == info.Item) == null) { difference.Add(info); } } } else { difference = itemsAfter; } if (item.StackAble) { finallyGet = bagInfo.itemList.Find(i => i.ItemID == item.ID) == null ? 0 : bagInfo.itemList.Find(i => i.ItemID == item.ID).Quantity - finallyGet; } else { finallyGet = difference.Count; } if (finallyGet > 0) { NotificationManager.Instance.NewNotification("获得了" + finallyGet + "个<color=orange>" + item.Name + "</color>"); } if (item.StackAble && itemAgents.Exists(ic => ic.GetComponent <ItemAgent>().itemInfo.ItemID == item.ID)) { return; } if (difference.Count > 0) { foreach (ItemInfo info in difference) { for (int i = 0; i < bagCells.Count; i++) { if (bagCells[i].transform.childCount <= 0) { ItemAgent tempCell = (Instantiate(itemCellPrefab, bagCells[i].transform) as GameObject).GetComponent <ItemAgent>(); tempCell.itemInfo = info; tempCell.isStored = false; itemAgents.Add(tempCell); break; } } } } }