public void AddItem(IItem itemToBeAdded) { if (this.IsFull()) { throw new BackpackFullException(InventoryMessages.BackpackFullMessage); } CommonSlot currentSlot = this.slots.First(x => x.IsEmpty); currentSlot.PutItem(itemToBeAdded); }
public void Give(string id, int amount = 1) { var item = ItemManager.Instance[id]; var stack = new ItemStack(id, amount); if (item == null) { throw new ArgumentException($"Invalid Item ID '{id}'"); } //重複してるところに入れてみる foreach (var i in this) { if (i.Id == id) { i.Add(amount); return; } } //なければ空いているところに入れてみる if (item is IItemKey) { KeyItemSlot.Add(stack); return; } if (item is IFoodItem) { var index = FoodSlot.Select((x, i) => new { x, i }) .FirstOrDefault(o => o.x == default(ItemStack)); if (index != null) { FoodSlot[index.i] = stack; } } // otherwise { var index = CommonSlot.Select((x, i) => new { x, i }) .FirstOrDefault(o => o.x == default(ItemStack)); if (index != null) { CommonSlot[index.i] = stack; } } //それでもダメならエラー投げる throw new InventoryIsFullException(); }
public IEnumerator <ItemStack> GetEnumerator() => CommonSlot .Concat(FoodSlot) .Concat(BodySlot) .Concat(KeyItemSlot) .GetEnumerator();