- added basic support for GameComponents (update- and draw-order missing)

This commit is contained in:
Glatzemann 2012-08-29 12:32:14 +00:00
parent c04709e3b4
commit c8f2ffb108
3 changed files with 92 additions and 34 deletions

View File

@ -5,6 +5,7 @@ using ANX.Framework.Content;
using ANX.Framework.Graphics;
using ANX.Framework.NonXNA;
using ANX.Framework.NonXNA.PlatformSystem;
using System.Collections.Generic;
#endregion // Using Statements
@ -36,6 +37,9 @@ namespace ANX.Framework
private ContentManager content;
private GameComponentCollection components;
private List<IGameComponent> drawableGameComponents;
// Events
public event EventHandler<EventArgs> Activated;
public event EventHandler<EventArgs> Deactivated;
@ -77,11 +81,20 @@ namespace ANX.Framework
this.inactiveSleepTime = TimeSpan.Zero;
this.targetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 60L); // default is 1/60s
//TODO: implement draw- and update-order handling of GameComponents
this.components = new GameComponentCollection();
this.components.ComponentAdded += new EventHandler<GameComponentCollectionEventArgs>(components_ComponentAdded);
this.components.ComponentRemoved += new EventHandler<GameComponentCollectionEventArgs>(components_ComponentRemoved);
this.drawableGameComponents = new List<IGameComponent>();
Logger.Info("finished initializing new Game class");
}
~Game()
{
this.components.ComponentAdded -= components_ComponentAdded;
this.components.ComponentRemoved -= components_ComponentRemoved;
//TODO: implement
}
@ -130,12 +143,24 @@ namespace ANX.Framework
protected virtual void Update(GameTime gameTime)
{
foreach (IUpdateable updateable in this.components)
{
if (updateable.Enabled)
{
updateable.Update(gameTime);
}
}
}
protected virtual void Draw(GameTime gameTime)
{
foreach (IDrawable drawable in this.drawableGameComponents)
{
if (drawable.Visible)
{
drawable.Draw(gameTime);
}
}
}
protected virtual void LoadContent()
@ -446,13 +471,11 @@ namespace ANX.Framework
}
}
public GameComponentCollection Components
{
get
{
throw new NotImplementedException();
}
}
public GameComponentCollection Components
{
get;
private set;
}
public void Dispose()
{
@ -483,5 +506,22 @@ namespace ANX.Framework
{
throw new NotImplementedException();
}
private void components_ComponentRemoved(object sender, GameComponentCollectionEventArgs e)
{
if (e.GameComponent is IDrawable)
{
drawableGameComponents.Remove(e.GameComponent);
}
}
private void components_ComponentAdded(object sender, GameComponentCollectionEventArgs e)
{
if (e.GameComponent is IDrawable)
{
drawableGameComponents.Add(e.GameComponent);
}
}
}
}

View File

@ -13,6 +13,12 @@ namespace ANX.Framework
public class GameComponent : IGameComponent, IUpdateable, IDisposable
{
private bool enabled = true;
private int updateOrder;
private Game game;
public event EventHandler<EventArgs> EnabledChanged;
public event EventHandler<EventArgs> UpdateOrderChanged;
public event EventHandler<EventArgs> Disposed;
public bool Enabled
{
@ -27,8 +33,6 @@ namespace ANX.Framework
}
}
private int updateOrder;
public int UpdateOrder
{
get { return updateOrder; }
@ -42,19 +46,11 @@ namespace ANX.Framework
}
}
private Game game;
public Game Game
{
get { return game; }
}
public event EventHandler<EventArgs> EnabledChanged;
public event EventHandler<EventArgs> UpdateOrderChanged;
public event EventHandler<EventArgs> Disposed;
public GameComponent(Game game)
{
this.game = game;

View File

@ -12,43 +12,65 @@ namespace ANX.Framework
{
public sealed class GameComponentCollection : Collection<IGameComponent>
{
#region Events
public event EventHandler<GameComponentCollectionEventArgs> ComponentAdded;
public event EventHandler<GameComponentCollectionEventArgs> ComponentRemoved;
#endregion
public GameComponentCollection()
{
throw new NotImplementedException();
// nothing to do here
}
protected override void ClearItems()
{
throw new NotImplementedException();
for (int i = 0; i < base.Count; i++)
{
OnComponentRemoved(base[i]);
}
base.Clear();
}
protected override void InsertItem(int index, IGameComponent item)
{
throw new NotImplementedException();
}
if (item == null)
{
throw new ArgumentNullException("item");
}
private void OnComponentAdded(GameComponentCollectionEventArgs eventArgs)
{
throw new NotImplementedException();
}
private void OnComponentRemoved(GameComponentCollectionEventArgs eventArgs)
{
throw new NotImplementedException();
base.Insert(index, item);
OnComponentAdded(item);
}
protected override void RemoveItem(int index)
{
throw new NotImplementedException();
IGameComponent component = base[index];
base.Remove(component);
OnComponentRemoved(component);
}
protected override void SetItem(int index, IGameComponent item)
{
throw new NotImplementedException();
base[index] = item;
OnComponentAdded(item);
}
private void OnComponentAdded(IGameComponent component)
{
if (ComponentAdded != null)
{
ComponentAdded(this, new GameComponentCollectionEventArgs(component));
}
}
private void OnComponentRemoved(IGameComponent component)
{
if (ComponentRemoved != null)
{
ComponentRemoved(this, new GameComponentCollectionEventArgs(component));
}
}
}
}