public void SlotIn(Slottable sltbl) { DebugUtility.PrintRed("Slot in called"); newSlottable = (Slottable)GameObject.Instantiate(slottablePrefab, Vector3.zero, Quaternion.identity, slotZoneTrans); newSlottable.transform.SetAsLastSibling(); newSlottable.Initialize(null, sltbl.m_itemInstance, sltbl.m_Item, sltbl.m_PickAmount); RectTransform newRT = newSlottable.GetComponent <RectTransform>(); newRT.anchorMin = new Vector2(0f, 1f); newRT.anchorMax = new Vector2(0f, 1f); newRT.anchoredPosition = new Vector2(.5f, .5f); newRT.sizeDelta = new Vector2(80f, 80f); }
public void InitSlots() { /* if isExpandable as many newSlotRects as needed needs to be created to accomodate slottables. * populate the list with pairs of newly created slottables and slotRects * children all the slottables to slotRects */ /* create slottables * initialize and stuck them in a temp list * sort in the order of itemId */ List <Slottable> slottableTempList = new List <Slottable>(); for (int i = 0; i < inventory.entries.Count; i++) { // if(inventory.entries[i].itemInstance != null){ if ((m_slotGroupType != SlotGroupType.Pool && inventory.entries[i].quantity > 0) || m_slotGroupType == SlotGroupType.Pool) { Slottable newSlottable = (Slottable)Instantiate(slottablePrefab, Vector3.zero, Quaternion.identity); newSlottable.Initialize(this, inventory.entries[i].itemInstance, inventory.entries[i].itemInstance.m_item, inventory.entries[i].quantity); slottableTempList.Add(newSlottable); } // } } List <Slottable> orderedTemp = new List <Slottable>(); while (slottableTempList.Count > 0) { int prevId = -1; int idAtMin = -1; for (int i = 0; i < slottableTempList.Count; i++) { if (slottableTempList[i].m_Item.itemId < prevId || prevId == -1) { prevId = slottableTempList[i].m_Item.itemId; idAtMin = i; } } orderedTemp.Add(slottableTempList[idAtMin]); slottableTempList.RemoveAt(idAtMin); } /* populate a temp list of slotRect in the sibling order * create if isExpandable * start with a clean slate in that case */ List <RectTransform> slotRTTempList = new List <RectTransform>(); if (isExpandable) { int numOfSlotsToCreate = inventory.entries.Count; if (panel.childCount != 0) { for (int i = 0; i < panel.childCount; i++) { Destroy(panel.GetChild(i)); } } /* create slots and add them in the temp list */ for (int i = 0; i < numOfSlotsToCreate; i++) { RectTransform newSlotRect = Instantiate(m_slotPrefab, Vector3.zero, Quaternion.identity, panel).GetComponent <RectTransform>(); slotRTTempList.Add(newSlotRect); } } else { for (int i = 0; i < panel.childCount; i++) { RectTransform slotRect = panel.GetChild(i).GetComponent <RectTransform>(); slotRTTempList.Add(slotRect); } } /* make pairs of slottable and slotRect from two temp lists, * and populate m_slots list * do keep in mind that there is possibly empty slots (one without matching slottables) */ if (m_slots != null) { m_slots.Clear(); } else { m_slots = new List <Slot>(); } for (int i = 0; i < slotRTTempList.Count; i++) { Slot newSlot = new Slot(); if (/*orderedTemp[i] == null*/ i >= orderedTemp.Count) { newSlot.slottable = null; } else { newSlot.slottable = orderedTemp[i]; } newSlot.slotRect = slotRTTempList[i]; // newSlot.slottable.m_rectTrans.anchoredPosition = newSlot.slotRect.anchoredPosition; // newSlot.slottable.transform.SetParent(newSlot.slotRect); if (newSlot.slottable != null) { newSlot.slottable.Attach(newSlot.slotRect); newSlot.slottable.InitHierarchyDependents(); newSlot.slottable.CheckQuantityColor(); } m_slots.Add(newSlot); } }
public void UpdateSlots(PointerEventData eventData) { /* create new slots part * search in the current slots for slottable reference * update quantit * if not found * create new and update its position and hierarchy * disable its image component * if the quantity is 0 and not pool * do not include it in the new slots * * Transit part * for each slottable in the current slots * try to spot the new slot referring to the newSlots * if found, start to move toward it * if not, detach it and disable image and leave it there until it is deleted upon completion of transaction */ /* Create part */ m_tempSlots = new List <Slot>(); for (int i = 0; i < m_slots.Count; i++) { Slot newSlot = new Slot(); newSlot.slottable = null; newSlot.slotRect = m_slots[i].slotRect; m_tempSlots.Add(newSlot); } int addedSlotsCount = inventory.entries.Count - m_slots.Count; if (addedSlotsCount > 0) { for (int i = 0; i < addedSlotsCount; i++) { Slot newSlot = new Slot(); newSlot.slottable = null; newSlot.slotRect = Instantiate(m_slotPrefab, Vector3.zero, Quaternion.identity).GetComponent <RectTransform>(); newSlot.slotRect.SetParent(panel); newSlot.slotRect.SetAsLastSibling(); // LayoutGroup panelLayoutGroup = panel.GetComponent<LayoutGroup>(); // if(panelLayoutGroup != null){ // panelLayoutGroup.CalculateLayoutInputHorizontal(); // panelLayoutGroup.CalculateLayoutInputVertical(); // panelLayoutGroup.SetLayoutHorizontal(); // panelLayoutGroup.SetLayoutVertical(); // } m_tempSlots.Add(newSlot); } } /* at this point the m_tempSlots has at least as many slots with slotRect as inventory entries * slotRects are oredered hierarchically * slottables are empty */ for (int i = 0; i < inventory.entries.Count; i++) { bool found = false; for (int j = 0; j < m_slots.Count; j++) { if (m_slots[j].slottable != null) { if (m_slots[j].slottable.m_itemInstance == inventory.entries[i].itemInstance) { found = true; m_tempSlots[i].slottable = m_slots[j].slottable; m_tempSlots[i].slottable.m_Quantity = inventory.entries[i].quantity; } } } if (!found) { Slottable newSlottable = Instantiate(slottablePrefab, Vector3.zero, Quaternion.identity); newSlottable.Initialize(this, inventory.entries[i].itemInstance, inventory.entries[i].itemInstance.m_item, inventory.entries[i].quantity); newSlottable.Attach(m_tempSlots[i].slotRect); newSlottable.InitHierarchyDependents(); newSlottable.GetComponent <Image>().enabled = false; m_tempSlots[i].slottable = newSlottable; } if (m_tempSlots[i].slottable.m_Quantity == 0 && m_slotGroupType != SlotGroupType.Pool) { m_tempSlots[i].slottable = null; } } for (int i = 0; i < m_tempSlots.Count; i++) { if (m_tempSlots[i].slottable == null && isExpandable) { m_tempSlots.RemoveAt(i); } } /* at this point all the itemInstance in the inventory is represented as slottable and stored in m_tempSlots * if not pool the zero quantity slottables are omitted */ /* Transit part */ for (int i = 0; i < m_slots.Count; i++) { bool found = false; for (int j = 0; j < m_tempSlots.Count; j++) { if (m_slots[i].slottable != null && m_tempSlots[j].slottable != null) { if (m_slots[i].slottable == m_tempSlots[j].slottable) { found = true; m_slots[i].slottable.StartCoroutine(m_slots[i].slottable.MoveWithinSG(m_tempSlots[j].slotRect, .5f, eventData)); continue; } } } if (!found) { if (m_slots[i].slottable != null) { m_slots[i].slottable.Detach(eventData); m_slots[i].slottable.GetComponent <Image>().enabled = false; } } } }