Click here to Skip to main content
11,643,493 members (73,930 online)
Click here to Skip to main content

Tagged as

Use Region and RegionManager to create special region in XNA, WPXNA (18)

, 1 Aug 2013 CPOL 3.1K 1
Rate this:
Please Sign up or sign in to vote.
Use Region and RegionManager to create special region in XNA, WPXNA (18).

Introduction/Catalog

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

  • Region
  • RegionManager
  • Example

Region

If some units entered a region, we will add some special effects for them, such as: slow down. Therefore, we define the Region class to represent the region.

In the Region class, we define the field targets that is the target entered. Method Append is used to add a new Spirit, if the Spirit is already included in the region, you can't add it again.

A derived class can modify the appending and appended methods. Method appending can be used to view the Spirit information, and then decide whether to add it. Method appended can be used to add various effects for the Spirit.

internal abstract class Region
 : Spirit
{
 private readonly List<Spirit> targets = new List<Spirit> ( );

 protected Region ( IPlayScene scene, int type, Vector2 location, 
      string movieName, float speed, int angle, HitArea hitArea, 
      int width, int height, double destroySecond )
  : base ( scene, type, location, movieName,
  null,
  speed, angle, hitArea, width, height, destroySecond,
  true,
  false,
  false,
  0
  )
 { }

 protected virtual bool appending ( Spirit spirit )
 { return true; }
 protected virtual void appended ( Spirit spirit )
 { }

 internal void Append ( IList<Spirit> spirits )
 {

  if ( null == spirits )
   return;

  foreach ( Spirit spirit in spirits )
   this.Append ( spirit );

 }
 internal void Append ( Spirit spirit )
 {

  if ( this.targets.Contains ( spirit ) || !this.appending ( spirit ) )
   return;

  this.targets.Add ( spirit );

  this.appended ( spirit );
 }

 protected override void move ( )
 {
  this.Location.X += this.xSpeed;
  this.Location.Y += this.ySpeed;
 }

 protected override Vector2 getMovieLocation ( )
 { return this.Location - this.halfSize; }

 protected override void Dispose ( bool disposing )
 {

  try
  {

   if ( disposing )
   {
    this.targets.Clear ( );
   }

  }
  catch { }

  base.Dispose ( disposing );
 }

}

As members of this class have no special, so you can reference other Spirit classes.

RegionManager

RegionManager class is derived from class SpiritManager<T>, the default order is 3000.

internal sealed class RegionManager
 : SpiritManager<Region>
{
 internal event EventHandler<RegionHitAreaEventArgs> HitTesting;

 internal RegionManager ( )
  : base ( 3000 )
 { }

 internal override void Update ( GameTime time )
 {

  if ( null == this.HitTesting )
   return;

  foreach ( Region region in this.Spirits.ToArray ( ) )
  {
   RegionHitAreaEventArgs hitAreaArg = new RegionHitAreaEventArgs ( region );
   this.HitTesting ( region, hitAreaArg );

   region.Append ( hitAreaArg.Targets );

   hitAreaArg.Dispose ( );
  }

 }

}

HitTesting event can let the outside world test collision and return the targets by the parameter RegionHitAreaEventArgs. Here, you do not need to set IsHit property of RegionHitAreaEventArgs to true.

Example

SceneT19 is an extension of SceneT18, in addition to the functions of SceneT18, it will also create a region.

We have defined a class MyRegion that is a region and it can last 10 seconds. When a Spirit entered into the region, it will be destroyed.

internal class MyRegion
 : Region
{

 internal MyRegion ( IPlayScene scene, Vector2 location )
  : base ( scene, 1, location, "myr", 0, 0,
  new SingleRectangleHitArea ( new Rectangle ( -75, -75, 150, 150 ) ),
  150, 150,
  10
  )
 { }

 protected override bool appending ( Spirit spirit )
 {
  spirit.Destroy ( );

  return false;
 }

}

We also defined a RegionManager.

private RegionManager regionManager;

internal SceneT19 ( )
 : base ( Vector2.Zero, GestureType.None, "background1",
 new Resource[] {
  new Resource ( "bird2.image", ResourceType.Image, @"image\bird2" ),
  new Resource ( "bullet.image", ResourceType.Image, @"image\bullet" ),
  new Resource ( "item.image", ResourceType.Image, @"image\item" ),
  new Resource ( "pinup.image", ResourceType.Image, @"image\pinup1" ),
  new Resource ( "r.image", ResourceType.Image, @"image\r1" ),
  new Resource ( "go.image", ResourceType.Image, @"image\button1" ),
 },
 new Making[] {
  new Movie ( "bird", "bird2.image", 80, 80, 5, "live",
   new MovieSequence ( "live", true, new Point ( 1, 1 ), new Point ( 2, 1 ) )
   ),
  new Movie ( "mybutton", "bullet.image", 10, 10, 0, "b",
   new MovieSequence ( "b", new Point ( 1, 1 ) )
   ),
  new Movie ( "myitem", "item.image", 30, 30, 0, "i",
   new MovieSequence ( "i", new Point ( 1, 1 ) )
   ),
  new Movie ( "mypinup", "pinup.image", 100, 50, 0, "p",
   new MovieSequence ( "p", new Point ( 1, 1 ) )
   ),
  new Movie ( "myr", "r.image", 150, 150, 0, "r",
   new MovieSequence ( "r", new Point ( 1, 1 ) )
   ),
  new Button ( "b.go", "go.image", "GO", 
               new Vector2 ( 10, 690 ), 100, 50, new Point ( 1, 1 ) ),
 }
 )
{
 // ...

 this.regionManager = new RegionManager ( );
 this.regionManager.Scene = this;
 this.regionManager.HitTesting += this.regionHitTesting;

 // ...
}

In the method regionHitTesting, we will decide which bullet entered the region, and region will attempt to add these bullets.

private void regionHitTesting ( object sender, RegionHitAreaEventArgs e )
{
 List<Spirit> targets = new List<Spirit> ( );

 foreach ( Bullet bullet in this.bulletManager.Spirits )
  if ( e.HitArea.HitTest ( bullet.HitArea ) )
   targets.Add ( bullet );

 e.Targets = targets;
}

Finally, we need to create a region after the bird was shot.

private void bulletHitTesting ( object sender, BulletHitAreaEventArgs e )
{

 if ( !this.bird.IsDied && e.HitArea.HitTest ( this.bird.HitArea ) )
 {
  e.IsHit = true;
  e.Targets = new IAssailable[] { this.bird };

  if ( !this.is1Hit )
  {
   this.is1Hit = true;
   this.pinupManager.Append ( new MyHit ( this, new Vector2 ( 0, 400 ) ) );
  }

  this.regionManager.Append ( new MyRegion ( this, new Vector2 ( 100, 100 ) ) );
 }

}

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zoyobar
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 1 Aug 2013
Article Copyright 2013 by zoyobar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid