Esempio n. 1
0
        public void Load(string levelFileName)
        {
            this.levelFileName = levelFileName;

            List<string> controllerId = new List<string>();
            List<string> controlledId = new List<string>();

            XmlTextReader reader = new XmlTextReader("Content/level/" + levelFileName);
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "size")
                    {
                        string width = reader.GetAttribute("width");
                        string height = reader.GetAttribute("height");
                        string treasureCountStr = reader.GetAttribute("treasureCount");

                        int treasureCount = int.Parse(treasureCountStr);
                        this.width = int.Parse(width);
                        this.height = int.Parse(height);

                        this.treasureMgr = new TreasureManager(this.levelFileName, treasureCount);
                    }
                    else if (reader.Name == "background")
                    {
                        backgroundTextureAssetName = reader.GetAttribute("texture");
                        string backgroundColorStr = reader.GetAttribute("color");
                        if (backgroundColorStr == "black")
                            backgroundColor = Color.Black;
                    }
                    else if (reader.Name == "foreground")
                    {
                        foregroundTextureAssetName = reader.GetAttribute("texture");
                        string bottom = reader.GetAttribute("bottom");
                    }
                    else if (reader.Name == "bound")
                    {
                        string leftStr = reader.GetAttribute("left");
                        string rightStr = reader.GetAttribute("right");
                        string topStr = reader.GetAttribute("top");
                        string bottomStr = reader.GetAttribute("bottom");
                        leftBound = int.Parse(leftStr);
                        rightBound = int.Parse(rightStr);
                        topBound = int.Parse(topStr);
                        bottomBound = int.Parse(bottomStr);
                        Block leftBoundBlock = new Block(game, new Vector2(leftBound, 0), 0, height);
                        Block rightBoundBlock = new Block(game, new Vector2(rightBound, 0), 0, height);
                        Block topBoundBlock = new Block(game, new Vector2(0, topBound), width, 0);
                        Block bottomBoundBlock = new Block(game, new Vector2(0, bottomBound), width, 0);
                        objects.Add(leftBoundBlock);
                        objects.Add(rightBoundBlock);
                        objects.Add(topBoundBlock);
                        objects.Add(bottomBoundBlock);
                    }
                    else if (reader.Name == "player")
                    {
                        player = new Player(game, reader);
                    }
                    else if (reader.Name == "enemy")
                    {
                        Enemy enemy = new Enemy(game, reader);
                    }
                    else if (reader.Name == "shiftStick")
                    {
                        ShiftStick stick = new ShiftStick(game, reader);

                        XmlReader subtree = reader.ReadSubtree();
                        while (subtree.Read())
                        {
                            if (subtree.NodeType == XmlNodeType.Element &&
                                subtree.Name == "controlled")
                            {
                                controllerId.Add(stick.Id);
                                controlledId.Add(subtree.GetAttribute("objId"));
                            }
                        }
                    }
                    else if (reader.Name == "lightsource")
                    {
                        LightSource light1 = new LightSource(game, reader);
                        lightSource.Add(light1);
                    }
                    else if (reader.Name == "mirror")
                    {
                        Mirror mirror1 = new Mirror(game, reader);
                    }
                    else if (reader.Name == "switch")
                    {
                        Switch switch1 = new Switch(game, reader);

                        XmlReader subtree = reader.ReadSubtree();
                        while (subtree.Read())
                        {
                            if (subtree.NodeType == XmlNodeType.Element &&
                                subtree.Name == "controlled")
                            {
                                controllerId.Add(switch1.Id);
                                controlledId.Add(subtree.GetAttribute("objId"));
                            }
                        }
                    }
                    else if (reader.Name == "ladder")
                    {
                        Ladder ladder = new Ladder(game, reader);
                    }
                    else if (reader.Name == "door")
                    {
                        Door door = new Door(game, reader);
                    }
                    else if (reader.Name == "platform")
                    {
                        Platform platform = new Platform(game, reader);
                    }
                    else if (reader.Name == "block")
                    {
                        Block block = new Block(game, reader);
                    }
                    else if (reader.Name == "launcher")
                    {
                        Launcher launcher = new Launcher(game, reader);
                    }
                    else if (reader.Name == "treasure")
                    {
                        Treasure treasure;
                        bool flag = false;
                        List<String> ids = this.treasureMgr.AreGotten();
                        foreach (String id in ids)
                        {
                            if (reader.GetAttribute("id") == id)
                            {
                                treasure = new Treasure(game, reader, true);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            treasure = new Treasure(game, reader, false);
                        }
                    }
                    else if (reader.Name == "deadlyobj")
                    {
                        DeadlyObject deadlyobj = new DeadlyObject(game, reader);
                    }
                    else if (reader.Name == "ogre")
                    {
                        Ogre ogre = new Ogre(game, reader);
                    }
                    else if (reader.Name == "gate")
                    {
                        Gate gate = new Gate(game, reader);
                    }
                    else if (reader.Name == "topic")
                    {
                        string pxStr = reader.GetAttribute("px");
                        string pyStr = reader.GetAttribute("py");

                        topic = reader.ReadElementString();

                        float px = float.Parse(pxStr);
                        float py = float.Parse(pyStr);
                        topicPos = new Vector2(px, py);
                    }
                    else if (reader.Name == "bindingPoint")
                    {
                        if (Game.BindingPoint == null)
                        {
                            Game.BindingPoint = new BindingPoint();
                            XmlReader subtree = reader.ReadSubtree();
                            while (subtree.Read())
                            {
                                if (subtree.NodeType == XmlNodeType.Element &&
                                subtree.Name == "p")
                                {
                                    string pxStr = subtree.GetAttribute("px");
                                    string pyStr = subtree.GetAttribute("py");
                                    string id = subtree.GetAttribute("id");

                                    Vector2 pos = new Vector2(int.Parse(pxStr), int.Parse(pyStr));
                                    Game.BindingPoint.Add(pos, levelFileName, id);
                                }
                            }
                        }
                    }
                }
            }

            reader.Close();

            List<string>.Enumerator controllerEnum = controllerId.GetEnumerator();
            List<string>.Enumerator controlledEnum = controlledId.GetEnumerator();
            while (controllerEnum.MoveNext() && controlledEnum.MoveNext())
            {
                string controllerStr = controllerEnum.Current;
                string controlledStr = controlledEnum.Current;
                IController controller = (IController)GetObjectById(controllerStr);
                IControlledObject controlled = (IControlledObject)GetObjectById(controlledStr);
                controller.Add(controlled);
            }

            Initialize();
            LoadContent();
        }
