Unity/RPG Project
RPG Project - 14 [ 아이템 버리기 ]
SiJun-Park
2024. 10. 22. 03:11
목표는 아이템 버리기 입니다.
이전에서 작업을 하였던 인벤토리 시스템을 수정을 하였습니다.
private ItemControl _dropitem;
private SlotControl _CurrentSlot;
public void Swap(SlotControl _Before, Vector3 _After)
{
SlotControl _GetPosition = Find_Closed_Slot(_After); // 현재 마우스 위치에서 가장 가까운 슬롯을 가져온다.
if(_GetPosition == null)
{
RectTransform inventory = transform.GetComponent<RectTransform>();
if (inventory == null) return;
if (!RectTransformUtility.RectangleContainsScreenPoint(inventory, Input.mousePosition, null))
{
_dropitem = _Before.GetItem();
if (_dropitem._isStackable)
{
_CurrentSlot = _Before;
_Check.SetActive(true);
}
else
{
GameObject newitem = Instantiate(ItemData._instance.GetPrefeb(_dropitem._prefebnum));
newitem.transform.position = _DropArea.transform.position;
DeleteSlot(_Before);
_dropitem = null;
}
}
return;
}
if(_Before == _GetPosition )
{
_Before.UpdateSlot(true, _Before.GetItemIcon().sprite); // 해당 위치 다시 사용!
return;
}
if (!_GetPosition.CheckingSlots()) // 사용중이 아니라면?
{
Change(_GetPosition, _Before);
}
else
{
int Count = _Before.GetItemCount();
ItemControl _item = _Before.GetItem();
Stack<ItemControl> temp = new Stack<ItemControl>();
for (int i = 0; i < Count; i++) temp.Push(_item); // 어차피 같은 아이템들이니 그냥 Push만!
_Before.ItemClear(); // 다 지워주기
//이제 빈칸에다가 타겟 지정한 아이템을 옮겨준다!
Change(_Before, _GetPosition);
//이제 타겟 지정쪽은 내 아이템들을 넣어준다.
Count = temp.Count;
_item = temp.Peek();
for (int i = 0; i < Count; i++) _GetPosition.PushItem(_item);
_GetPosition.SetStacktables(_item._isStackable);
_GetPosition.UpdateSlot(true, temp.Peek()._Icon);
}
}
원래 코드입니다.
if(_GetPosition == null)
{
RectTransform inventory = transform.GetComponent<RectTransform>();
if (inventory == null) return;
if (!RectTransformUtility.RectangleContainsScreenPoint(inventory, Input.mousePosition, null))
{
_dropitem = _Before.GetItem();
if (_dropitem._isStackable)
{
_Input.text = "";
_CurrentSlot = _Before;
_Check.SetActive(true);
}
else
{
GameObject newitem = Instantiate(ItemData._instance.GetPrefeb(_dropitem._prefebnum));
newitem.transform.position = _DropArea.transform.position;
newitem.layer = LayerMask.NameToLayer("Item");
ItemControl current = newitem.GetComponent<ItemControl>();
ItemControl newitemcontrol = ItemDB._instance.GetItem(current._name);
if (newitemcontrol != null) current.CopyValue(newitemcontrol);
LogSystem._instance.UpdateLog($"{_Before.GetItem()._name}을 1개 버리셨습니다");
DeleteSlot(_Before);
_dropitem = null;
}
}
return;
}
바뀐 부분은 이 부분 입니다. 만약에 쌓을 수 없는 즉 포션 종류 같이 여러개를 가질 수 없는 경우에는
바로 새로운 아이템을 소환하고 Slot을 비워줍니다. 그게 아닐시 Object를 Active 해줍니다.
*여기서 Value값을 다 복사해서 붙혀넣기 위해 값을 다 넘겨주는 CopyValue 함수를 선언해준 뒤 연결 하였습니다.
즉 번들 아이템이면 위와 같이 얼마나 버릴 지 정하고 버리게 됩니다.
public void OkButton()
{
int Count = int.Parse(_Input.text);
if (_Input.text is null || Count <= 0)
{
GameObject obj = Instantiate(_HighText);
obj.GetComponentInChildren<TopTextControl>().Change_Text($"제대로 입력을 해주세요.");
return;
}
else
{
if(Count > _CurrentSlot._items.Count)
{
GameObject obj = Instantiate(_HighText);
obj.GetComponentInChildren<TopTextControl>().Change_Text($"현재 수량보다 많습니다.");
return;
}
_Check.SetActive(false);
int CurrentCount = _CurrentSlot._items.Count;
for (int i = 0; i < Count; i++)
{
GameObject newitem = Instantiate(ItemData._instance.GetPrefeb(_dropitem._prefebnum));
newitem.transform.position = _DropArea.transform.position;
if(CurrentCount > 0)
{
_CurrentSlot._items.Pop();
CurrentCount--;
}
}
if (CurrentCount == 0) DeleteSlot(_CurrentSlot);
else _CurrentSlot.UpdateSlot(true, _CurrentSlot.GetItemIcon().sprite);
_dropitem = null;
_CurrentSlot = null;
}
}
void DeleteSlot(SlotControl slot)
{
slot.ItemClear();
slot.SetStacktables(false);
slot.UpdateSlot(false, slot._default);
}
public void NoButton() {
_Check.SetActive(false);
_dropitem = null;
_CurrentSlot = null;
}
만약에 InputField의 text가 비어져있거나 그 수량이 0이면 return을 해줍니다.
또한 내가 가지고 있는 수량보다 많이 버리게 되면 다시 return을 해줍니다.
그게 아닐 시 이제 for문을 돌며 Count만큼 새롭게 아이템을 생성해 줍니다.
이때 아이템의 총 수량은 Current에 저장을 해주어 -1씩 값을 빼주었습니다.
그다음에 총 수량이 0개면? Slot을 Clear 해줍니다.
그게 아니라 수량이 남아있다면 남아있는 만큼 새롭게 업데이트를 해줍니다.