在XNA中定位文件(4.0)

时间:2011-10-05 22:27:38

标签: file xna xna-4.0 file-location

我目前正在开发XNA 4.0中的游戏项目,我们目前通过引用.txt文件读取我们的级别(考虑到xml但.txt工作正常)我们能够将这些级别作为引用文件引用。如下:

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();
        }
    }

输出到文件(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>

但程序的行为就像文件中的所有内容一样:

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

即便如此

下一步是创建一个关卡编辑器/生成器,但在此之前我需要能够从Level文件夹中获取文件而不使用静态字符串。然后通过字符串操作手到内容管理器来获取要加载的级别(无论是开发人员还是玩家创建的)

1 个答案:

答案 0 :(得分:0)

为什么不只是枚举级别目录?我错过了什么吗?也许是这样的事情:

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;
}