在 XNA(4.0) 中定位文件

发布于 2024-12-08 09:58:59 字数 5902 浏览 1 评论 0原文

我目前正在 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 文件夹中获取文件,而无需使用静态字符串。然后通过字符串操作将其交给内容管理器以加载要加载的关卡(无论是开发人员还是玩家创建的)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2024-12-15 09:58:59

为什么不直接枚举级别目录呢?我错过了什么吗?也许是这样的:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文