Original Post
Hi all, I've been working on an RPG in C# and XNA (just so you understand the syntax) and have been storing my game details in XML files. The main problem I have is that my design doesn't seem all that great. Basically id numbers are used to associate everything, sort of like a database, but then anything that uses the ID's need access to the 'database'. I think an example will best suit this. stats.xml players.xml Stats ('Database' for the stats.xml - I realise it doesn't load in the XML file atm, but its just as a test) Stat.cs Players.cs ('Database' for the players) As you can see if you bothered to read through all that jibberish is that each 'Player' is given the pointer to the 'Stats' database. It feels to me like this is a somewhat bad design, can anyone suggest any kind of improvement? Thank you for even just reading all this, let alone helping -SD
<?xml version="1.0" encoding="utf-8" ?>
<stats>
<stat id="1">
<name>Health Points</name>
<shortname>HP</shortname>
<description>The amount of health you have. Reaching 0 will cause death.</description>
</stat>
<stat id="2">
<name>Mana Points</name>
<shortname>MP</shortname>
<description>The amount of magic you can cast.</description>
</stat>
<stat id="3">
<name>Strength</name>
<shortname>Str</shortname>
<description>Your physical condition.</description>
</stat>
</stats>
<?xml version="1.0" encoding="utf-8" ?>
<players>
<player id="1">
<name>Jim Amaranthine</name>
<menuimage file="maps/tiles/tile2" />
<animations file="sprites/knt1">
<animation name="UP">
<frame x="0" y="0" width="32" height="32" />
<frame x="32" y="0" width="32" height="32" />
</animation>
<animation name="DOWN">
<frame x="64" y="0" width="32" height="32" />
<frame x="96" y="0" width="32" height="32" />
</animation>
<animation name="LEFT">
<frame x="128" y="0" width="32" height="32" />
<frame x="160" y="0" width="32" height="32" />
</animation>
<animation name="RIGHT">
<frame x="192" y="0" width="32" height="32" />
<frame x="224" y="0" width="32" height="32" />
</animation>
</animations>
<levels>
<level id="1">
<stats>
<stat id="1" increaseby="50" />
<stat id="2" increaseby="25" />
</stats>
</level>
</levels>
<currentlevel>1</currentlevel>
</player>
</players>
using System;
using System.Collections.Generic;
using System.Text;
namespace TileEngine
{
public class Stats
{
private Dictionary<int, Stat> stats;
public Stats(String statXMLFilename)
{
stats = new Dictionary<int, Stat>();
// TODO: Load stats from the XML File
stats.Add(1, new Stat("Health", "HP", "HEALTH YEA"));
stats.Add(2, new Stat("Mana", "MP", "OH It's MANAAAA"));
}
public Stat getStat(int id)
{
if (stats.ContainsKey(id))
return stats[id];
return null;
}
public int getId(Stat stat)
{
return 0;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TileEngine
{
public class Stat
{
private String name;
private String shortname;
private String description;
public String Name
{
get
{
return name;
}
}
public String ShortName
{
get
{
return shortname;
}
}
public String Description
{
get
{
return description;
}
}
public Stat(String name, String shortname, String description)
{
this.name = name;
this.shortname = shortname;
this.description = description;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace TileEngine
{
public class Players
{
private Dictionary<int, Player> players;
private Stats stats;
public Players(ContentManager content, String XMLFilename, Stats stats)
{
// TODO: Load players from the XML File
players = new Dictionary<int, Player>();
this.stats = stats;
XmlTextReader xmlReader;
try
{
xmlReader = new XmlTextReader(XMLFilename);
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Element:
{
if (xmlReader.Name.Equals("player"))
{
Player player = new Player(stats);
int id = int.Parse(xmlReader.GetAttribute("id"));
//String name = "";
//Dictionary<String, FrameAnimation> animations = new Dictionary<string,FrameAnimation>();
//Texture2D animationTexture = null;
//Texture2D menuImage = null;
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name.Equals("player"))
{
break;
}
if (xmlReader.NodeType == XmlNodeType.Element)
{
if (xmlReader.Name.Equals("name"))
{
player.Name = xmlReader.ReadString();
}
else if (xmlReader.Name.Equals("menuimage"))
{
String filename = xmlReader.GetAttribute("file");
if (!String.IsNullOrEmpty(filename))
player.MenuImage = content.Load<Texture2D>(filename);
}
else if (xmlReader.Name.Equals("animation"))
{
String animationName = xmlReader.GetAttribute("name");
List<Rectangle> rects = new List<Rectangle>();
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name.Equals("animation"))
{
break;
}
if (xmlReader.Name.Equals("frame"))
{
int posX, posY, width, height;
posX = int.Parse(xmlReader.GetAttribute("x"));
posY = int.Parse(xmlReader.GetAttribute("y"));
width = int.Parse(xmlReader.GetAttribute("width"));
height = int.Parse(xmlReader.GetAttribute("height"));
rects.Add(new Rectangle(posX, posY, width, height));
}
}
player.AddAnimation(animationName, new FrameAnimation(rects));
}
else if (xmlReader.Name.Equals("animations"))
{
String filename = xmlReader.GetAttribute("file");
if (!String.IsNullOrEmpty(filename))
player.AnimationTexture = content.Load<Texture2D>(filename);
}
else if (xmlReader.Name.Equals("level"))
{
String level = xmlReader.GetAttribute("id");
List<StatValue> levelStats = new List<StatValue>();
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name.Equals("level"))
{
break;
}
if (xmlReader.Name.Equals("stat"))
{
int statId = int.Parse(xmlReader.GetAttribute("id"));
int value = int.Parse(xmlReader.GetAttribute("increaseby"));
levelStats.Add(new StatValue(statId, value));
}
}
player.AddLevel(int.Parse(level), levelStats);
}
else if (xmlReader.Name.Equals("currentlevel"))
{
player.setLevel(int.Parse(xmlReader.ReadString()));
}
}
}
players.Add(id, player);
}
break;
}
}
}
}
catch (XmlException xmle)
{
Console.WriteLine(xmle.Message);
}
}
public Player getPlayer(int id)
{
if (players.ContainsKey(id))
return players[id];
else
return null;
}
}
}