The original post can be found here.
Introduction/Catalog
I have 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.)
Item
Players can get items in a game, so we created a new class to represent the items. Item
class inherits from the Spirit
, it is similar to the Bullet
class.
internal abstract class Item
: Spirit
{
private readonly bool isAutoPick;
protected Item ( IPlayScene scene, int type, Vector2 location, string movieName,
float speed, int angle, HitArea hitArea, int width, int height, double destroySecond,
bool isAreaLimited, bool isAreaEntered, double areaSecond, bool isAutoPick )
: base ( scene, type, location, movieName,
null,
speed, angle, hitArea, width, height, destroySecond,
false,
isAreaLimited, isAreaEntered, areaSecond )
{
this.isAutoPick = isAutoPick;
this.isMoving = true;
}
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 updateSpeed ( )
{
this.xSpeed = Calculator.Cos ( this.angle ) * this.speed;
this.ySpeed = Calculator.Sin ( this.angle ) * this.speed;
base.updateSpeed ( );
}
}
Field isAutoPick
is used to indicate whether an item can be automatically picked up. The method updateSpeed
is used to calculate the xSpeed
and ySpeed
.
As for the other members, we can reference the Bullet
class.
ItemManager
ItemManager
class is derived from the class SpiritManager<T>
, and the default order is -1000
.
Event Picked
is used to tell the outside world, the items have been picked up, then the outside world can complete a variety of jobs, such as: increase player's HP.
As for the other members, we can reference the BulletManager
class.
internal class ItemManager
: SpiritManager<Item>
{
internal event EventHandler<HitAreaEventArgs> HitTesting;
internal event EventHandler<SpiritEventArgs> Picked;
internal ItemManager ( )
: this ( -1000 )
{ }
internal ItemManager ( int defaultOrder )
: base ( defaultOrder )
{ }
internal override void Update ( GameTime time )
{
if ( null == this.HitTesting )
return;
foreach ( Item item in this.Spirits.ToArray ( ) )
if ( null != item.HitArea )
{
HitAreaEventArgs hitAreaArg = new HitAreaEventArgs ( item.Type, item.HitArea );
this.HitTesting ( item, hitAreaArg );
if ( hitAreaArg.IsHit )
{
if ( null != this.Picked )
this.Picked ( item, new SpiritEventArgs ( item ) );
item.Destroy ( );
}
}
}
}
Example
SceneT17
is an extension of SceneT16
, in SceneT16
, we let the bullets hit the bird, in SceneT17
, we also create items which can increase the HP of bird
.
First of all, we modified the life
field of the Bird
class, he can be accessed by the outside world.
internal class Bird
: Spirit, IAssailable
{
internal int Life = 10;
}
Then, we define the MyItem
class, it is a new item that can increase the HP of bird
.
internal class MyItem
: Item
{
internal MyItem ( IPlayScene scene, Vector2 location, int angle )
: base ( scene, 1, location, "myitem", 5, angle,
new SingleRectangleHitArea ( new Rectangle ( -15, -15, 30, 30 ) ),
30, 30,
0,
true,
true,
0,
true
)
{ }
}
We created a new ItemManager
, and set its HitTesting
and Picked
events.
In the itemHitTesting
method, we will test whether the items and bird
occur collisions. In the itemPicked
method, we will add HP to the bird
.
internal sealed class SceneT17
: CommandScene, IPlayScene
{
private ItemManager itemManager;
internal SceneT17 ( )
: 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 ( "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 Button ( "b.go", "go.image", "GO",
new Vector2 ( 10, 690 ), 100, 50, new Point ( 1, 1 ) ),
}
)
{
this.itemManager = new ItemManager ( );
this.itemManager.Scene = this;
this.itemManager.HitTesting += this.itemHitTesting;
this.itemManager.Picked += this.itemPicked;
}
private void itemHitTesting ( object sender, HitAreaEventArgs e )
{
if ( !this.bird.IsDied && e.HitArea.HitTest ( this.bird.HitArea ) )
e.IsHit = true;
}
private void itemPicked ( object sender, SpiritEventArgs e )
{
this.bird.Life++;
Debug.WriteLine ( "item picked, life={0}", this.bird.Life );
}
}
Finally, we create items when the button is clicked.
private void goButtonSelected ( object sender, ButtonEventArgs e )
{
this.bulletManager.Append ( new MyBullet ( this, new Vector2 ( 10, 10 ), 45 ) );
this.itemManager.Append ( new MyItem ( this, new Vector2 ( 420, 30 ), 135 ) );
}
Get the code here, for more contents, please visit WPXNA.