home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2002 September / PCPlus_193_Laplink2000.iso / Prog / c# / wombat / AdvThings.cs < prev    next >
Encoding:
Text File  |  2002-05-07  |  5.4 KB  |  313 lines

  1. using System;
  2. using System.Collections;
  3.  
  4. namespace wombat
  5. {
  6.     /// <summary>
  7.     /// AdventureThings contains a hierarchy of the
  8.     /// essential object types of an adventure game
  9.     /// such as Treasure, Room and Player
  10.     /// </summary>
  11.     ///
  12.  
  13.     // class ThingList
  14.     public class ThingList : System.Collections.CollectionBase
  15.         // A custom Collection. This works like an ArrayList
  16.         // but it is typed to accept only Thing objects
  17.     {
  18.         
  19.         public  void Add(Thing aThing)
  20.         {
  21.             InnerList.Add(aThing);
  22.         }
  23.  
  24.         public void AddRange(ThingList aThingList)
  25.         {
  26.             InnerList.AddRange(aThingList);
  27.         }
  28.         
  29.         public void Remove(int index)
  30.         {
  31.             // Check to see if there is an item at the supplied index.
  32.             if (index > Count - 1 || index < 0)
  33.                 // If no item exists, a messagebox is shown and the operation 
  34.                 // is cancelled.
  35.             {
  36.                 System.Windows.Forms.MessageBox.Show("Index not valid!");
  37.             }
  38.             else
  39.             {
  40.                 List.RemoveAt(index); 
  41.             }
  42.         }
  43.         
  44.         public Thing Item(int Index)
  45.         {
  46.             // The appropriate item is retrieved from the List object and
  47.             // explicitly cast to the Thing type, then returned to the 
  48.             // caller.
  49.             return (Thing) List[Index];
  50.         }
  51.  
  52.         public string describe()
  53.         {
  54.             string s = "";
  55.             if (this.List.Count == 0) 
  56.                 s = "Nothing in ThingList.";
  57.             else
  58.             foreach(Thing t in this)
  59.             {                
  60.                 s = s + t.describe()+"; ";            
  61.             }                
  62.             return s;        
  63.         }        
  64.         
  65.     } // ThingList
  66.  
  67.     // class RoomList
  68.     public class RoomList : System.Collections.CollectionBase
  69.         // A custom Collection. This works like an ArrayList
  70.         // but it is typed to accept only Room objects
  71.     {
  72.         
  73.         public void Add(Room aRoom)
  74.         {
  75.             InnerList.Add(aRoom);
  76.         }
  77.  
  78.         public void AddRange(RoomList aRoomList)
  79.         {
  80.             InnerList.AddRange(aRoomList);
  81.         }
  82.  
  83.         public Room Item(int Index)
  84.         {
  85.             // The appropriate item is retrieved from the List object and
  86.             // explicitly cast to the Room type, then returned to the 
  87.             // caller.
  88.             return (Room) List[Index];
  89.         }
  90.  
  91.  
  92.         public string describe()
  93.         {
  94.             string s = "";            
  95.             if (this.List.Count == 0) 
  96.                 s = "Nothing in RoomList.";
  97.             else
  98.             foreach(Room r in this)
  99.             {                
  100.                 s = s + r.describe()+"\r\n";            
  101.             }                
  102.             return s;        
  103.         }
  104.  
  105.         
  106.     } // RoomList
  107.  
  108.     // class Thing
  109.     public class Thing
  110.     {   // the basic Thing from which all Adventure
  111.         // objects descend.
  112.  
  113.         private string _name;
  114.         private string _description;
  115.  
  116.         public Thing(string aName, string aDescription)
  117.         {
  118.             _name = aName;
  119.             _description = aDescription;
  120.         }
  121.  
  122.         public string Name   //  Name property
  123.         {
  124.             get 
  125.             {
  126.                 return _name; 
  127.             }
  128.             set
  129.             {
  130.                 _name = value;
  131.             }
  132.         }
  133.  
  134.         public string Description   // Description property
  135.         {
  136.             get 
  137.             {
  138.                 return _description; 
  139.             }
  140.             set
  141.             {
  142.                 _description = value;
  143.             }
  144.         }
  145.         
  146.         public virtual string describe() //!! note this is a virtual method
  147.         {
  148.             return Name + " " + Description;        
  149.         }
  150.         
  151.         
  152.     } // Thing
  153.  
  154.     
  155.     // class ThingHolder
  156.     public class ThingHolder : Thing
  157.     {
  158.         private ThingList _things = new ThingList();
  159.  
  160.         public ThingHolder(string aName, string aDescription, ThingList tl): base(aName, aDescription)
  161.         {
  162.             _things = tl;
  163.         }
  164.  
  165.         public ThingList Things
  166.         {
  167.             get
  168.             {
  169.                 return _things;
  170.             }
  171.             set
  172.             {                
  173.                 _things = value;
  174.             }
  175.         }
  176.  
  177.         public void addThing( Thing aThing )
  178.         {
  179.             _things.Add( aThing );
  180.         }
  181.  
  182.         public void addThings( ThingList aThingList )
  183.         {
  184.             _things.AddRange( aThingList );
  185.         }
  186.  
  187.         public  /* override */ string describe()  //!! uncomment 'override' to make this an overridden method
  188.         {
  189.             return String.Format("Name: {0}, Description {1} - it contains ->",Name,Description) + _things.describe();
  190.  
  191.         }
  192.  
  193.     } // ThingHolder
  194.  
  195.     // class Room
  196.     public class Room : ThingHolder
  197.     {
  198.         private int _n,_s,_w,_e;
  199.         public Room(string aName, string aDescription,
  200.             int aN, int aS, int aW, int aE, ThingList tl): base(aName, aDescription, tl)
  201.         {
  202.             _n = aN;
  203.             _s = aS;
  204.             _w = aW;
  205.             _e = aE;            
  206.         }
  207.  
  208.         public int N
  209.         {
  210.             get
  211.             {
  212.                 return _n;
  213.             }
  214.             set
  215.             {
  216.                 _n = value;
  217.             }
  218.         }
  219.  
  220.         public int S
  221.         {
  222.             get
  223.             {
  224.                 return _s;
  225.             }
  226.             set
  227.             {
  228.                 _s = value;
  229.             }
  230.         }
  231.  
  232.         public int W
  233.         {
  234.             get
  235.             {
  236.                 return _w;
  237.             }
  238.             set
  239.             {
  240.                 _w = value;
  241.             }
  242.         }
  243.  
  244.         public int E
  245.         {
  246.             get
  247.             {
  248.                 return _e;
  249.             }
  250.             set
  251.             {
  252.                 _e = value;
  253.             }
  254.         }
  255.         
  256.         public void setDirs( int aN, int aS, int aW, int aE )
  257.         {
  258.             _n = aN;
  259.             _s = aS;
  260.             _w = aW;
  261.             _e = aE;
  262.         }
  263.  
  264.         public void getDirs( out int aN, out int aS, 
  265.                              out int aW, out int aE )
  266.         {
  267.             aN = _n;
  268.             aS = _s;
  269.             aW = _w;
  270.             aE = _e;
  271.         }
  272.  
  273.         public string describe() 
  274.         {
  275.             return String.Format("Name: {0}, Description {1}. Exits: N:{2} S:{3} W:{4} E:{5}",Name,Description,_n,_s,_w,_e) 
  276.                     + "\r\nThings here: " + this.Things.describe();
  277.  
  278.         }
  279.  
  280.  
  281.     } // Room
  282.  
  283.     // class Actor
  284.     public class Actor : ThingHolder
  285.     {
  286.         private Room _room; // Room where Person is at present
  287.  
  288.         public Actor(string aName, string aDescription, Room aRoom, ThingList tl): 
  289.                                     base(aName, aDescription, tl)
  290.         {
  291.             _room = aRoom;
  292.         }
  293.  
  294.         public Room CurrentRoom
  295.         {
  296.             get
  297.             {
  298.                 return _room;
  299.             }
  300.             set
  301.             {
  302.                 _room = value;
  303.             }
  304.         }
  305.         public string describe()
  306.         {
  307.             return String.Format("Name: {0}, Description {1}",Name,Description)+
  308.                 "\r\nCurrent in this room: " + this.CurrentRoom.describe();        
  309.         }
  310.  
  311.     }// Actor
  312. }
  313.