UEXDev

GameComponent Manager class

Apr 22nd, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. // Component manager base class
  2. // <typeparam name="T">Type of component to manage</typeparam>
  3. public abstract class ComponentManager<T> : DrawableGameComponent where T : IGameComponent, IUpdateable
  4. {
  5.     // List of managed components
  6.     protected List<T> components;
  7.  
  8.     // True, if T implements IDrawable
  9.     private bool isDrawable;
  10.  
  11.     public ComponentManager(Game game)
  12.         : base(game)
  13.     {
  14.         components = new List<T>();
  15.  
  16.         // Examine type interfaces
  17.         Type[] interfaces = typeof(T).GetInterfaces();
  18.         isDrawable = interfaces.Contains(typeof(IDrawable));
  19.     }
  20.  
  21.        
  22.     public override void Initialize()
  23.     {
  24.         base.Initialize();
  25.  
  26.         foreach (T item in this.components)
  27.         {
  28.             item.Initialize();
  29.         }
  30.     }
  31.  
  32.     public override void Update(GameTime gameTime)
  33.     {
  34.         foreach (T item in this.components)
  35.             item.Update(gameTime);                    
  36.     }
  37.  
  38.     public override void Draw(GameTime gameTime)
  39.     {
  40.         // Only call draw if the type implements IDrawable
  41.         if (!isDrawable) return;
  42.  
  43.         foreach (T item in this.components)
  44.             ((IDrawable)item).Draw(gameTime);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment