There are lots of ways you could do it.
Though you can do it with lists. Dictionarys are more suited to strings as they map its hash.
define a sprite class
public class Sprite
{
public int x;
public int y;
public string nickName;
public Texture2D texture;
public Sprite(string nickname, Texture2D t)
{
this.nickName = nickname;
this.texture = t;
}
public Sprite(string nickname, Texture2D t, int x, int y)
{
this.nickName = nickname;
this.texture = t;
this.x = x;
this.y = y;
}
public override string ToString()
{
return "Sprite{NickName: "+ nickName + " FileName: " + texture.Name + " Position X: " + x + " Y: " + y +"}";
}
}
Create a class that uses dictonarys of dictionary’s to do what you want.
public class SpriteContainer
{
Microsoft.Xna.Framework.Content.ContentManager content;
Dictionary<string, Dictionary<string, Sprite>> typeItems = new Dictionary<string, Dictionary<string, Sprite>>();
// construct
public SpriteContainer(Microsoft.Xna.Framework.Content.ContentManager content)
{
this.content = content;
}
// load and overload
public void Load(string fileName, string typeName, string nickName)
{
Load(fileName, typeName, nickName, 0, 0);
}
public void Load(string fileName, string typeName, string nickName, int x, int y)
{
if(content == null) { Console.WriteLine("you forgot to pass in content to the constructor"); }
var texture = content.Load<Texture2D>(fileName);
if (typeItems.ContainsKey(typeName))
{
typeItems[typeName].Add(nickName, new Sprite(nickName, texture, x, y) );
}
else
{
Dictionary<string, Sprite> sprites = new Dictionary<string, Sprite>();
sprites.Add(nickName, new Sprite(nickName, texture, x, y) );
typeItems.Add(typeName, sprites);
}
}
// get
public Sprite GetSprite(string typeName, string nickName)
{
return typeItems[typeName][nickName];
}
public Texture2D GetSpriteTexture(string typeName, string nickName)
{
return typeItems[typeName][nickName].texture;
}
// set
public void SetSprites_Xy(string typeName, string nickName, int x, int y)
{
typeItems[typeName][nickName].x = x;
typeItems[typeName][nickName].y = y;
}
}
usage
SpriteContainer sc;
// setup in load
sc = new SpriteContainer(Content);
sc.Load("littleTexture", "energy", "energy1");
use were ever
// set
sc.SetSprites_Xy("energy", "energy1", 1, 10);
// get
var texture = sc.GetSprite("energy", "energy1").texture;
// spriteBatch.Draw(sc.GetSpriteTexture("energy", "energy1") , ... , ... );
Edit saw a booboo