The use of Bullet, BulletManager creates the bullet attack targets in XNA (fifte

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Square has developed some Windows Phone some of the game, not what technology. Share your experiences here, only in order to exchange experiences and friends. Square will gradually will write the class uploaded to the hosting project, no good name, called WPXNA, finally, please master detour, so as not to waste time. (in order to focus and reduce space, some sample code may not strict enough. )

Bullet

The bullet is very important in the game, almost all games have similar bullet or bullet. Below we introduce Bullet class.

The Bullet class is derived from the Spirit class, the following is his field:

internal readonly int Power;
private int life;

Field Power says a bullet, field life said that the bullet of the value of life. The bullet every attack targets will be deducted from his own life value, life value is less than or equal to 0, then the bullet will be destroyed. If the bullet initial life value is 0, said the value of life infinite.

The Bullet class constructor parameters like Spirit, isMoving field is set to true, the bullets start at a mobile state, but you need to move the speed setting.

protected Bullet ( /* IPlayScene scene */ IScene scene, int type, Vector2 location, string movieName, float speed, int angle, HitArea hitArea, int width, int height, int power, int life, double destroySecond, bool isMovieRotable, bool isAreaLimited, bool isAreaEntered, double areaSecond )
    : base ( scene, type, location, movieName,
    null,
    speed, angle, hitArea, width, height, destroySecond, isMovieRotable, isAreaLimited, isAreaEntered, areaSecond )
{
    this.Power = power <0 ? 0 : power;
    this.life = life;

    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; }

In method move, our position to move according to xSpeed and ySpeed. In getMovieLocation, we set up a film location, so that the center of the film and the center line.

internal virtual void Attack ( IList<IAssailable> targets )
{

    if ( null == targets )
        return;

    foreach ( IAssailable target in targets )
    {
        target.Injure ( this.Power, this.type );

        if ( this.life > 0 )
        {
            this.life -= this.Power;

            if ( this.life <= 0 )
            {
                this.Destroy ( );
                break;
            }

        }

    }

}

Methods Attack can let the bullets to attack targets, this target will need to implement the IAssailable interface. We will not be deducted directly target the value of life, but to call the Injure method to them, so that each task can be completed its task.

The bullet Manager

The BulletManager class is derived from class SpiritManager<T>, The default rendering order is 2000.

The HitTesting event to let the outside testing collision, the parameter BulletHitAreaEventArgs, BulletManager can know whether the ball hits the target, if the bullet hit the target, it will call the Attack method.

We need to call the ToArray method of field Spirits, because the Spirits in the cycle is likely to change.

internal event EventHandler<BulletHitAreaEventArgs> HitTesting;

internal BulletManager ( )
    : base ( 2000 )
{ }

internal override void Update ( GameTime time )
{

    if ( null == this.HitTesting )
        return;

    foreach ( Bullet bullet in this.Spirits.ToArray ( ) )
        if ( null != bullet.HitArea )
        {
            BulletHitAreaEventArgs hitAreaArg = new BulletHitAreaEventArgs ( bullet );
            this.HitTesting ( bullet, hitAreaArg );

            if ( hitAreaArg.IsHit )
                bullet.Attack ( hitAreaArg.Targets );

            hitAreaArg.Dispose ( );
        }

}

In this example, HitTest does not call, the method will return and parameter area collision.

internal List<Bullet> HitTest ( HitArea area, int mode )
{
    List<Bullet> bullets = new List<Bullet> ( );

    foreach ( Bullet bullet in this.Spirits )
        if ( mode == 1 && bullet.Type <100 && area.HitTest ( bullet.HitArea ) )
            bullets.Add ( bullet );
        else if ( mode == 2 && bullet.Type >= 100 && area.HitTest ( bullet.HitArea ) )
            bullets.Add ( bullet );

    return bullets;
}

Example

Scene SceneT16, click the button to create the bullet and hit the bird.

Bird appeared in the previous example, we implement the IAssailable interface for him, in the Injure method, we deduct the bird life value, life value is less than or equal to 0, then the destruction of the bird and the IsDied field is set to true.

internal class Bird
    : Spirit, IAssailable
{
    private int life = 10;
    internal bool IsDied = false;

    internal Bird ( IScene scene, Vector2 location )
        : base ( scene, 0, location,
        "bird", null,
        4, 0,
        new SingleRectangleHitArea ( new Rectangle ( -40, -40, 80, 80 ) ),
        80,
        80,
        0,
        true,
        false,
        false,
        0
        )
    { }

    public bool Injure ( int life, int type )
    {
        this.life -= life;
        Debug.WriteLine ( "life={0}", this.life );

        if ( this.life <= 0 )
        {
            Debug.WriteLine ( "life<0" );
            this.IsDied = true;
            this.Destroy ( );
        }

        return true;
    }

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

}

MyBullet is derived from Bullet, we modify the updateSpeed method, is used to set the xSpeed and ySpeed.

internal class MyBullet
    : Bullet
{

    internal MyBullet ( IScene scene, Vector2 location, int angle )
        : base ( scene, 100, location, "mybutton", 5, angle,
        new SingleRectangleHitArea ( new Rectangle ( -5, -5, 10, 10 ) ),
        10, 10,
        2, 2,
        0,
        false,
        true,
        true,
        0
        )
    { }

    protected override void updateSpeed ( )
    {
        this.xSpeed = Calculator.Cos ( this.angle ) * this.speed;
        this.ySpeed = Calculator.Sin ( this.angle ) * this.speed;

        base.updateSpeed ( );
    }

}

In the Selected method of the Append button, we will call the BulletManager method to create and add MyBullet, thus creating new bullets.

Method bulletHitTesting used to judge whether the ball hits the bird. Parameters of HitArea field representation of e collision region bullets, set the IsHit field in a e true, said the collision occurred, the Targets field representation of e target.

internal sealed class SceneT16
    : CommandScene
{
    // ...

    private Bird bird;
    private BirdManager birdManager;
    private BulletManager bulletManager;
    private readonly Button goButton;

    internal SceneT16 ( )
        : 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 ( "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 Button ( "b.go", "go.image", "GO", new Vector2 ( 10, 690 ), 100, 50, new Point ( 1, 1 ) ),
        }
        )
    {
        this.birdManager = new BirdManager ( );
        this.birdManager.Scene = this;

        this.bulletManager = new BulletManager ( );
        this.bulletManager.Scene = this;
        this.bulletManager.HitTesting += this.bulletHitTesting;

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

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

    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 };
        }

    }

    private void goButtonSelected ( object sender, ButtonEventArgs e )
    { this.bulletManager.Append ( new MyBullet ( this, new Vector2 ( 10, 10 ), 45 ) ); }

    protected override void updating ( GameTime time )
    {
        this.bulletManager.Update ( time );

        base.updating ( time );
    }

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

        this.bird = new Bird ( this, new Vector2 ( 200, 200 ) );
        this.birdManager.Append ( this.bird );
    }

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

        base.UnloadContent ( );
    }

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

        this.bulletManager.HitTesting -= this.bulletHitTesting;
        this.bulletManager.Dispose ( );

        this.goButton.Selected -= this.goButtonSelected;

        base.Dispose ( );
    }

}

This video

Project address
More about WPXNA

Square the development of the game

QQ group 213685539

Welcome to visit the same article I published in other locations

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Cher at December 16, 2013 - 11:25 AM