The SpiritManager class management used in XNA game sprite (fourteen)

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

SpiritManager<T>

SpiritManager<T> Contains some management methods of elf, we can derive a lot of management, such as: the management class. Below is SpiritManager<T> Members.

Field Spirits Management Manager wizard, field defaultOrder sprite rendering order. Field Scene allows us to access the content in the scene. Later, we will replace it with the IPlayScene.

The method used by Append will sprites to the manager, we will set the Destroyed event wizard, in this case, destroyed the Elves will be removed from the manager. In the remove method, we will cancel 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

Scene SceneT15 and SceneT14 before is similar, we also have a Bird class, but we added a BirdManager class, this class from SpiritManager<T> The derived.

BirdManager has two methods, the Go method will call the Go method of the Bird class of all, the Stop method will call the Stop method of the Bird class of all.

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 clicking the two button, will call BirdManager Go and Stop method. In the SceneT15 constructor, we create a BirdManager class, and the need to set the field Scene.

In the LoadContent method, we will have two class Bird is added to the BirdManager. While in the UnloadContent method, RemoveAll method, we call BirdManager to delete all the birds.

In the Dispose method, we will call the BirdManager method of the Dispose.

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

}

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 Claudia at December 26, 2013 - 3:36 PM