Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using the SpiritManager class manages the Spirit in XNA, WPXNA (14)

0.00/5 (No votes)
18 Jul 2013 1  
Using the SpiritManager class to manage the Spirit in XNA.

Introduction/Catalog

I has developed some games on Windows Phone. Here, I'll share my experiences and gradually to upload some classes, no good name, I just call it WPXNA. (Some example code may not be stringent enough.)

SpiritManager<T>

SpiritManager<T> contains some methods, we can also derive a lot of management classes, such as bullet management class. The following are members of the SpiritManager<T>.
 
Field Spirits is the spirits managed by manager, field defaultOrder is the order of the spirits. Field Scene allow us to access content in the scene. Later, we will replace it with the IPlayScene.
 
The method Append will add spirit to the manager, we set the Destroyed event, in this case, the destroyed spirits will be removed from the manager. In the Remove method, we will log off the Destroyed event.
internal abstract class SpiritManager<T>
 : IDisposable
 where T : Spirit
{

 internal readonly List<T> Spirits = new List<T> ( );
 private readonly int defaultOrder;

 //internal IPlayScene Scene;
 internal IScene Scene;

 protected SpiritManager ( )
  : this ( 0 )
 { }
 protected SpiritManager ( int defaultOrder )
 { this.defaultOrder = defaultOrder; }

 protected virtual void spiritDestroyed ( object sender, SpiritEventArgs e )
 { this.remove ( sender as T ); }

 internal void Append ( IList<T> spirits )
 {

  if ( null != spirits )
   foreach ( T spirit in spirits )
    this.Append ( spirit );

 }

 internal void Append ( T spirit )
 { this.Append ( spirit, this.defaultOrder ); }

 internal virtual void Append ( T spirit, int order )
 {

  spirit.Destroyed += this.spiritDestroyed;

  if ( order != 0 && spirit.DrawOrder == 0 )
   spirit.DrawOrder = order;

  this.Spirits.Add ( spirit );
  this.Scene.World.Components.Add ( spirit );
 }

 private void remove ( T spirit )
 {

  spirit.Destroyed -= this.spiritDestroyed;
  this.Scene.World.Components.Remove ( spirit );
  this.Spirits.Remove ( spirit );

  spirit.Dispose ( );
 }

 internal virtual void Update ( GameTime time )
 { }

 internal void RemoveAll ( )
 {

  T[] spirits = this.Spirits.ToArray ( );

  foreach ( T spirit in spirits )
   this.remove ( spirit );

 }

 public virtual void Dispose ( )
 { this.RemoveAll ( ); }

}

Example

SceneT15 and SceneT14 are similar, we also have a Bird class, but we have added a BirdManager class that derives from SpiritManager<T>.
 
The BirdManager has two methods, Go method will call Go method of all birds, Stop method will call Stop method of all birds.
internal class BirdManager
 : SpiritManager<Bird>
{

 internal BirdManager ( )
  : base ( )
 { }

 internal void Go ( )
 {

  foreach ( Bird bird in this.Spirits )
   bird.Go ( );

 }

 internal void Stop ( )
 {

  foreach ( Bird bird in this.Spirits )
   bird.Stop ( );

 }

}
SceneT15 also contains two buttons, when you click on the two buttons, the BirdManager will call Go and Stop methods. In the SceneT15 constructor, we create a BirdManager class.
 
In the LoadContent method, we add the two Bird class to BirdManager. In the UnloadContent method, we call the RemoveAll method to remove all the birds.
In the Dispose method, we will call the Dispose method of the BirdManager.
internal sealed class SceneT15
 : CommandScene
{
 private BirdManager birdManager;
 private readonly Button goButton;
 private readonly Button stopButton;

 internal SceneT15 ( )
  : base ( Vector2.Zero, GestureType.None, "background1",
  new Resource[] {
   new Resource ( "bird2.image", ResourceType.Image, @"image\bird2" ),
   new Resource ( "go.image", ResourceType.Image, @"image\button1" ),
   new Resource ( "stop.image", ResourceType.Image, @"image\button2" ),
  },
  new Making[] {
   new Movie ( "bird", "bird2.image", 80, 80, 5, "stop",
    new MovieSequence ( "go", true, new Point ( 1, 1 ), new Point ( 2, 1 ) ),
    new MovieSequence ( "stop", true, new Point ( 3, 1 ) )
    ),
   new Button ( "b.go", "go.image", "GO", new Vector2 ( 100, 100 ), 100, 50, new Point ( 1, 1 ) ),
   new Button ( "b.play", "stop.image", "STOP", new Vector2 ( 100, 300 ), 100, 50, new Point ( 1, 1 ) )
  }
  )
 {
  this.birdManager = new BirdManager ( );
  this.birdManager.Scene = this;

  this.goButton = this.makings[ "b.go" ] as Button;
  this.stopButton = this.makings[ "b.play" ] as Button;

  this.goButton.Selected += this.goButtonSelected;
  this.stopButton.Selected += this.stopButtonSelected;
 }

 private void goButtonSelected ( object sender, ButtonEventArgs e )
 { this.birdManager.Go ( ); }

 private void stopButtonSelected ( object sender, ButtonEventArgs e )
 { this.birdManager.Stop ( ); }

 public override void LoadContent ( )
 {
  base.LoadContent ( );

  this.birdManager.Append ( new Bird ( this, new Vector2 ( 200, 100 ) ) );
  this.birdManager.Append ( new Bird ( this, new Vector2 ( 300, 200 ) ) );
 }

 public override void UnloadContent ( )
 {
  this.birdManager.RemoveAll ( );

  base.UnloadContent ( );
 }

 public override void Dispose ( )
 {
  this.birdManager.Dispose ( );

  base.Dispose ( );
 }

}
Get code hereļ¼Œhttp://wp-xna.googlecode.com/, more contents, please visit WPXNA.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here