home *** CD-ROM | disk | FTP | other *** search
- using System;
- using System.Collections;
-
- namespace wombat
- {
- /// <summary>
- /// AdventureThings contains a hierarchy of the
- /// essential object types of an adventure game
- /// such as Treasure, Room and Player
- /// </summary>
- ///
-
- // class ThingList
- public class ThingList : System.Collections.CollectionBase
- // A custom Collection. This works like an ArrayList
- // but it is typed to accept only Thing objects
- {
-
- public void Add(Thing aThing)
- {
- InnerList.Add(aThing);
- }
-
- public void AddRange(ThingList aThingList)
- {
- InnerList.AddRange(aThingList);
- }
-
- public void Remove(int index)
- {
- // Check to see if there is an item at the supplied index.
- if (index > Count - 1 || index < 0)
- // If no item exists, a messagebox is shown and the operation
- // is cancelled.
- {
- System.Windows.Forms.MessageBox.Show("Index not valid!");
- }
- else
- {
- List.RemoveAt(index);
- }
- }
-
- public Thing Item(int Index)
- {
- // The appropriate item is retrieved from the List object and
- // explicitly cast to the Thing type, then returned to the
- // caller.
- return (Thing) List[Index];
- }
-
- public string describe()
- {
- string s = "";
- if (this.List.Count == 0)
- s = "Nothing in ThingList.";
- else
- foreach(Thing t in this)
- {
- s = s + t.describe()+"; ";
- }
- return s;
- }
-
- } // ThingList
-
- // class RoomList
- public class RoomList : System.Collections.CollectionBase
- // A custom Collection. This works like an ArrayList
- // but it is typed to accept only Room objects
- {
-
- public void Add(Room aRoom)
- {
- InnerList.Add(aRoom);
- }
-
- public void AddRange(RoomList aRoomList)
- {
- InnerList.AddRange(aRoomList);
- }
-
- public Room Item(int Index)
- {
- // The appropriate item is retrieved from the List object and
- // explicitly cast to the Room type, then returned to the
- // caller.
- return (Room) List[Index];
- }
-
-
- public string describe()
- {
- string s = "";
- if (this.List.Count == 0)
- s = "Nothing in RoomList.";
- else
- foreach(Room r in this)
- {
- s = s + r.describe()+"\r\n";
- }
- return s;
- }
-
-
- } // RoomList
-
- // class Thing
- public class Thing
- { // the basic Thing from which all Adventure
- // objects descend.
-
- private string _name;
- private string _description;
-
- public Thing(string aName, string aDescription)
- {
- _name = aName;
- _description = aDescription;
- }
-
- public string Name // Name property
- {
- get
- {
- return _name;
- }
- set
- {
- _name = value;
- }
- }
-
- public string Description // Description property
- {
- get
- {
- return _description;
- }
- set
- {
- _description = value;
- }
- }
-
- public virtual string describe() //!! note this is a virtual method
- {
- return Name + " " + Description;
- }
-
-
- } // Thing
-
-
- // class ThingHolder
- public class ThingHolder : Thing
- {
- private ThingList _things = new ThingList();
-
- public ThingHolder(string aName, string aDescription, ThingList tl): base(aName, aDescription)
- {
- _things = tl;
- }
-
- public ThingList Things
- {
- get
- {
- return _things;
- }
- set
- {
- _things = value;
- }
- }
-
- public void addThing( Thing aThing )
- {
- _things.Add( aThing );
- }
-
- public void addThings( ThingList aThingList )
- {
- _things.AddRange( aThingList );
- }
-
- public /* override */ string describe() //!! uncomment 'override' to make this an overridden method
- {
- return String.Format("Name: {0}, Description {1} - it contains ->",Name,Description) + _things.describe();
-
- }
-
- } // ThingHolder
-
- // class Room
- public class Room : ThingHolder
- {
- private int _n,_s,_w,_e;
- public Room(string aName, string aDescription,
- int aN, int aS, int aW, int aE, ThingList tl): base(aName, aDescription, tl)
- {
- _n = aN;
- _s = aS;
- _w = aW;
- _e = aE;
- }
-
- public int N
- {
- get
- {
- return _n;
- }
- set
- {
- _n = value;
- }
- }
-
- public int S
- {
- get
- {
- return _s;
- }
- set
- {
- _s = value;
- }
- }
-
- public int W
- {
- get
- {
- return _w;
- }
- set
- {
- _w = value;
- }
- }
-
- public int E
- {
- get
- {
- return _e;
- }
- set
- {
- _e = value;
- }
- }
-
- public void setDirs( int aN, int aS, int aW, int aE )
- {
- _n = aN;
- _s = aS;
- _w = aW;
- _e = aE;
- }
-
- public void getDirs( out int aN, out int aS,
- out int aW, out int aE )
- {
- aN = _n;
- aS = _s;
- aW = _w;
- aE = _e;
- }
-
- public string describe()
- {
- return String.Format("Name: {0}, Description {1}. Exits: N:{2} S:{3} W:{4} E:{5}",Name,Description,_n,_s,_w,_e)
- + "\r\nThings here: " + this.Things.describe();
-
- }
-
-
- } // Room
-
- // class Actor
- public class Actor : ThingHolder
- {
- private Room _room; // Room where Person is at present
-
- public Actor(string aName, string aDescription, Room aRoom, ThingList tl):
- base(aName, aDescription, tl)
- {
- _room = aRoom;
- }
-
- public Room CurrentRoom
- {
- get
- {
- return _room;
- }
- set
- {
- _room = value;
- }
- }
- public string describe()
- {
- return String.Format("Name: {0}, Description {1}",Name,Description)+
- "\r\nCurrent in this room: " + this.CurrentRoom.describe();
- }
-
- }// Actor
- }
-