Skip to content

Instantly share code, notes, and snippets.

@ScottLilly
Created October 11, 2013 18:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ScottLilly/6939851 to your computer and use it in GitHub Desktop.
Save ScottLilly/6939851 to your computer and use it in GitHub Desktop.
C# code for inventory system that has a limited number of slots, and the ability to stack some items (with a different maximum size stack for different items).
using System;
using System.Collections.Generic;
using System.Linq;
namespace BusinessObjects.RedditRPGQuestion
{
public class InventorySystem
{
private const int MAXIMUM_SPACES_IN_INVENTORY = 10;
public readonly List<InventoryRecord> InventoryRecords = new List<InventoryRecord>();
public void AddItem(ObtainableItem item, int quantityToAdd)
{
while(quantityToAdd > 0)
{
// If an object of this item type already exists in the inventory, and has room to stack more items,
// then add as many as we can to that stack.
if(InventoryRecords.Exists(x => (x.InventoryItem.ID == item.ID) && (x.Quantity < item.MaximumStackableQuantity)))
{
// Get the item we're going to add quantity to
InventoryRecord inventoryRecord =
InventoryRecords.First(x => (x.InventoryItem.ID == item.ID) && (x.Quantity < item.MaximumStackableQuantity));
// Calculate how many more can be added to this stack
int maximumQuantityYouCanAddToThisStack = (item.MaximumStackableQuantity - inventoryRecord.Quantity);
// Add to the stack (either the full quanity, or the amount that would make it reach the stack maximum)
int quantityToAddToStack = Math.Min(quantityToAdd, maximumQuantityYouCanAddToThisStack);
inventoryRecord.AddToQuantity(quantityToAddToStack);
// Decrease the quantityToAdd by the amount we added to the stack.
// If we added the total quantityToAdd to the stack, then this value will be 0, and we'll exit the 'while' loop.
quantityToAdd -= quantityToAddToStack;
}
else
{
// We don't already have an existing inventoryRecord for this ObtainableItem object,
// so, add one to the list, if there is room.
if(InventoryRecords.Count < MAXIMUM_SPACES_IN_INVENTORY)
{
// Don't set the quantity value here.
// The 'while' loop will take us back to the code above, which will add to the quantity.
InventoryRecords.Add(new InventoryRecord(item, 0));
}
else
{
// Throw an exception, or somehow let the user know they are out of inventory space.
// This exception here is just a quick example. Do something better in your code.
throw new Exception("There is no more space in the inventory");
}
}
}
}
public class InventoryRecord
{
public ObtainableItem InventoryItem { get; private set; }
public int Quantity { get; private set; }
public InventoryRecord(ObtainableItem item, int quantity)
{
InventoryItem = item;
Quantity = quantity;
}
public void AddToQuantity(int amountToAdd)
{
Quantity += amountToAdd;
}
}
}
}
using System;
namespace BusinessObjects.RedditRPGQuestion
{
public abstract class ObtainableItem
{
public Guid ID { get; set; }
public string Name { get; set; }
public int MaximumStackableQuantity { get; set; }
protected ObtainableItem()
{
MaximumStackableQuantity = 1;
}
}
}
namespace BusinessObjects.RedditRPGQuestion
{
public class Plant : ObtainableItem
{
public Plant()
{
MaximumStackableQuantity = 50;
}
}
}
namespace BusinessObjects.RedditRPGQuestion
{
public class Potion : ObtainableItem
{
public Potion()
{
MaximumStackableQuantity = 10;
}
}
}
namespace BusinessObjects.RedditRPGQuestion
{
public class Weapon : ObtainableItem
{
// No constructor needed, since we want to use the base class default of MaximumStackableQuantity of 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment