Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Component manager base class
- // <typeparam name="T">Type of component to manage</typeparam>
- public abstract class ComponentManager<T> : DrawableGameComponent where T : IGameComponent, IUpdateable
- {
- // List of managed components
- protected List<T> components;
- // True, if T implements IDrawable
- private bool isDrawable;
- public ComponentManager(Game game)
- : base(game)
- {
- components = new List<T>();
- // Examine type interfaces
- Type[] interfaces = typeof(T).GetInterfaces();
- isDrawable = interfaces.Contains(typeof(IDrawable));
- }
- public override void Initialize()
- {
- base.Initialize();
- foreach (T item in this.components)
- {
- item.Initialize();
- }
- }
- public override void Update(GameTime gameTime)
- {
- foreach (T item in this.components)
- item.Update(gameTime);
- }
- public override void Draw(GameTime gameTime)
- {
- // Only call draw if the type implements IDrawable
- if (!isDrawable) return;
- foreach (T item in this.components)
- ((IDrawable)item).Draw(gameTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment