I'm trying to load levels in XNA from XML files.
I have currently got a system to do this working, but it looks like it might get horribly complex as I add more objects later on.
My world currently consists of
Planet entities along with physics props, scenery, static props, etc. It will later include NPCs, interactive machines, and a lot more content (but I want to sort this out before going any further).
Based on a tutorial I read, my level loader loads a
LevelData entity from XML. This contains arrays of
StaticData. My test file is as shown:
<?xml version="1.0" encoding="utf-8" ?> <XnaContent> <!-- TODO: replace this Asset with your own XML asset data. --> <Asset Type="DataTypes.LevelData">` <PlanetData> <Item> <Position>5.2 5.2</Position> <BodyRadius>300</BodyRadius> <FieldRadius>450</FieldRadius> <Name>Jupiter</Name> <Rotation>0</Rotation> <ImageName>-</ImageName> </Item> <Item> <Position>-2 -2</Position> <BodyRadius>300</BodyRadius> <FieldRadius>450</FieldRadius> <Name>Jupiter2</Name> <Rotation>0</Rotation> <ImageName>-</ImageName> </Item> </PlanetData> <PropData> <Item> <Type>Crate</Type> <Position>2 3</Position> </Item> </PropData> <StaticData> <Item> <Type>FromImage</Type> <Position>-4.5 -4.5</Position> <ImagePath>Lamp</ImagePath> <Rotation>2</Rotation> <Radius>0</Radius> <Vertices></Vertices> <PlanetOwner>Jupiter2</PlanetOwner> </Item> </StaticData> </Asset> </XnaContent>
In other words, I have to put all my content files into arrays. However, for me to have different types of
StaticData from the XML file, I have to specify the type with a String (called
I then load the XML using
Content.Load<LevelData>("name here.xml"). I go through every
PlanetData in the array and create a new
Planet based on it's properties. I also use switch statements based on the
Type field to specify which type to create...
Is this the only way to load level data into objects using XML (I hope not, it's hideous to use). I couldn't really find much on this topic...