Skip to content

Instantly share code, notes, and snippets.

@cubed2d
Created November 24, 2011 16:10
Show Gist options
  • Save cubed2d/1391703 to your computer and use it in GitHub Desktop.
Save cubed2d/1391703 to your computer and use it in GitHub Desktop.
Swindle Stats manager
public class SatsManger
{
private Dictionary<string, BaseSwindleStat> allStats = new Dictionary<string, BaseSwindleStat>(); // a collection of all the stats that we could unlock
private Dictionary<string, BaseSwindleStat> unlockedStats = new Dictionary<string, BaseSwindleStat>(); // a collection of stats we have unlocekd.
public bool HasUnlockedStat(string statName)
{
return unlockedStats.ContainsKey(statName);
}
// Example: getting a stat called JumpCount that is an int representing the amout of times you can doublejump.
// var jumpCount = statManager.GetStat<int>("JumpCount");
// jumpCount.Value stores the ammount of jumps you can make :)
public IReadOnlySwindleStat<T> GetStat<T>(string statName)
{
BaseSwindleStat stat = null;
if (this.unlockedStats.TryGetValue(statName, out stat))
{
if (stat is SwindleStat<T>)
{
return stat as IReadOnlySwindleStat<T>;
}
}
return null;
}
}
// All you need to do now is add a way to add stats to the manager, store how much they cost when you upgrade them and all that kinda stuff.
// Example: createing a SwindleStat to hold the ammount of times your allowed to doublejump
// var jumpCount = new SwindleStat<int>(statsManger,"JumpCount")
// jumpCount.Value = 2;
// this is the base class for a stat. We need this so we can mix differnt types of stats in a collection. Hopfuly the thinking here will become clear enough soon
// abstract means you cant instance it directly. This is also important :)
public abstract class BaseSwindleStat
{
public SatsManger Owner { get; private set; }
public string Name { get; set; }
public BaseSwindleStat(SatsManger owner, string name)
{
Owner = owner;
Name = name;
}
}
// an interface that provides restricted access to the getters of the stat data. We dont want anything other than the stat manager to changes values.
public interface IReadOnlySwindleStat<T>
{
string Name { get; }
T Value { get; }
int Cost { get; }
}
// actuall implementation of the stat. holds the value we want to share (the T Value bit)
public class SwindleStat<T> : BaseSwindleStat, IReadOnlySwindleStat<T>
{
public class StatLevel
{
public int Cost;
public T Value;
}
private List<StatLevel> UpgradeList;
private int level;
public SwindleStat(PlayerStatManager manager, string name, List<StatLevel> upgradeList) : base(manager, name)
{
this.UpgradeList = upgradeList;
}
public T Value
{
get { return UpgradeList[level].Value; }
}
public int Cost
{
get { return UpgradeList[level].Cost; }
}
public void Upgrade()
{
level++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment