public void SetSlotWithoutNotifyingInventory(ItemSaveFile newItem) { if (newItem != null && newItem.ID != ItemID.Empty) { //Status currentItem = ItemDirectory.GetItem(newItem.ID); itemHasCooldown = currentItem.HasCooldown; hasValidItem = true; //Description descriptionText.text = currentItem.description; itemNameText.text = currentItem.itemName; //Set icon image.sprite = currentItem.icon; countText.text = (currentItem.IsStackable && newItem.stacks > 1) ? newItem.stacks.ToString() : string.Empty; //Set reference this.itemFile = newItem; } else { ClearSlotWithoutNotifyingInventory(); } }
void ArrayPopulate(ItemSaveFile[] array, int length) { for (int i = 0; i < length; i++) { array[i] = new ItemSaveFile(); } }
public void SlotRequest_ForceAssignItem_NonSwapping(ItemSaveFile newFile, int slotIndex) { //A brute force assignment of item, not asing to swap out any file. This method is used when preverifications have all being made. It's not a perfect solution but it works for now. Item item = GetItemFromID(newFile.ID); //Just put it in if this slot is empty if (SlotIsEmpty(slotIndex)) { SetItemFileAt(newFile, slotIndex); OnItemSlotted(slotIndex); InvokeEvent_SlotChange(slotIndex); } else //If not empty, try stacking { if (item.IsStackable && ItemsAreTheSame(itemList[slotIndex], item)) { itemList[slotIndex].stacks += newFile.stacks; InvokeEvent_SlotChange(slotIndex); } else //If not stackable then OVERRIDE IT { SetItemFileAt(newFile, slotIndex); OnItemSlotted(slotIndex); InvokeEvent_SlotChange(slotIndex); } } }
public void ReorderingInventory(int[] sortingMap) { //Select the items where the ID is not empty, and then get the item from the item directory //Create an ordering tabel to associate the itemSaveFiles with an integer representing their itemType (enum). List <(ItemSaveFile, int)> orderingTable = new List <(ItemSaveFile, int)>(); foreach (ItemSaveFile item in itemList) { //Fill the table with valid entries if (item != null && item.ID != ItemID.Empty) { orderingTable.Add((item, ItemDirectory.GetItemTypeInt(item.ID))); } } //Order the table using an integer array sorting map orderingTable = orderingTable.OrderBy(x => sortingMap[(x.Item2)]).ToList(); //Update the itemList with these values for (int i = 0; i < itemList.Length; i++) { if (i < orderingTable.Count) { itemList[i] = new ItemSaveFile(orderingTable[i].Item1.ID, orderingTable[i].Item1.stacks); } else { ClearSlotWithoutNotify(i); } } }
//shop only public bool SlotRequest_CheckIfCanReleaseFile(ItemSaveFile file, int slotIndex) { if (file != null && file.ID != ItemID.Empty) { Item item = GetItemFromID(file.ID); return((releasingCondition == null || releasingCondition(item, slotIndex)) ? true : false); } return(false); }
public bool SlotRequest_TryStackItem(ItemSaveFile newFile, int slotIndex) { if (itemList[slotIndex].ID == newFile.ID && ItemDirectory.GetItem(newFile.ID).IsStackable) { itemList[slotIndex].stacks += newFile.stacks; InvokeEvent_SlotChange(slotIndex); return(true); } return(false); }
void Start() { itemList = new ItemSaveFile[slotManager.SlotCount]; slotManager.Initialize(this); TryPickUpItem(new ItemSaveFile(ItemID.potion, 3)); TryPickUpItem(new ItemSaveFile(ItemID.weapon, 1)); TryPickUpItem(new ItemSaveFile(ItemID.armor, 2)); TryPickUpItem(new ItemSaveFile(ItemID.potion, 3)); }
public void StartDrag(UIItemSlot uiSlot, ItemSaveFile itemFile) { if (itemFile != null && itemFile.ID != ItemID.Empty) { IsDragging = true; startingSlot = uiSlot; startingFile = itemFile; image.sprite = ItemDirectory.GetItem(itemFile.ID).icon; image.enabled = true; } }
bool TryStackItemInAnyslot(ItemSaveFile newItem) { //See if the item already exists in the inventory foreach (var i in itemList) { if (i != null && i.ID == newItem.ID) { i.stacks += newItem.stacks; return(true); } } return(false); }
bool TryAddToNextEmptySlot(ItemSaveFile newItem) { for (int i = 0; i < itemList.Length; i++) { if (SlotIsEmpty(i)) { itemList[i] = new ItemSaveFile(newItem); return(true); } } return(false); }
bool TryGetItem(out Item item) { item = null; ItemSaveFile itemSaveFile = inventory.ItemList[slotIndex]; if (itemSaveFile != null) { ItemID id = itemSaveFile.ID; if (id != ItemID.Empty) { item = ItemDirectory.GetItem(itemSaveFile.ID); return(true); } } return(false); }
protected void Awake() { //Initialize approval = new Approval(); playerBodyLayer = CharacterSettings.instance.PlayerBodyLayer; itemList = new ItemSaveFile[Inventory.NPCShopSlots]; for (int i = 0; i < itemList.Length; i++) { if (sellingItems.Count > i && sellingItems[i] != ItemID.Empty) { itemList[i] = new ItemSaveFile(sellingItems [i], 1); } } }
public void ClearSlotWithoutNotifyingInventory() { //image.enabled = false; image.sprite = null; countText.text = string.Empty; itemFile = null; //Status currentItem = null; hasValidItem = false; itemHasCooldown = false; //Description descriptionText.text = String.Empty; itemNameText.text = String.Empty; cooldownMask.fillAmount = 0f; }
public virtual bool TryPickUpItem(ItemSaveFile newItemFile) { if (newItemFile != null && newItemFile.ID != ItemID.Empty) { Item item = GetItemFromID(newItemFile.ID); //If the item is stackable, try stack it if (item.IsStackable && TryStackItemInAnyslot(newItemFile)) { InvokeEvent_InventoryChange(); return(true); } //Add to next empty slot if (TryAddToNextEmptySlot(newItemFile)) { InvokeEvent_InventoryChange(); return(true); } } return(false); }
public ItemSaveFile(ItemSaveFile clone) { ID = clone.ID; stacks = clone.stacks; }
public void StopDrag(GameObject endObject) { if (!IsDragging || !startingSlot.CanReleaseFile(startingFile)) { return; } IsDragging = false; image.enabled = false; /* Potential outcomes in drag-and-drop: * A - item is dropped on the ground * B - Receiver has no item and simply takes the sender's file * C1 - 2 items swap successfull * C2 - 2 items stacked successfully * C3. Sender file can't be accepted by receiver OR * Receiver cann't accept the sender's returning file */ //If we didn't drag onto a slot, then drop the item if (endObject == null) { startingSlot.DragAndDraop_DropItemOnGroundAndNotifyInventory(); } else { UIItemSlot endSlot = endObject.GetComponent <UIItemSlot>(); if (endSlot == null) { //A - Drop item startingSlot.DragAndDraop_DropItemOnGroundAndNotifyInventory(); } else { ItemSaveFile endFile = endSlot.ItemFile; //If there is an end slot and it accepts this file type... if (endSlot.CanAcceptFile(startingFile)) { if (IsFileEmpty(endFile)) { //B - If the slot is empty then just add it endSlot.DragAndDrop_ForceAssignFile_AndNotifyInventory(startingFile); startingSlot.DragAndDrop_ClearSlotAndNotifyInventory(); } else //...but if there is already an item here... { //...First check if we can stack, and if not... if (endSlot.DragAndDrop_TryStackingItems_AndNotifyInventory(startingFile)) { startingSlot.DragAndDrop_ClearSlotAndNotifyInventory(); } else { // ... check if we can swap... if (startingSlot.CanAcceptFile(endSlot.ItemFile)) { endSlot.DragAndDrop_ForceAssignFile_AndNotifyInventory(new ItemSaveFile(startingFile.ID, startingFile.stacks)); startingSlot.DragAndDrop_ForceAssignFile_AndNotifyInventory(new ItemSaveFile(endFile.ID, endFile.stacks)); } } } } } } }
protected bool ItemsAreTheSame(ItemSaveFile itemFile, Item item) => item.ID == itemFile.ID;
public void ClearSlotWithoutNotify(int slot) => itemList[slot] = new ItemSaveFile(ItemID.Empty, 1);
protected void SetItemFileAt(ItemSaveFile itemFile, int slot) => itemList[slot] = itemFile;
//"Notify inventory" is just saying, we're not just changing the ui image and text, we want the save data to change public bool DragAndDrop_TryStackingItems_AndNotifyInventory(ItemSaveFile newFile) { return(inventory.SlotRequest_TryStackItem(newFile, slotIndex)); }
public void DragAndDrop_ForceAssignFile_AndNotifyInventory(ItemSaveFile newFile) { inventory.SlotRequest_ForceAssignItem_NonSwapping(newFile, slotIndex); }
public bool CanReleaseFile(ItemSaveFile file) => inventory.SlotRequest_CheckIfCanReleaseFile(file, slotIndex);
bool IsItemFileEmpty(ItemSaveFile item) => item == null || item.ID == ItemID.Empty;
public static void SpawnItemAtPlayerPosition(ItemSaveFile file) { SpawnItem(file, PlayerController.Instance.transform.position); }
public static void SpawnItem(ItemSaveFile file, Vector3 position) { Vector3 p = new Vector3(position.x + Random.Range(-5f, 5f), position.y + 0.5f, position.z + Random.Range(-1f, 1f)); Instantiate(GetItem(file.ID), p, Quaternion.identity).GetComponent <Item>().stacks = file.stacks; }
bool IsFileEmpty(ItemSaveFile file) => file == null || file.ID == ItemID.Empty;