duck

duck

Oct 14th, 2010
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. public class Inventory
  2. {
  3.     private List<Item> contents = new List<Item>();
  4.  
  5.     public bool Add(Item item)
  6.     {
  7.         // returns true for success, false if could not add
  8.        
  9.         if (contents.Contains(item))
  10.         {
  11.             Debug.Log("Cannot add "+item.name+" to inventory. An equal item is already present!");
  12.             return false;
  13.         }
  14.         else
  15.         {
  16.             Console.WriteLine("Adding "+item.name+" to inventory");
  17.             contents.Add(item);
  18.             return true;
  19.         }
  20.     }
  21. }
  22.  
  23. public class Item : IEquatable<Item>
  24. {
  25.  
  26.     private static int idIndex = 0;
  27.     public string name;
  28.    
  29.     public Item(string name)
  30.     {
  31.         this.name = name;
  32.         ID = idIndex++;
  33.     }
  34.  
  35.     public bool Equals(Item other)
  36.     {
  37.         return (this.ID == other.ID);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment