Game Development Reference
In-Depth Information
Saving a level requires creating and writing an XML file like this:
private void saveLevelToolStripMenuItem_Click(object sender, EventArgs e)
{
XmlDocument levelXml = new XmlDocument();
XmlElement world = levelXml.CreateElement(
World
);
levelXml.AppendChild(world);
XmlElement staticActors = levelXml.CreateElement(
StaticActors
);
world.AppendChild(staticActors);
int[] actorList = GetActorList();
for (int i = 0; i < actorList.GetLength(0); i++)
{
uint actorId = Convert.ToUInt32(actorList[i]);
XmlElement actorXml = GetActorXml(actorId);
if (actorXml != null)
{
staticActors.AppendChild(
staticActors.OwnerDocument.ImportNode(actorXml, true));
}
}
// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter(m_CurrentLevelFile, null);
writer.Formatting = Formatting.Indented;
levelXml.Save(writer);
}
The level file format is basically a list of actors and their components.
<World>
<StaticActors>
<Actor type=
Grid
>
component XML definitions go here
</Actor>
<Actor type=
Light
>
component XML definitions go here
</Actor>
</StaticActors>
</World>
The root level element, World , contains the actor list defined by the StaticActors
element. A good extension for this format might add level specific information, such
as a definition for background music or where the level chains to after it is com-
pleted. In this simple example, just the actor list is shown.
Search WWH ::




Custom Search