- 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.Graphics;
using ANX.Framework.NonXNA; using ANX.Framework.NonXNA;
using ANX.Framework.NonXNA.PlatformSystem; using ANX.Framework.NonXNA.PlatformSystem;
using System.Collections.Generic;
#endregion // Using Statements #endregion // Using Statements
@ -36,6 +37,9 @@ namespace ANX.Framework
private ContentManager content; private ContentManager content;
private GameComponentCollection components;
private List<IGameComponent> drawableGameComponents;
// Events // Events
public event EventHandler<EventArgs> Activated; public event EventHandler<EventArgs> Activated;
public event EventHandler<EventArgs> Deactivated; public event EventHandler<EventArgs> Deactivated;
@ -77,11 +81,20 @@ namespace ANX.Framework
this.inactiveSleepTime = TimeSpan.Zero; this.inactiveSleepTime = TimeSpan.Zero;
this.targetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 60L); // default is 1/60s 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"); Logger.Info("finished initializing new Game class");
} }
~Game() ~Game()
{ {
this.components.ComponentAdded -= components_ComponentAdded;
this.components.ComponentRemoved -= components_ComponentRemoved;
//TODO: implement //TODO: implement
} }
@ -130,12 +143,24 @@ namespace ANX.Framework
protected virtual void Update(GameTime gameTime) protected virtual void Update(GameTime gameTime)
{ {
foreach (IUpdateable updateable in this.components)
{
if (updateable.Enabled)
{
updateable.Update(gameTime);
}
}
} }
protected virtual void Draw(GameTime gameTime) protected virtual void Draw(GameTime gameTime)
{ {
foreach (IDrawable drawable in this.drawableGameComponents)
{
if (drawable.Visible)
{
drawable.Draw(gameTime);
}
}
} }
protected virtual void LoadContent() protected virtual void LoadContent()
@ -446,13 +471,11 @@ namespace ANX.Framework
} }
} }
public GameComponentCollection Components public GameComponentCollection Components
{ {
get get;
{ private set;
throw new NotImplementedException(); }
}
}
public void Dispose() public void Dispose()
{ {
@ -483,5 +506,22 @@ namespace ANX.Framework
{ {
throw new NotImplementedException(); 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 public class GameComponent : IGameComponent, IUpdateable, IDisposable
{ {
private bool enabled = true; 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 public bool Enabled
{ {
@ -27,8 +33,6 @@ namespace ANX.Framework
} }
} }
private int updateOrder;
public int UpdateOrder public int UpdateOrder
{ {
get { return updateOrder; } get { return updateOrder; }
@ -42,19 +46,11 @@ namespace ANX.Framework
} }
} }
private Game game;
public Game Game public Game Game
{ {
get { return game; } get { return game; }
} }
public event EventHandler<EventArgs> EnabledChanged;
public event EventHandler<EventArgs> UpdateOrderChanged;
public event EventHandler<EventArgs> Disposed;
public GameComponent(Game game) public GameComponent(Game game)
{ {
this.game = game; this.game = game;

View File

@ -12,43 +12,65 @@ namespace ANX.Framework
{ {
public sealed class GameComponentCollection : Collection<IGameComponent> public sealed class GameComponentCollection : Collection<IGameComponent>
{ {
#region Events
public event EventHandler<GameComponentCollectionEventArgs> ComponentAdded; public event EventHandler<GameComponentCollectionEventArgs> ComponentAdded;
public event EventHandler<GameComponentCollectionEventArgs> ComponentRemoved; public event EventHandler<GameComponentCollectionEventArgs> ComponentRemoved;
#endregion
public GameComponentCollection() public GameComponentCollection()
{ {
throw new NotImplementedException(); // nothing to do here
} }
protected override void ClearItems() 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) protected override void InsertItem(int index, IGameComponent item)
{ {
throw new NotImplementedException(); if (item == null)
} {
throw new ArgumentNullException("item");
}
private void OnComponentAdded(GameComponentCollectionEventArgs eventArgs) base.Insert(index, item);
{ OnComponentAdded(item);
throw new NotImplementedException();
}
private void OnComponentRemoved(GameComponentCollectionEventArgs eventArgs)
{
throw new NotImplementedException();
} }
protected override void RemoveItem(int index) 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) 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));
}
} }
} }
} }