A Bullets Collection


For the bullets, there's really nothing else left to do; however, you will probably store numerous bullets to render and update throughout the game play. It is probably useful to have a collection class to store the bullets in. Go ahead and add the Bullets class (in Listing 15.7) to this same code file.

Listing 15.7. The Bullets Collection
 /// <summary> /// A collection of bullets /// </summary> public class Bullets : IEnumerable {     private ArrayList bulletList = new ArrayList();     /// <summary>     /// Indexer for bullets     /// </summary>     public Bullet this[int index]     {         get { return bulletList[index] as Bullet; }         set { bulletList[index] = value; }     }     /// <summary>     /// Add a new bullet to the list     /// </summary>     public void Add(Bullet b)     {         bulletList.Add(b);     }     /// <summary>     /// Remove a bullet from the list     /// </summary>     public void Remove(Bullet b)     {         bulletList.Remove(b);     }     /// <summary>     /// Remove all bullets from the list     /// </summary>     public void Clear()     {         bulletList.Clear();     }     /// <summary>     /// The number of items in the list     /// </summary>     public int Count     {         get { return bulletList.Count; }     }     /// <summary>     /// Returns the enumerator for the bullet list     /// </summary>     public IEnumerator GetEnumerator()     {         return bulletList.GetEnumerator();     } } 

As you can see, this is a simple wrapper around an array list that makes it typesafe for a Bullet class instead. Only the items that you actually care about are implemented (instead of the entire thing). There isn't anything overly complex here.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net