存储游戏范围内变量的最佳方式
本文关键字:最佳 方式 变量 游戏 范围内 存储 | 更新日期: 2023-09-27 18:12:47
我有一个选项屏幕,如难度,分辨率,全屏等,但我努力找到最好的方法来存储/获得这些变量在游戏时间。
我目前决定这样做的方式是创建一个包含所有GameOption枚举的"Constants"类。但是,如何为所有这些选项选择默认值,以及如何获得当前选择的枚举?
特别是分辨率-我决定存储值,但不确定如何获得默认或当前存储的值?
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;
}
}
任何方向都可以,谢谢:)
枚举就像类本身,它们不是变量。这里有一个更好的设置布局的方法。
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
之类的东西,所以用户的设置将在他们关闭游戏后被记住。
我使用 isolatedstoragessettings 来保存AppSettings:
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
//set value
settings.Add("Difficulty", "EASY");
//get value
object difficulty = settings["Difficulty"];
确定您可以使用枚举来获取密钥:Difficulty.EASY.ToString()