using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using System.Reflection; using System.IO; using System.Xml; using System.Xml.Linq; namespace DataDrivenExample { class Constants { // Public properties -- constants available for use // throughout the program public Vector2 Gravity { get; private set; } public float Pi { get; private set; } public bool DebugTriggerArea { get; private set; } public int ScreenWidth { get; private set; } public int ScreenHeight { get; private set; } public static Constants GetInstance() { if (mInstance == null) { mInstance = new Constants(); mInstance.LoadConstants("Content/Constants.xml"); } return mInstance; } // Private methods and data fields, for implementing the static class // and reading the constants in from an XML file private static Constants mInstance; private Constants() { // Set default values for the constants declared in this class // These values can be overriden by the XML file Gravity = new Vector2(0, 9.8f); Pi = 3.14159f; } protected void AddConstant(XElement elem) { XMLParse.AddValueToClassInstance(elem, Constants.GetInstance()); } private void LoadConstants(String constantFile) { using (XmlReader reader = XmlReader.Create(new StreamReader(constantFile))) { XDocument xml = XDocument.Load(reader); XElement root = xml.Root; foreach (XElement elem in root.Elements()) { AddConstant(elem); } } } } }