コード例 #1
0
        /***********************************************************************
        *             Drawing methods to draw the map on the screen
        ***********************************************************************/

        private void clearFore()
        {
            if (engine.getCurrentLevel() < 0)
            {
                MessageBox.Show("Please load a level first!");
                return;
            }
            editSinceLastSave = true;
            int textured = -1;

            for (int a = 0; a < engine.getCurrentMap().getLayer(LayerType.FOREGROUND).getHeight(); a++)
            {
                for (int b = 0; b < engine.getCurrentMap().getLayer(LayerType.FOREGROUND).getWidth(); b++)
                {
                    engine.getCurrentMap().getLayer(LayerType.FOREGROUND).getTile(b, a).setTexture(-1);
                    tileLoader.addToMapLayer(getEditLayer(), b, a, textured);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Loads a level from a file and adds it to the list of levels
        /// </summary>
        /// <param name="file">Input the file name where the level is located. Only input the name, not the extension. Level files by default are stored in the Content/levels/ folder. Ex: To load "Content/levels/level1.txt", simply call loadLevel("level1");</param>
        public void loadLevel(String file)
        {
            StreamReader sr = new StreamReader(file);

            //begin reading data

            // Title
            string title;

            title = sr.ReadLine();

            // Rest of the data
            string[] id = sr.ReadToEnd().Replace(Environment.NewLine, " ").Split(' ');

            int width, height;

            int.TryParse(id[0], out width);
            int.TryParse(id[1], out height);

            // Create the map
            Map newLevel = new Map(title, width, height);

            tileLoader.resetMapLayers(width, height);

            //Populate the map data
            int currentID = 3;
            int textureID = 0;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // Convert the texture ID to an int
                    int.TryParse(id[currentID], out textureID);

                    // Create a new tile at location (x,y) with texture id given and collision flag = 0
                    newLevel.getLayer(LayerType.BACKGROUND).setTile(new Tile(textureID, false), x, y);
                    tileLoader.addToMapLayer(LayerType.BACKGROUND, x, y, textureID);

                    // increment the currentID
                    currentID++;
                }
            }

            currentID++; // For line break after each array
            bool hasCollision = false;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // Convert the texture ID to an int
                    int.TryParse(id[currentID], out textureID);
                    if (textureID == 0)
                    {
                        hasCollision = true;
                    }
                    else
                    {
                        hasCollision = false;
                    }

                    // Create a new tile at location (x,y) with texture id given and collision flag = 0
                    newLevel.getLayer(LayerType.BACKGROUND).getTile(x, y).setCollision(hasCollision);
                    tileLoader.addToMapLayer(LayerType.COLLISION, x, y, textureID);

                    // increment the currentID
                    currentID++;
                }
            }

            currentID++; // For line break after each array
            for (int y = 0; y < height * 2; y++)
            {
                for (int x = 0; x < width * 2; x++)
                {
                    // Convert the texture ID to an int
                    int.TryParse(id[currentID], out textureID);

                    if (x == 59 && y == 44)
                    {
                        Console.WriteLine(textureID);
                    }

                    // Create a new tile at location (x,y) with texture id given and collision flag = 0
                    newLevel.getLayer(LayerType.FOREGROUND).setTile(new Tile(textureID, false), x, y);
                    tileLoader.addToMapLayer(LayerType.FOREGROUND, x, y, textureID);

                    // increment the currentID
                    currentID++;
                }
            }

            currentID++; // For line break after each array
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // Convert the texture ID to an int
                    int.TryParse(id[currentID], out textureID);

                    // Create a new tile at location (x,y) with texture id given and collision flag = 0
                    newLevel.getLayer(LayerType.OBJECTS).setTile(new Tile(textureID, false), x, y);
                    tileLoader.addToMapLayer(LayerType.OBJECTS, x, y, textureID);

                    // increment the currentID
                    currentID++;
                }
            }

            levels.Add(newLevel);
            currentLevel++;

            sr.Close();
        }