Awardments for your XBLIG Part 2
In part 1 of this tutorial serious, we created a basic Awardments class (if you skipped part 1, I'd advise visiting that first and then coming back here). Today we will create a collection for our Awardments and add some method for updating them.
While we could use a basic List to hold instances of our Awardment class, I've decided to instead use a Collection. This enables us to keep all our Awardments together in one object that can also have it's own associated methods (you'll need to add "using System.Collections.Generic;" to the top of your code file to use Collections).
The following code contains the Awardments class with boolean values for flagging if Awardments are completed and an Update() method for checking for these completions. More description after the code...
public class Awardments : System.Collections.ObjectModel.Collection<Awardment>
{
public bool CompleteAllLevels
{
get;
set;
}
public bool CollectAllGems
{
get;
set;
}
public bool CompleteLevelWithoutDeath
{
get;
set;
}
public Awardments(ContentManager content)
{
this.CompleteAllLevels = false;
this.CollectAllGems = false;
this.CompleteLevelWithoutDeath = false;
// Add all awardments we want to use.
Awardment awardment1 = new Awardment(AwardmentType.LevelWithoutDeath, content);
this.Add(awardment1);
Awardment awardment2 = new Awardment(AwardmentType.CollectAllGems, content);
this.Add(awardment2);
Awardment awardment3 = new Awardment(AwardmentType.GluttonForPunishment, content);
this.Add(awardment3);
Awardment awardment4 = new Awardment(AwardmentType.YouCompleteMe, content);
this.Add(awardment4);
}
/// <summary>
/// Loop and determine which awardments are newly completed.
/// </summary>
public void Update(SoundBank soundBank)
{
for (int i = 0; i < this.Items.Count; i++)
{
// Test only incomplete Awardments.
if (!this.Items[i].Complete)
{
switch (this.Items[i].CurrentAwardmentType)
{
case AwardmentType.YouCompleteMe:
{
if (this.CompleteAllLevels)
{
this.Items[i].Complete = true;
}
break;
}
case AwardmentType.CollectAllGems:
{
if (this.CollectAllGems)
{
this.Items[i].Complete = true;
}
break;
}
case AwardmentType.GluttonForPunishment:
{
// These are global values used in Volchaos. Feel free to use your own.
if ((Program.EnemyDeaths + Program.LavaDeaths + Program.SpikeDeaths) >= 100)
{
this.Items[i].Complete = true;
}
break;
}
case AwardmentType.LevelWithoutDeath:
{
if (this.CompleteLevelWithoutDeath)
{
this.Items[i].Complete = true;
}
break;
}
}
// This event was just completed.
// Play a sound and take other actions.
if (this.Items[i].Complete)
{
soundBank.PlayCue("Awardment");
this.Items[i].Complete = true;
}
}
}
}
}
To use these Awardments, we must first create an Awardments object and pass it a ContentManager object as the constructor. Then we need to call the Update method. You'll want to place the Update() method where it gets called every frame (it's not necessary now but once we add the onscreen Awardment display stuff you'll need it, so might as well put it in the correct spot in the first place). The Update() method is what will determine if an Awardment has been completed.
Awardments awardments;
awardments = new Awardments(this.content);
// Place this update call where it belongs!
awardments.Update(this.soundBank);
// If the user collected all the gems, do this:
awardments.CollectAllGems = false;
For the YouCompleteMe, CollectAllGems, and LevelWithoutDeath Awardments, we use the similarly named boolean values on our Awardments object... just set any of these values to true when the Awardment is completed in your code and your Awardment will be completed in this object the next time the Update() method is called. For the remaining GluttonForPunishment Awardment, we're using a couple global values to test for completion. This gives you an example of another way you can determine completeness of an Awardment.
In the next set of tutorials, I'll show you how you can display on screen when Awardments are completed and finally we'll get into saving and loading Awardments (so the player doesn't have to complete them every time they play). We're half way there!
This article has been view 1046 times.
|