The use of Region, RegionManager to create a special area in XNA (eighteen)

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. )

Regional

If some units to enter the area, then they will be added special effects, such as: slow down or sustained damage. Therefore, we define a Region class to represent the region.

In Region, we define targets field used to represent the into the region of the target. The method used by Append to add new Spirit to area, if the Spirit is included in the region, cannot again be added.

The derived class can modify the appending and appended methods. Appending method can be used to view the Spirit information, and then according to these information to decide whether to add sprite. Methods appended can add various effects for increased elves.

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

}

Other members can reference other Spirit interpretation.

Regional Manager

The RegionManager class is derived from class SpiritManager<T>, The default rendering 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 ( );
        }

    }

}

Event HitTesting allows collision outside judgment elves and area, and through the parameter RegionHitAreaEventArgs returns and regional impact target. Here you don't need to IsHit's RegionHitAreaEventArgs property is set to true.

Example

Scene SceneT19 is the extension of SceneT18, in addition to a SceneT18 function, will also create a region.

We define a class of MyRegion, he is a regional, lasts 10 seconds. When the elves into the area, he 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 define 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 of regionHitTesting, we will determine which bullets into the region, and the 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 be shot at the bird, the creation of a regional.

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

}

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 Jared at December 22, 2013 - 4:27 PM