XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Need help with a LevelEditor for a 2D game

Last post 06-01-2008 1:33 AM by ReVarre. 3 replies.
  • 05-31-2008 12:27 PM

    Need help with a LevelEditor for a 2D game

    Hi, I'm in need of help with a 2D game, I've made a leveleditor and I've made it write the new maps to a new file.

    When the maps are written out of the level editor they look like this:

    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSWWWSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSWPWWWWWWSSSSSSSSSSSSSSSSSSS
    SSSSSWFFFFFFWSSSSSSSSSSSSSSSSSSS
    SSSSSWFFFFFFWSSSSSSSSSSSSSSSSSSS
    SSSSSWFFFFFFWSSSSSSSSSSSSSSSSSSS
    SSSWWWFFRFFFWSSSSSSSSSSSSSSSSSSS
    SSSWGFFFRFFFWSSSSSSSSSSSSSSSSSSS
    SSSWGFFFFFFFWSSSSSSSSSSSSSSSSSSS
    SSSWWWWWWWWWWSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

    As you can see the map is drawn out pretty roughly, what I need it to do is to change it to this:

    SSWPWWWWWW
    SSWFFFFFFW
    SSWFFFFFFW
    SSWFFFFFFW
    WWWFFRFFFW
    WGFFFRFFFW
    WGFFFFFFFW
    WWWWWWWWWW

    Why I want it to do this is because I'm using an xOffset and yOffset to center the map drawn. And I'm pretty much stuck how to do this.

    I got an idea that if I get the nearest walls (W) amount of Spacers (S) I can strip the map of that amount. Though, this wont do since it if the second closest wall is equal to 6 and the closest wall is equal to 3, it will strip both of them of their spacers to nothingness..


     
    using System;  
    using System.IO;  
    using System.Collections.Generic;  
    using Microsoft.Xna.Framework;  
    using Microsoft.Xna.Framework.Audio;  
    using Microsoft.Xna.Framework.Content;  
    using Microsoft.Xna.Framework.GamerServices;  
    using Microsoft.Xna.Framework.Graphics;  
    using Microsoft.Xna.Framework.Input;  
    using Microsoft.Xna.Framework.Net;  
    using Microsoft.Xna.Framework.Storage;  
      
    namespace Calculus  
    {  
        public class LevelEditor  
        {  
            SpriteBatch spriteBatch;  
            List<Grid> grid = new List<Grid>();  
            int screenWidth = Game1.screenWidth;  
            int screenHeight = Game1.screenHeight;  
            ItemType chosentype = ItemType.Wall;  
            int xmax = Game1.screenWidth / 25;  
            int ymax = Game1.screenHeight / 25;  
            string[ line;  
            int x = 0;  
            int y = 0;  
      
            public LevelEditor(SpriteBatch spriteBatch)  
            {  
                this.spriteBatch = spriteBatch;  
                xmax = screenWidth / 25;  
                line = new string[ymax];  
            }  
      
            public void BuildGrid()  
            {  
                Grid tmp;  
                if (grid.Count == 0)  
                {  
                    for (int h = 0; h < screenHeight; h += 25)  
                    {  
                        for (int w = 0; w < screenWidth; w += 25)  
                        {  
                            tmp.rect = new Rectangle(w, h, 25, 25);  
                            tmp.x = w / 25;  
                            tmp.y = h / 25;  
                            tmp.imgrect = new Rectangle((int)ItemType.Grid * 25, 0, 25, 25);  
                            tmp.zindex = 0.0f;  
                            tmp.text = Game1.items;  
                            tmp.itemtype = ItemType.Grid;  
      
                            grid.Add(tmp);  
                        }  
                    }  
                }  
                else  
                    foreach (Grid g in grid)  
                    {  
                        spriteBatch.Draw(g.text, g.rect, g.imgrect, Color.White, 0, new Vector2(), SpriteEffects.None, g.zindex);  
                    }  
            }  
            int pl;  
            public void PlaceBlock()  
            {  
                int x = Game1.mouseState.X;  
                int y = Game1.mouseState.Y;  
      
                Rectangle rect = new Rectangle(x, y, 1, 1);  
      
                for (int i = 0; i < grid.Count; i++)  
                {  
                    if (grid[i].rect.Contains(new Point(x, y)))  
                    {  
                        if (chosentype == ItemType.Player)  
                        {  
                            if(pl != 1)  
                            {  
                                pl++;  
                                Grid tmp = new Grid();  
                                tmp.rect = new Rectangle(grid[i].x * 25, grid[i].y * 25, 25, 25);  
                                tmp.imgrect = new Rectangle((int)MoveDirection.Up * 25, 0, 25, 25);  
                                tmp.x = grid[i].x;  
                                tmp.y = grid[i].y;  
                                tmp.zindex = 0.5f;  
                                tmp.text = Game1.player;  
                                tmp.itemtype = ItemType.Player;  
                                Grid tmp2;  
                                tmp2 = grid[i];  
                                grid.Remove(tmp2);  
                                grid.Insert(i, tmp);  
                            }  
                        }  
                        else  
                        {  
                            Grid tmp = new Grid();  
                            tmp.rect = new Rectangle(grid[i].x * 25, grid[i].y * 25, 25, 25);  
                            tmp.imgrect = new Rectangle((int)chosentype * 25, 0, 25, 25);  
                            tmp.x = grid[i].x;  
                            tmp.y = grid[i].y;  
                            tmp.zindex = 0.5f;  
                            tmp.text = Game1.items;  
                            tmp.itemtype = chosentype;  
                            Grid tmp2;  
                            tmp2 = grid[i];  
                            if (tmp2.itemtype == ItemType.Player)  
                                pl--;  
                            grid.Remove(tmp2);  
                            grid.Insert(i, tmp);  
                        }  
                        break;  
                    }  
                }  
            }  
      
            public void DeleteBlock()  
            {  
                int x = Game1.mouseState.X;  
                int y = Game1.mouseState.Y;  
      
                Rectangle rect = new Rectangle(x, y, 1, 1);  
      
                for (int i = 0; i < grid.Count; i++)  
                {  
                    if (grid[i].rect.Contains(new Point(x, y)))  
                    {  
                        if (grid[i].itemtype == ItemType.Player)  
                            pl--;  
                        Grid tmp = new Grid();  
                        tmp.rect = new Rectangle(grid[i].x * 25, grid[i].y * 25, 25, 25);  
                        tmp.imgrect = new Rectangle((int)ItemType.Grid * 25, 0, 25, 25);  
                        tmp.x = grid[i].x;  
                        tmp.y = grid[i].y;  
                        tmp.zindex = 0.0f;  
                        tmp.text = Game1.items;  
                        tmp.itemtype = ItemType.Grid;  
                        Grid tmp2;  
                        tmp2 = grid[i];  
                        grid.Remove(tmp2);  
                        grid.Insert(i, tmp);  
                        break;  
                    }  
                }  
            }  
      
            public void Draw()  
            {  
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.None);  
                spriteBatch.Draw(Game1.mouseTexture, new Rectangle(Game1.mouseState.X, Game1.mouseState.Y, 13, 13), new Rectangle(0, 0, 13, 13), Color.White, 0, new Vector2(), SpriteEffects.None, 1.0f);  
                BuildGrid();  
      
                CheckCommands();  
      
                if(Game1.mouseState.LeftButton == ButtonState.Pressed)  
                    PlaceBlock();  
      
                if (Game1.mouseState.RightButton == ButtonState.Pressed)  
                    DeleteBlock();  
      
                if (Game1.keyboardState.IsKeyDown(Keys.H))  
                    DrawHelp();  
      
                spriteBatch.End();  
            }  
      
            public void CheckCommands()  
            {  
                if (Game1.keyboardState.IsKeyDown(Keys.W))  
                {  
                    chosentype = ItemType.Wall;  
                }  
                if (Game1.keyboardState.IsKeyDown(Keys.F))  
                {  
                    chosentype = ItemType.Floor;  
                }  
                if (Game1.keyboardState.IsKeyDown(Keys.C))  
                {  
                    chosentype = ItemType.Rock;  
                }  
                if (Game1.keyboardState.IsKeyDown(Keys.P))  
                {  
                    chosentype = ItemType.Player;  
                }  
                if (Game1.keyboardState.IsKeyDown(Keys.G))  
                {  
                    chosentype = ItemType.Goal;  
                }  
                if (Game1.keyboardState.IsKeyDown(Keys.Q))  
                {  
                    SaveToFile();  
                    Game1.createlevel = false;  
                }  
                  
            }  
      
            public void DrawHelp()  
            {  
                string helpmsg = "Commands (Press em buttons):\nLeft Click - Place Block\nRight Click - Delete Block\nP - Set Player Spawnpoint\nW - Select 'Wall' Block\nC - Select 'Crate' Block\nF - Select 'Floor' Block\nG - Select 'Goal' Block\nS - Save & Exit\nH - Show this...";  
                spriteBatch.DrawString(Game1.smallfont, helpmsg, new Vector2(  
                    screenWidth / 2 - (int)Game1.smallfont.MeasureString(helpmsg).X / 2,   
                    screenHeight / 2 - (int)Game1.smallfont.MeasureString(helpmsg).Y / 2), Color.White, 0, new Vector2(), 1, SpriteEffects.None, 1.0f);  
      
                spriteBatch.Draw(Game1.overlay, new Rectangle(  
                    screenWidth / 2 - (int)Game1.smallfont.MeasureString(helpmsg).X / 2,  
                    screenHeight / 2 - (int)Game1.smallfont.MeasureString(helpmsg).Y / 2,  
                    (int)Game1.smallfont.MeasureString(helpmsg).X, screenHeight / 2 - (int)Game1.smallfont.MeasureString(helpmsg).Y / 2), new Rectangle(0, 0, 1, 1), Color.White, 0, new Vector2(), SpriteEffects.None, 0.95f);  
            }  
      
            public void SaveToFile()  
            {  
                foreach (Grid g in grid)  
                {  
                    ConvertToText(g);  
                }  
      
                string mapname = string.Empty;  
                for (int i = 1; i < 999; i++)  
                {  
                    if (!File.Exists(BuildLevel.mapDirectory + i.ToString("000") + ".map"))  
                    {  
                        mapname = i.ToString("000") + ".map";  
                        break;  
                    }  
                }  
      
                using (StreamWriter sw = new StreamWriter(BuildLevel.mapDirectory + mapname))  
                {  
                    for (int i = 0; i < y; i++)  
                    {  
                        sw.WriteLine(line[i]);  
                    }  
                }  
            }  
      
            private void ConvertToText(Grid g)  
            {  
                x++;  
                switch (g.itemtype)  
                {  
                    case ItemType.Player:  
                        line[y] += "P";  
                        break;  
      
                    case ItemType.Grid:  
                        line[y] += "S";  
                        break;  
      
                    case ItemType.Wall:  
                        line[y] += "W";  
                        break;  
      
                    case ItemType.Floor:  
                        line[y] += "F";  
                        break;  
      
                    case ItemType.Goal:  
                        line[y] += "G";  
                        break;  
      
                    case ItemType.Rock:  
                        line[y] += "R";  
                        break;  
                }  
      
                if (x >= xmax)  
                {  
                    y++;  
                    x = 0;  
                }  
            }  
        }  
    }  

    Thanks in advance!
    /ReVarre

  • 05-31-2008 11:39 PM In reply to

    Re: Need help with a LevelEditor for a 2D game

    Sounds like what you need to do is first read the whole big block into an array or string and then run a loop that scans through the entire block in order to locate the minimum and maximum row as well as the minimum and maximum column which contains a W. So on each pass of the loop it would look at the current character, and, if it is a W, see if its row is either the smallest or largest row found so far to contain a W, and if so store that information. Do the same for the columns. Once you know your row and column bounds, you can rebuild the array or string and strip out all characters that are outside of those bounds.
  • 06-01-2008 12:12 AM In reply to

    Re: Need help with a LevelEditor for a 2D game

    If there is a way you could use a different letter than the s  the few times it appears and is needed in the grid then either try string.replace or if condition to ignore the s when doing something with it (such as saving to a file or array/list

     

    SSWPWWWWWW
    SSWFFFFFFW
    SSWFFFFFFW
    SSWFFFFFFW
    WWWFFRFFFW
    WGFFFRFFFW
    WGFFFFFFFW
    WWWWWWWWWW


    In this example only the top left 's' are required maybe change them.

  • 06-01-2008 1:33 AM In reply to

    Re: Need help with a LevelEditor for a 2D game

    Actually, I solved my problem. Thanks for all the replies!

     What I did was I got the closest wall x-wise and y-wise, also got the last wall x-wise, y-wise.

    Then I used line[i].Substring(firstx-wise, lastx-wise); and if(i < firsty-wise) line[i] = null.

     So again thanks for all the replies!
    /ReVarre

Page 1 of 1 (4 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG