Friday 18 May 2012

Level Manager revisited–residual objects from deleted levels

 

In a previous post I went through a level manager. This post picks up were that one left off.

If you create objects in the sub class levels, then when you set the level to null, thus not activating it next time around the Array. Then the objects contained within the Level 1 for example will still exist. For this reason we could implement Idisposable and a destructor to cleanup after the Level 1 object is deleted (see here) but that is beyond the scope of what we know here. I will cover this technique at a later date. Another more simple is reported here.

say we play Sounds and a backing track within a level Level 1. Thus in the constructor assuming the appropriate Sound and Song variables are declared

sound = g.Content.Load<SoundEffect>("Tank-Moving");
backtrack = g.Content.Load<Song>("Sleep Away");
soundInst = sound.CreateInstance();
soundInst.IsLooped = true;
soundInst.Play();
MediaPlayer.IsRepeating = true;
MediaPlayer.Play(backtrack);




we must cleanup after ourselves before the level is set to null. Otherwise the objects using the external resources (media player) may not be disposed of as they are still active.



public void cleanup()
{
soundInst.Stop();
if (MediaPlayer.State == MediaState.Playing)
MediaPlayer.Stop();
player = null;

}


It would be better put the cleanup routine in the Level class along with all the possible content objects and actually the player object should be there also. But as some objects are declared in the subclass the level manager must call the cleanup method of each Sub Class Level as when you delete a sub level the objects may not be decommissioned by the .NET garbage collector until they are no longer needed. The media player will continue to play as it is repeating and will definitely not be released.



The Level manager must be able to distinguish the type of the Level to call methods appropriate to that Level. So when we are iterating through the Level objects we check what type the level object is and call it’s appropriate clean up method. This method can be used to specialize actions (method calls) for levels also. So in the update for the level manager (look for the if (l.LevelState == LEVELSTATE.FINISHED)) statement when we are checking to see if the Level is finished we call the cleanup method thus



// if the current level has finished
if (l.LevelState == LEVELSTATE.FINISHED)
{ // Get rid of the level
if (l.GetType() == typeof(Level1))
{
Level1 current = (Level1)l;
current.cleanup();
}

Levels[CurrentLevel] = null;
etc..


What we do here is we check to see what type of level object we are dealing with. You can do this with any object using the GetType() function to return the Object type (you cannot use a switch statement with though). Then we cast the Level object as a Level1 object so we can call the cleanup routine for the appropriate level. You would need an if block for every level object that requires cleaning up.

Saturday 5 May 2012

A conveyor belt of Spawning a horizontal Sprite

A Horizontal spawning Sprite is similar to a patrolling platform sprite in it’s behavior and also similar to a falling sprite. But simpler.

A simple algorithm for a collection of Spawning sprites is as follows.

  1. Set up a timer to control when to Spawn the next sprite. The class that creates the Spawning sprite collection (Game1 here) controls this timer.
  2. The Spawning Sprite object has a simple state that is either Spawned or not. So that is Boolean.
  3. An Un-spawned sprite will not be updated or drawn.
  4. The objects are spawned off screen to the left and have a target off screen to the right.
  5. While they are spawned they just move to the target position at a certain speed.
  6. When they get to the target position Spawned is set to false and they return to the starting position waiting to be Spawned again when the timer goes off.

So in a new Game Class we create an array to hold our Spawning Objects. Our Base classes are as we previously used

image

 


The Spawning enemy class variables and constructor is as follows


class Spawn : Enemy
{
Vector2 EndPosition;
public bool Spawned;
public Spawn(Game g, Texture2D texture,
Vector2 Position1, Vector2 Position2,
int framecount) : base(g,texture,Position1,framecount)
{
// start, Target and End positions are defined in Enemy
// as most if not all enemies will have them
startPosition = Position1;
TargetPosition = EndPosition = Position2;
Spawned = false;
}

public override void Update(GameTime gt)
{
if (Spawned)
{
// Move X * Velocity as we are moving Horizontally
position += new Vector2(1, 0) * Velocity;
if (Vector2.Distance(position, EndPosition) < Velocity)
{
position = startPosition;
TargetPosition = EndPosition;
Spawned = false;
}
}
base.Update(gt);

}


When a Spawn is created it is initially false and it is activated by the timer in the Game1 class. The update of the Spawn just moves it horizontally. You could do the same vertically. When it reaches the target. It is reset to the start position Spawned is set to false this will cause it to be activated when the timer next goes off in Game1.



Now to Game1



The class variables



Arrays are used here as other collections have not been covered in First year programming



const float MAXSPWAN = 1000;
float SpawnTime = MAXSPWAN; // milliseconds
Spawn[] cokeBottles;


protected override void Initialize()
{
// TODO: Add your initialization logic here
cokeBottles = new Spawn[5];
base.Initialize();
}


We fill the array in the Load Content method. The start Vector is off to the left of the viewport. The End and target position are off the right of the viewport



for(int i = 0; i < 5;i++)
cokeBottles[i] = new Spawn(this,
Content.Load<Texture2D>("CokeBottle"),
new Vector2(-30, 300),
new Vector2(GraphicsDevice.Viewport.Width + 100, 300),1);


So now we have an array of 5 Spawn objects. The Update of Game1 controls the Spawning of a sprite based on the Timer.




if (SpawnTime > 0)
//Count down the spawn time
SpawnTime -= gameTime.ElapsedGameTime.Milliseconds;
else
{
// reset the spawn time
SpawnTime = MAXSPWAN;
// if any of the Spawn reach the target
// which is off to the right of the viewport
// then the Spawn will set it's
// Spawned property to False
foreach (Spawn s in cokeBottles)
if (!s.Spawned)
{
s.Spawned = true;
break; // This is Key only one spawned at a time
}
}
// Normal Update Spawned or not
foreach (Spawn s in cokeBottles)
s.Update(gameTime);



The Draw for Game1 just draws the sprite collection



foreach (Spawn s in cokeBottles)
s.Draw(spriteBatch);


and that’s it