I have this messy chunk of code here which isn't doing what I want it to yet. I have two Items of class Item, Rock : Item and Twig : Item. I have an inventory system which has 5 slots, each item will go in one slot and will stack if another item of the same type is picked up, like any inventory system. When I pick up an object, it goes into my inventory and shows up at the bottom. When I pick up a different type of item however, the item count will increase but will not move to the next inventory slot. So heres the code and ill describe it the best I can.
for (int t = items.Count() - 1; t >= 0; t--)
{
if (player.rectangle.Intersects(items[t].ItemRectangle))
{
for (int i = 0; i < player.PlayerInventory.itemSlots.Count(); i++)
{
if (player.PlayerInventory.itemSlots[i].IsSlotOpen == true)
{
player.PlayerInventory.itemSlots[i].item = items[t];
player.PlayerInventory.itemSlots[i].IsSlotOpen = false;
items[t].ItemPickedUp = true;
break;
}
Looping through the Items in my list (items) checking for a collision. Also looping through my inventory and checking if a slot if open. If it is, add that item to the open slot and close the slot. Set the itemPickedUp to true which is used later on.
else if (player.PlayerInventory.itemSlots[i].item is Twig && player.PlayerInventory.itemSlots[i].IsSlotOpen == false)
{
player.PlayerInventory.itemSlots[i].item.NumberOfItems += 1;
items[t].ItemPickedUp = true;
break;
}
else if (player.PlayerInventory.itemSlots[i].item is Rock && player.PlayerInventory.itemSlots[i].IsSlotOpen == false)
{
player.PlayerInventory.itemSlots[i].item.NumberOfItems += 1;
items[t].ItemPickedUp = true;
break;
}
else
{
return null;
}
I think this is where my problem is. If the item in the slot is a twig and the slot is closed, add 1 to the quantity of the item. Set pickup to true.
Now... if the item is a rock in the slot and the slot is closed, do the same. But this is not what is happening! No idea why :P
if (items[t].ItemPickedUp == true && items[t] is Twig)
{
Twig pickedUpTwig = new Twig(items[t].Position, items[t].Texture, items[t].NumberOfItems);
items.RemoveAt(t);
return pickedUpTwig;
}
else if (items[t].ItemPickedUp == true && items[t] is Rock)
{
Rock pickedUpRock = new Rock(items[t].Position, items[t].Texture, items[t].NumberOfItems);
items.RemoveAt(t);
return pickedUpRock;
}
Final chunk of code. If the item is a twig and it is picked up, remove the twig from the list and add a twig to your inventory. Same with the rock.
The code is messy right now but I'm having trouble getting this to work. I might just have to stop staring at it lol.