Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 2nd, 2013  |  syntax: C#  |  size: 1.70 KB  |  views: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class World
  2. {
  3.     List<Item> _Items;
  4.     public World()
  5.     {
  6.          // assign all the stuff, blah blahy
  7.          _Items =  new List<Item>;
  8.     }
  9.  
  10.     public AddItem(Item item)
  11.     {
  12.          _Items.Add(item);
  13.     }
  14.  
  15.     public RemoveItem(Item item)
  16.     {
  17.         _Items.Remove(item);
  18.     }
  19. }
  20.  
  21. public class ItemActions
  22. {
  23.     public List<Action<Item>> _actions;
  24.  
  25.     public ItemActions()
  26.     {
  27.         _actions = new List<Action<Item>>();
  28.     }
  29.  
  30.     public void AddAction(Action<Item> action)
  31.     {
  32.         _actions.Add(action);
  33.     }
  34.  
  35.     public void ApplyAll(Item item)
  36.     {
  37.         foreach(Action action in _actions)
  38.         {
  39.              action(item);
  40.         }
  41.     }
  42. }
  43.  
  44. public class ItemType
  45. {
  46.     // boring properties and more
  47.     ItemActions onHarvest;
  48.  
  49.     public ItemType()
  50.     {
  51.         tree.properties = yada;
  52.         tree.onHarvest = new List<Action>();
  53.         tree.onHarvest.AddAction(delegate(Item i) { i.Parent.AddItem(new Item(Parent, ItemTypes.Log)); };
  54.         tree.onHarvest.AddAction(delegate(Item i) { i.Remove(); };
  55.     }
  56. }
  57.  
  58. public class Item
  59. {
  60.     World Parent;
  61.  
  62.     public Item(World parent, ItemType itemtype)
  63.     {
  64.          Parent = parent;
  65.          // assign properties from type, boring stuff.
  66.          this.stuff = itemtype.stuff        
  67.     }
  68.  
  69.     public void Remove()
  70.     {
  71.          Parent.RemoveItem(this);
  72.     }
  73. }
  74.  
  75. public class ItemHarvestable : Item
  76. {
  77.     ItemActions OnHarvest;
  78.  
  79.     public ItemHarvestable(World parent, ItemType itemtype) : base(parent, itemtype)
  80.     {
  81.         OnHarvest = itemtype.onHarvest;
  82.     }
  83.  
  84.     public void Harvest()
  85.     {
  86.         OnHarvest.ApplyAll(this);
  87.     }
  88. }
clone this paste RAW Paste Data