Esempio n. 2
0
        public void Load(string levelFileName)
        {
            this.levelFileName = levelFileName;

            List <string> controllerId = new List <string>();
            List <string> controlledId = new List <string>();

            XmlTextReader reader = new XmlTextReader("Content/level/" + levelFileName);

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "size")
                    {
                        string width            = reader.GetAttribute("width");
                        string height           = reader.GetAttribute("height");
                        string treasureCountStr = reader.GetAttribute("treasureCount");

                        int treasureCount = int.Parse(treasureCountStr);
                        this.width  = int.Parse(width);
                        this.height = int.Parse(height);

                        this.treasureMgr = new TreasureManager(this.levelFileName, treasureCount);
                    }
                    else if (reader.Name == "background")
                    {
                        backgroundTextureAssetName = reader.GetAttribute("texture");
                        string backgroundColorStr = reader.GetAttribute("color");
                        if (backgroundColorStr == "black")
                        {
                            backgroundColor = Color.Black;
                        }
                    }
                    else if (reader.Name == "foreground")
                    {
                        foregroundTextureAssetName = reader.GetAttribute("texture");
                        string bottom = reader.GetAttribute("bottom");
                    }
                    else if (reader.Name == "bound")
                    {
                        string leftStr   = reader.GetAttribute("left");
                        string rightStr  = reader.GetAttribute("right");
                        string topStr    = reader.GetAttribute("top");
                        string bottomStr = reader.GetAttribute("bottom");
                        leftBound   = int.Parse(leftStr);
                        rightBound  = int.Parse(rightStr);
                        topBound    = int.Parse(topStr);
                        bottomBound = int.Parse(bottomStr);
                        Block leftBoundBlock   = new Block(game, new Vector2(leftBound, 0), 0, height);
                        Block rightBoundBlock  = new Block(game, new Vector2(rightBound, 0), 0, height);
                        Block topBoundBlock    = new Block(game, new Vector2(0, topBound), width, 0);
                        Block bottomBoundBlock = new Block(game, new Vector2(0, bottomBound), width, 0);
                        objects.Add(leftBoundBlock);
                        objects.Add(rightBoundBlock);
                        objects.Add(topBoundBlock);
                        objects.Add(bottomBoundBlock);
                    }
                    else if (reader.Name == "player")
                    {
                        player = new Player(game, reader);
                    }
                    else if (reader.Name == "enemy")
                    {
                        Enemy enemy = new Enemy(game, reader);
                    }
                    else if (reader.Name == "shiftStick")
                    {
                        ShiftStick stick = new ShiftStick(game, reader);

                        XmlReader subtree = reader.ReadSubtree();
                        while (subtree.Read())
                        {
                            if (subtree.NodeType == XmlNodeType.Element &&
                                subtree.Name == "controlled")
                            {
                                controllerId.Add(stick.Id);
                                controlledId.Add(subtree.GetAttribute("objId"));
                            }
                        }
                    }
                    else if (reader.Name == "lightsource")
                    {
                        LightSource light1 = new LightSource(game, reader);
                        lightSource.Add(light1);
                    }
                    else if (reader.Name == "mirror")
                    {
                        Mirror mirror1 = new Mirror(game, reader);
                    }
                    else if (reader.Name == "switch")
                    {
                        Switch switch1 = new Switch(game, reader);

                        XmlReader subtree = reader.ReadSubtree();
                        while (subtree.Read())
                        {
                            if (subtree.NodeType == XmlNodeType.Element &&
                                subtree.Name == "controlled")
                            {
                                controllerId.Add(switch1.Id);
                                controlledId.Add(subtree.GetAttribute("objId"));
                            }
                        }
                    }
                    else if (reader.Name == "ladder")
                    {
                        Ladder ladder = new Ladder(game, reader);
                    }
                    else if (reader.Name == "door")
                    {
                        Door door = new Door(game, reader);
                    }
                    else if (reader.Name == "platform")
                    {
                        Platform platform = new Platform(game, reader);
                    }
                    else if (reader.Name == "block")
                    {
                        Block block = new Block(game, reader);
                    }
                    else if (reader.Name == "launcher")
                    {
                        Launcher launcher = new Launcher(game, reader);
                    }
                    else if (reader.Name == "treasure")
                    {
                        Treasure      treasure;
                        bool          flag = false;
                        List <String> ids  = this.treasureMgr.AreGotten();
                        foreach (String id in ids)
                        {
                            if (reader.GetAttribute("id") == id)
                            {
                                treasure = new Treasure(game, reader, true);
                                flag     = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            treasure = new Treasure(game, reader, false);
                        }
                    }
                    else if (reader.Name == "deadlyobj")
                    {
                        DeadlyObject deadlyobj = new DeadlyObject(game, reader);
                    }
                    else if (reader.Name == "ogre")
                    {
                        Ogre ogre = new Ogre(game, reader);
                    }
                    else if (reader.Name == "gate")
                    {
                        Gate gate = new Gate(game, reader);
                    }
                    else if (reader.Name == "topic")
                    {
                        string pxStr = reader.GetAttribute("px");
                        string pyStr = reader.GetAttribute("py");

                        topic = reader.ReadElementString();

                        float px = float.Parse(pxStr);
                        float py = float.Parse(pyStr);
                        topicPos = new Vector2(px, py);
                    }
                    else if (reader.Name == "bindingPoint")
                    {
                        if (Game.BindingPoint == null)
                        {
                            Game.BindingPoint = new BindingPoint();
                            XmlReader subtree = reader.ReadSubtree();
                            while (subtree.Read())
                            {
                                if (subtree.NodeType == XmlNodeType.Element &&
                                    subtree.Name == "p")
                                {
                                    string pxStr = subtree.GetAttribute("px");
                                    string pyStr = subtree.GetAttribute("py");
                                    string id    = subtree.GetAttribute("id");

                                    Vector2 pos = new Vector2(int.Parse(pxStr), int.Parse(pyStr));
                                    Game.BindingPoint.Add(pos, levelFileName, id);
                                }
                            }
                        }
                    }
                }
            }

            reader.Close();

            List <string> .Enumerator controllerEnum = controllerId.GetEnumerator();
            List <string> .Enumerator controlledEnum = controlledId.GetEnumerator();
            while (controllerEnum.MoveNext() && controlledEnum.MoveNext())
            {
                string            controllerStr = controllerEnum.Current;
                string            controlledStr = controlledEnum.Current;
                IController       controller    = (IController)GetObjectById(controllerStr);
                IControlledObject controlled    = (IControlledObject)GetObjectById(controlledStr);
                controller.Add(controlled);
            }

            Initialize();
            LoadContent();
        }