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

Monday, 30 April 2012

A simple State based level manager in XNA

This post is to explain a simple level manager that will handle the transition between levels of a simple sprite based game based on simple state management of a sequence of levels. It assumes linear progression from one level to the next

A level consists of a set of items (Sprite based items in this case) that you want to interact with or get them to interact independently, during game play. The key to level management is to have a parent class for each level that you want in your game and that will contain references to collections that you may have in each level of your game.

The level manager manages the transition between the levels based on the outcome of a particular level. Failure and re-doing a level is not dealt with here but could be easily accommodated by extending the state model for the Level Class and coding the Level Manager accordingly.

To keep it simple here and concentrate on the Level class and the Level Manager class I will create two levels of a game that will just transit through states based on a Timer. These two classes will be level1 and level2 respectively. I will also only have static (non-animated and non interactive) content in the Level class which can then be filled in the two sub classes (level1 and level2).

The level Manager is created, updated and drawn in the Game1 class. The Draw is implemented just to call the Draw methods of the level1 and level2 classes which in turn pass the draw message on to the components that they contain.

All the classes here only implement or override  a common Update or Draw method, which are most fundamental in Game programming.

So the Game creates the Game Manager.

image

The Level manager creates the Game Levels.

image

The Levels create instances of the level contents Sprites, sound, textures.

There are a couple of Key issues about the structure and control of the levels.

  1. As Level1 and Level2 inherit from Level they can be treated as being the general Level class in some instances. Such as iteration over the collection of Level Objects.
  2. The Sate of a level is kept in the parent class Level and is chosen from the enumerated type LEVELSTATE

image

 

So to the actual State Management and transition of the individual levels. The Level Manager maintains a collection of levels that are created and assigned in the constructor.

class LevelManager
{
// Indicate the last level is complete
bool gameOver = false;

public bool GameOver
{
get { return gameOver; }
set { gameOver = value; }
}
// Counter for the current level
int CurrentLevel = 0;
// Maximum amount of levels

const int MAXLEVEL = 2;

// Collection of levels which are created as subclasses of Level
Level[] Levels;


public LevelManager(Game g)
{
Levels = new Level[MAXLEVEL];
Levels[0] = new Level1(g);
Levels[1] = new Level2(g);

Levels[0].LevelState = LEVELSTATE.PLAYING;

}


Level1 is put into playing mode. It is up to Level1 to transit itself into Finished Mode when it’s end condition is met. In this case a timer is used to time down the fairly static levels. The levels here just show a collection of Chasing Sprites in a location and a player in Level1 that the Chasing sprites will chase.



The transition from playing for a Level is monitored in the Update method of the Level Manager class



 



public void Update(GameTime t)
{
if (!gameOver)
{
foreach (Level l in Levels)
{
if (l != null && l.LevelState == LEVELSTATE.PLAYING)
{ // Update the current playing level
l.Update(t);
// if the current level has finished
if (l.LevelState == LEVELSTATE.FINISHED)
{ // Get rid of the level should
Levels[CurrentLevel] = null;
// and if the not the last level finished
if (++CurrentLevel < MAXLEVEL)
// then play the next level
Levels[CurrentLevel].LevelState = LEVELSTATE.PLAYING;
//Or else we are finished
else gameOver = true;
}
}
}
}

}


The key thing here is that although level1 and level2 are objects in their own right they have a LevelState variable that can be monitored. The Level class definition is shown below



class Level
{

private LEVELSTATE levelState;
public LEVELSTATE LevelState
{
get { return levelState; }
set { levelState = value; }
}
protected ChasingEnemy[] enemies;
protected Sprite[] collectables;
protected Sprite BackGround;
protected Player player;

public virtual void Update(GameTime t)
{

}

public virtual void Draw(SpriteBatch sp)
{

}


The virtual methods are provided in case the methods need to be overridden in the future. Any collections of object that you want to have in levels are added as class variables here and can be populated in the constructor for the sub class for example here is the constructor for the Level1 class



class Level1 : Level
{
float Timer = 5000; // milliseconds

public Level1(Game g)
{
player = new Player(g, g.Content.Load<Texture2D>("FullTank"), new Vector2(400, 600), 1);
enemies = new ChasingEnemy[3];
enemies[0] = new ChasingEnemy(g, g.Content.Load<Texture2D>("EnemyDot"), new Vector2(100, 200), 1);
enemies[1] = new ChasingEnemy(g, g.Content.Load<Texture2D>("EnemyDot"), new Vector2(300, 200), 1);
enemies[2] = new ChasingEnemy(g, g.Content.Load<Texture2D>("EnemyDot"), new Vector2(300, 400), 1);
LevelState = LEVELSTATE.CREATED;
}


NOTE the sub-classing of the Level class is Key as it contains the exposed LevelState and allows all levels to iterated over using for each and treating them all as Level objects for the purposes of this iteration



Here is the state transition in the Level1 update method



public override void Update(GameTime t)
{
if (Timer > 0)
Timer -= t.ElapsedGameTime.Milliseconds;
else
LevelState = LEVELSTATE.FINISHED;

player.Update(t);

foreach (ChasingEnemy c in enemies)
c.follow(player);

foreach (ChasingEnemy c in enemies)
c.Update(t);

}

the full code listing can be found here