存储游戏范围变量的最佳方式

时间:2014-07-13 17:38:16

标签: c# enums 2d-games

我有一个选项屏幕,例如难度,分辨率,全屏等等,但我很难找到在游戏时间存储/获取这些变量的最佳方法。

我目前决定这样做的方法是创建一个' Constants'包含所有GameOption枚举的类。但是如何为所有这些选项选择默认值,以及如何获取当前选择的枚举?

特别是在分辨率的情况下 - 我决定存储值但不确定如何获取默认值或当前存储的值?

namespace V1.test.RPG
{
    public class GameOptions
    {
        public enum Difficulty
        {
            EASY,
            MEDIUM,
            HARD
        }

        public enum Sound
        {
            ON,
            QUIET,
            OFF
        }

        public enum Music
        {
            ON,
            QUIET,
            OFF
        }

        public enum ResolutionWidth
        {
            SMALL = 1280,
            MEDIUM = 1366,
            LARGE = 1920,
            WIDESCREEN = 2560
        }

        public enum ResolutionHeight
        {
            SMALL = 800,
            MEDIUM = 768,
            LARGE = 1080,
            WIDESCREEN = 1080
        }

        public Boolean fullScreen = false;
    }
}

任何方向都会很棒, 谢谢:))

2 个答案:

答案 0 :(得分:7)

Enums就像类本身一样,它们不是变量。这是设置布局的更好方法。

namespace V1.test.RPG
{
    public class GameOptions
    {
        public GameOptions()
        {
             FullScreen = false;
             DifficultyLevel = Difficulty.Normal;
             SoundSetting = Sound.On;
             // And so on.
        }

        public Boolean FullScreen { get; set; }
        public Difficulty DifficultyLevel { get; set; }
        public Sound SoundSetting { get; set; }
        //And so on...
    }

    public enum Difficulty 
    {
        EASY,
        MEDIUM,
        HARD
    }

    public enum Sound 
    {
        ON,
        QUIET,
        OFF
    }

    public enum Music
    {
        ON,
        QUIET,
        OFF
    }

    public enum ResolutionWidth
    {
        SMALL = 1280,
        MEDIUM = 1366,
        LARGE = 1920,
        WIDESCREEN = 2560
    }

    public enum ResolutionHeight
    {
        SMALL = 800,
        MEDIUM = 768,
        LARGE = 1080,
        WIDESCREEN = 1080
    }

}

您现在可以在GameOptions类的构造函数中设置所有默认值,然后只需将变量的副本传递给需要读取选项的类,它只需读取所需的设置。

public class GameEngine
{
    private readonly GameOptions _options;
    public GameEngine()
    {
        _options = new GameOptions();    
    }

    public void ShowSetupScreen()
    {
        //If the setup screen modifies the values inside the _gameOptions variable 
        // by passing the variable in it removes the need for "Global variables"
        using(var setupScreen = new SetupScreen(_gameOptions);
        {
            setupScreen.Show();
        }
    }

    public void Enemy CreateEnemy()
    {
        //Creates a new enemy and passes the options in so it can read the settings
        // like the difficulty level.
        return new Enemy(_gameOptions);
    }

    //And so on.

通过使用实例变量而不是静态全局变量,可以使用XmlSerializer之类的内容更轻松地保存和加载设置,因此用户设置将在关闭游戏后被记住。

答案 1 :(得分:1)

我正在使用 IsolatedStorageSettings 来保存AppSettings:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

//set value
settings.Add("Difficulty", "EASY");

//get value
object difficulty = settings["Difficulty"];

确保您可以使用枚举来获取密钥:Difficulty.EASY.ToString()