// 매개변수로 넘어온 toPos의 위치에 있는 아이템과 fromSlot의 아이템을 swap한다. public void ItemSwap(csInventorySlot fromSlot, Vector3 toPos) { csInventorySlot toSlot; int toIndex; if (!NearSlot(toPos, out toSlot, out toIndex)) { fromSlot.Restore(); return; } // 바꿀 아이템의 index 값을 구한다. int fromIndex = curInventory.IndexOf(fromSlot.Item); // 아이템의 위치와 개수를 swap 한다. csItem tempItem = fromSlot.Item; int tempCount = fromSlot.Count; fromSlot.SetItem(toSlot.Item, toSlot.Count); toSlot.SetItem(tempItem, tempCount); // list의 아이템 정보를 swap 한다. ReplaceToList(fromIndex, toIndex); // curIndex를 재탐색한다. SetIndex(); }
// 해당 좌표에 있는 오브젝트의 csInventorySlot 인스턴스를 찾고 // 없는 경우 false를 반환한다. private bool NearSlot(Vector3 pos, out csInventorySlot slot, out int index) { Transform tempObj; bool isQuick; // 해당 pos의 y좌표가 90보다 작으면 퀵바 슬롯 기준으로 탐색하고, 크면 인벤토리 슬롯 기준으로 탐색한다. if (pos.y < 90) { tempObj = objQuickBar; isQuick = true; } else { tempObj = objInventory; isQuick = false; } // pos의 좌표가 Slot이 있는 위치인지 파악하며, 찾지 못했을 시 false를 반환한다. for (int i = 0; i < tempObj.childCount; i++) { float x = tempObj.GetChild(i).position.x; // pos의 x좌표가 현재 탐색한 Slot의 x좌표 안의 값인지 확인한다. if (x < pos.x && x + 40 > pos.x) { float y = tempObj.GetChild(i).position.y; // pos의 y좌표가 현재 탐색한 Slot의 y좌표 안의 값인지 확인한다. if (y > pos.y && y - 40 < pos.y) { // x, y좌표가 모두 범위 안에 있는 경우 out 파라미터인 slot에 현재 탐색된 slot을 저장한다. slot = tempObj.GetChild(i).GetComponent <csInventorySlot> (); // 현재 인덱스를 저장한다. // 퀵바인 경우와 인벤토리인 경우의 인덱스를 다르게 한다. if (isQuick) { index = i; } else { index = i + QUICK_SIZE; } return(true); } } } slot = objInventory.GetChild(0).GetComponent <csInventorySlot> (); index = 0; return(false); }
// 슬롯의 초기 설정을 한다. public void Init() { // 아이템의 이동을 시각적으로 표현하기 위한 dragPos, dragImg를 초기화한다. // 임의로 위치할 dragPos의 위치와 이미지로 나타낼 dragImg가 없는 경우 초기화한다. // SelectItemPos은 해당 슬롯의 사용, 버림과 관련된 변수다. if (dragPos == null || dragImg == null || SelectItemPos == null) { dragPos = csAlreadyGame.DragItemView; dragImg = dragPos.GetComponent <Image> (); SelectItemPos = csAlreadyGame.SelectItemView; } // 초기 아이템은 빈 아이템으로 대체한다. item = csItemList.Instance.EmptyItem; GetComponent <Image> ().sprite = item.Picture; slot = GetComponent <csInventorySlot> (); }