public static void AddObjectsToLoad(ICheckpointLoadable loadable)
        {
            if (listLoadable == null)
            {
                listLoadable = new List <ICheckpointLoadable>();
            }

            listLoadable.Add(loadable);
        }
示例#2
0
        public static void LoadTiledMap(string filename)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(filename);

            XmlNode root       = doc.DocumentElement;
            int     cols       = int.Parse(root.Attributes["width"].Value);
            int     rows       = int.Parse(root.Attributes["height"].Value);
            int     tileWidth  = int.Parse(root.Attributes["tilewidth"].Value);
            int     tileHeight = int.Parse(root.Attributes["tileheight"].Value);

            XmlNode tileset = root.FirstChild;
            string  source  = tileset.Attributes["source"].Value;

            Dictionary <int, XmlNode> dictProperties;
            List <string>             tileNames = LoadTileSet("Assets/Tiles/" + source, out dictProperties);

            //create ground without colliison
            XmlNode groundData = tileset.NextSibling.FirstChild;

            CreateTileLayer(groundData, ref tileNames, tileWidth, tileHeight, cols, 1);

            //create water
            XmlNode     waterNode = tileset.NextSibling.NextSibling;
            XmlNodeList list      = waterNode.ChildNodes;

            foreach (XmlNode objectNode in list)
            {
                InstantiateWater(objectNode, dictProperties);
            }

            //create middleground object
            XmlNode middleGroundObjects = waterNode.NextSibling;

            CreateObjectsLayers(middleGroundObjects.ChildNodes, ref tileNames, DrawManager.Layer.Middleground);


            //create ground with collision

            XmlNode dataGroundWithRigidbody = middleGroundObjects.NextSibling.FirstChild;

            CreateTileLayer(dataGroundWithRigidbody, ref tileNames, tileWidth, tileHeight, cols);

            /*
             * Create PlayGround Objects
             *
             */

            List <ICheckpointLoadable> objectLoadable = new List <ICheckpointLoadable>();


            //Create player
            XmlNode playerObj  = dataGroundWithRigidbody.ParentNode.NextSibling;
            XmlNode playerNode = playerObj.FirstChild;

            Player p = null;

            if (playerNode != null)
            {
                string xString = playerNode.Attributes["x"].Value;
                float  xPos    = float.Parse(xString, System.Globalization.CultureInfo.InvariantCulture);

                string yString = playerNode.Attributes["y"].Value;
                float  yPos    = float.Parse(yString, System.Globalization.CultureInfo.InvariantCulture);

                Vector2 playerPos = new Vector2(xPos + (int.Parse(playerNode.Attributes["width"].Value) / 2), yPos - (int.Parse(playerNode.Attributes["height"].Value) / 2));

                p = new Player(playerPos);
                PlayScene.Player = p;
            }
            else
            {
                p = new Player(new Vector2(40, Game.Window.Height - 260));
                PlayScene.Player = p;
            }

            objectLoadable.Add(p);

            XmlNode crateNode = playerObj.NextSibling;

            list = crateNode.ChildNodes;

            Dictionary <BorderCrate, int>       borderCrates  = new Dictionary <BorderCrate, int>();
            Dictionary <int, TriggerIronCrate>  triggerCrates = new Dictionary <int, TriggerIronCrate>();
            Dictionary <int, TriggerNitroCrate> triggerNitro  = new Dictionary <int, TriggerNitroCrate>();
            List <CheckpointCrate> checkpoints = new List <CheckpointCrate>();

            foreach (XmlNode objectNode in list)
            {
                InstantiateCrate(objectNode, dictProperties, ref borderCrates, ref triggerCrates,
                                 ref triggerNitro, ref objectLoadable, ref checkpoints);
            }

            foreach (KeyValuePair <BorderCrate, int> b in borderCrates)
            {
                triggerCrates[b.Value].AddCrate(b.Key);
            }

            XmlNode pickableNode = crateNode.NextSibling;

            list = pickableNode.ChildNodes;


            foreach (XmlNode objectNode in list)
            {
                InstantiatePickable(objectNode, dictProperties, ref objectLoadable);
            }

            XmlNode enemiesNode = pickableNode.NextSibling;

            list = enemiesNode.ChildNodes;


            foreach (XmlNode objectNode in list)
            {
                InstantiateEnemies(objectNode, dictProperties, ref objectLoadable);
            }

            XmlNode objectsWalkableNode = enemiesNode.NextSibling;

            list = objectsWalkableNode.ChildNodes;


            foreach (XmlNode objectNode in list)
            {
                InstantiateObjectsWakable(objectNode, dictProperties);
            }

            XmlNode trapNode = objectsWalkableNode.NextSibling;

            list = trapNode.ChildNodes;

            Dictionary <int, ITriggableAction> trapsTriggable = new Dictionary <int, ITriggableAction>();

            foreach (XmlNode objectNode in list)
            {
                InstantiateTraps(objectNode, dictProperties, ref trapsTriggable, ref objectLoadable);
            }

            XmlNode triggersEventNode = trapNode.NextSibling;

            list = triggersEventNode.ChildNodes;

            foreach (XmlNode objectNode in list)
            {
                InstantiateTriggersEvent(objectNode, dictProperties, ref trapsTriggable, ref objectLoadable);
            }

            //create foregroundobjects
            XmlNode foreGroundObjects = triggersEventNode.NextSibling;

            CreateObjectsLayers(foreGroundObjects.ChildNodes, ref tileNames, DrawManager.Layer.Foreground);
            //method to create foreground

            for (int i = 0; i < objectLoadable.Count; i++)
            {
                ICheckpointLoadable loadable = objectLoadable[i];
                CheckpointCrate.AddObjectsToLoad(loadable);
            }

            GameManager.Loadables = objectLoadable;
        }