开发者

locating files in XNA(4.0)

I am currently working on a game project in XNA 4.0 we currently read our levels in by referencing .txt files (considered xml but .txt works fine) we are able to reference the levels as referenced files. as follows:

LevelScreen.cs:

    private void LoadLevel()
    {
        string levelPath;
        // possible case switch or counter for multiple levels

        string level1Path;
        level1Path = "GameContent\\levels\\level1.txt";
        string level2Path;
        level2Path = "GameContent\\Levels\\level2.txt";
        string level3Path;
        level3Path = "GameContent\\Levels\\level3.txt";

        //Loops to find levels
        while (true)
        {
            //finds level files using game location
            //levelPath = "Levels/level1.txt";
            //levelPath = Path.Combine(FullName, "Content/" + levelPath);
            //Will be fixed to load from wherever the games file is located to find the level files.

            //gets path of executable
            levelPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

            if (levelPath.EndsWith("Game\\bin\\x86\\Debug\\Game.exe"))
            {
                //deletes end of path to set filepath to project folder
                levelPath = levelPath.Remove(levelPath.Length - 43);
                //increments level everytime it is loaded
                levelNum++;
                //keeps level within first and last
                if (levelNum > lastLevel)
                    levelNum = 1;
                //adds filepath for level
                switch (levelNum)
                {
                    case 1:
                        levelPath = string.Concat(levelPath, level1Path);
                        break;
                    case 2:
                        levelPath = string.Concat(levelPath, level2Path);
                        break;
                    case 3:
                        levelPath = string.Concat(levelPath, level3Path);
                        break;
                 //HERE we can put in a case statement to load other levels.
                   default:  // currently no action (?win screen?)
                        break;
                }
            }

            if (File.Exists(levelPath))
                break;
        }

MenuScreen.cs:

        private void GenerateLevelSelectMenu()
        {
        List<string> Levels = new List<string>();
        string directory = "Content/Levels";

        //get list of files in levelsFolder
        foreach (string file in Directory.GetFiles(directory))
        {
            Levels.Add(file);
        }

        //generate XML file.
        string targetDirectory = "Content/Menus/LevelSelect.xml";
        using (StreamWriter writer = new StreamWriter(targetDirectory, false))
        {
            //needed to be read as xml
            writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");

            //writing xml
            writer.WriteLine("<Menu>");
            writer.WriteLine("  <MenuName>Level Select</MenuName>");
            //stepping through the list of Levels to generate the data
            for (int ii = 0; ii < Levels.Count(); ii++)
            {
                writer.WriteLine("  <MenuItem>");
                writer.WriteLine("    <MenuItemText>" + Levels[ii] + "</MenuItemEvent>");
                writer.WriteLine("    <MenuItemEvent>" + Levels[ii] + "</MenuItemEvent>");
                writer.WriteLine("    <EventParams>Option" + ii + "</EventParams>");
                writer.WriteLine("  </MenuItem>");
            }
            //needed to go to the previous menu.
            writer.WriteLine("  <MenuItem>");
            writer.WriteLine("    <MenuItemText>Back</MenuItemEvent>");
            writer.WriteLine("    <MenuItemEvent>BackEvent</MenuItemEvent>");
            writer.WriteLine("    <EventParams>OptionBack</EventParams>");
            writer.WriteLine("  </MenuItem>");
            //placement of the menu itself
            writer.WriteLine("  <PositionX>427</PositionX>");
            writer.WriteLine("  <PositionY>240</PositionY>");
            writer.WriteLine("  <SelectedItemNum>0</SelectedItemNum>");
            writer.WriteLine("</Menu>");
            writer.Close();
        }
    }

Output to file(LevelSelect.xml):

<?xml version="1.0" encoding="utf-8" ?>
<Menu>
  <MenuName>Level Select</MenuName>
  <MenuItem>
    <MenuItemText>Content/Levels\level1.txt</MenuItemEvent>
    <MenuItemEvent>Content/Levels\level1.txt</MenuItemEvent>
    <EventParams>Option0</EventParams>
  </MenuItem>
  <MenuItem>
    <MenuItemText>Content/Levels\level2.txt</MenuItemEvent>
    <MenuItemEvent>Content/Levels\level2.txt</MenuItemEvent>
    <EventParams>Option1</EventParams>
  </MenuItem>
  <MenuItem>
    <MenuItemText>Content/Levels\level3.txt</MenuItemEvent>
    <MenuItemEvent>Content/Levels\level3.txt</MenuItemEvent>
    <EventParams>Option2</EventParams>
  </MenuItem>
  <MenuItem>
    <MenuItemText>Back</MenuItemEvent>
    <MenuItemEvent>BackEvent</MenuItemEvent>
    <EventParams>OptionBack</EventParams>
  </MenuItem>
  <PositionX>427</PositionX>
  <PositionY>240</PositionY>
  <SelectedItemNum>0</SelectedItemNum>
</Menu>

but the program is acting like all that is in the file is this:

<?xml version="1.0" encoding="utf-8" ?>
<Menu>
  <MenuName>Level Select</MenuName>
</Menu>

if even that

the next step is to create a level editor/generator, but before that I need to be able to get the files from the Level folder without using static strings. then through string manipulation hand that to the content manager to get the level to be loaded (whether 开发者_如何学运维it is developer or player created)


Why not just enumerate the level directory? Am I missing something? Perhaps something like:

static void CheckLevels(string directory) {
    List<string> levels = new List<string>();

    foreach (string file in Directory.GetFiles(directory, ".txt")) { // You could change ".txt" to some other file extension. I always think it's cool for my games to use special extensions =)
        levels.Add(file);
    }

    return levels;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