Esempio n. 1
0
 protected override void OnLoad(EventArgs e)
 {
     scene = SceneLoader.FromFile("Levels\\Test.xml");
     base.OnLoad(e);
 }
        public static Scene FromXml(string xml)
        {
            const string me = "SceneLoader";
            NumberStyles styles = NumberStyles.Number;
            CultureInfo culture = CultureInfo.InvariantCulture;
            Scene scene = new Scene();

            scene.Resources.LoadResource("Sound\\Environment\\WalkerPropCollision.mp3");
            scene.Jukebox.AssertSound("Sound\\Environment\\WalkerPropCollision.mp3", "WalkerPropCollision");

            scene.Resources.LoadResource("Sound\\Footsteps\\step1.mp3");
            scene.Resources.LoadResource("Sound\\Footsteps\\step2.mp3");
            scene.Resources.LoadResource("Sound\\Footsteps\\step3.mp3");
            scene.Resources.LoadResource("Sound\\Footsteps\\step4.mp3");
            scene.Jukebox.AssertSound("Sound\\Footsteps\\step1.mp3", "step1");
            scene.Jukebox.AssertSound("Sound\\Footsteps\\step2.mp3", "step2");
            scene.Jukebox.AssertSound("Sound\\Footsteps\\step3.mp3", "step3");
            scene.Jukebox.AssertSound("Sound\\Footsteps\\step4.mp3", "step4");
            scene.Player.AddStepSound("step1");
            scene.Player.AddStepSound("step2");
            scene.Player.AddStepSound("step3");
            scene.Player.AddStepSound("step4");

            XmlDocument document = new XmlDocument();
            document.LoadXml(xml);
            XmlElement level = document["level"];

            XmlElement resources = level["resources"];
            XmlElement entities = level["entities"];
            XmlElement scripts = level["scripts"];
            XmlElement player = level["player"];

            Func<XmlElement, string, double> ParseDoubleAttr = (element, name) =>
                double.Parse(element.Attributes[name].Value, styles, culture);
            Func<XmlElement, string, bool> ParseBoolAttr = (element, name) =>
                bool.Parse(element.Attributes[name].Value);

            scene.Player.X = ParseDoubleAttr(player, "x");
            scene.Player.Y = ParseDoubleAttr(player, "y");
            scene.Player.Z = ParseDoubleAttr(player, "z");

            foreach (XmlElement child in resources.ChildNodes)
            {
                if (child.Name == "resource")
                {
                    string path = child.Attributes["path"].Value;
                    string macro = child.Attributes["macro"].Value;
                    scene.Resources.LoadResource(path);
                    scene.Jukebox.AssertSound(path, macro);
                    LogConsole.Info(me, "Loading '{0}'", path);
                }
            }

            foreach (XmlElement child in scripts.ChildNodes)
            {
                // TODO: Parse 'n compile scripts!
            }

            foreach (XmlElement child in entities.ChildNodes)
            {
                if (child.Name == "entity")
                {
                    var typename = "InvisibilityGame." + child.Attributes["class"].Value;
                    var args = new object[1] { child.Attributes["name"].Value };
                    var type = Type.GetType(typename);
                    Entity ent = (Entity)Activator
                        .CreateInstance(type, args);

                    if (child.HasAttribute("x"))
                        ent.X = ParseDoubleAttr(child, "x");
                    if (child.HasAttribute("y"))
                        ent.Y = ParseDoubleAttr(child, "y");
                    if (child.HasAttribute("z"))
                        ent.Z = ParseDoubleAttr(child, "z");
                    if (child.HasAttribute("width"))
                        ent.Width = ParseDoubleAttr(child, "width");
                    if (child.HasAttribute("height"))
                        ent.Height = ParseDoubleAttr(child, "height");
                    if (child.HasAttribute("length"))
                        ent.Length = ParseDoubleAttr(child, "length");

                    scene.AddEntity(ent);
                }
            }

            return scene;
        }