12.4.2 管理游戏设置

你可能想在不同的计算机上设置不同的选项,例如分辨率、全屏模式和键盘映射等。这些设置可以从文件读取和存入文件,这样你就无需每次运行游戏时都重新进行设置。要实现这个功能,你需要创建一些结构存储游戏设置,一个辅助类帮助你存储和读取这些设置。游戏设置保存在XML文件中,XML格式容易阅读并且可以用文本编辑器修改。

在Helpers命名空间创建一个新的叫做SettingsManager的类,在这个类内部创建一个叫做KeyboardSettings的结构用来存储键盘映射。下面是KeyboardSettings结构的代码:

[Serializable]
public struct KeyboardSettings
{
    public Keys A; 
    public Keys B; 
    public Keys X; 
    public Keys Y; 
    public Keys LeftShoulder; 
    public Keys RightShoulder; 
    public Keys LeftTrigger; 
    public Keys RightTrigger; 
    public Keys LeftStick; 
    public Keys RightStick; 
    public Keys Back; 
    public Keys Start; 
    public Keys DPadDown; 
    public Keys DPadLeft;
    public Keys DPadRight; 
    public Keys DPadUp; 
    public Keys LeftThumbstickDown; 
    public Keys LeftThumbstickLeft; 
    public Keys LeftThumbstickRight; 
    public Keys LeftThumbstickUp;
    public Keys RightThumbstickDown; 
    public Keys RightThumbstickLeft; 
    public Keys RightThumbstickRight; 
    public Keys RightThumbstickUp; 
} 

在KeyboardSettings中,你为每个可以映射键盘的手柄按钮创建了一个属性。接下来,创建一个叫做GameSettings的结构,下面是GameSettings结构的代码:

[Serializable]
public struct GameSettings 
{
    public bool PreferredFullScreen; 
    public int PreferredWindowWidth; 
    public int PreferredWindowHeight; 
    public KeyboardSettings[] KeyboardSettings; 
} 

这个结构存储了分辨率,全屏模式和键盘设置的数组,这个数组用来将手柄按钮映射到键盘。最后,你还需在SettingsManager类中创建两个方法用来读取和保存游戏设置。因为你不需要SettingsManager类的实例,所以这个类和它所包含的方法都设置为静态的。下面是SettingsManager 类的Read方法的代码:

public static GameSettings Read(string settingsFilename)
{
    GameSettings gameSettings; 
    Stream stream = File.OpenRead(settingsFilename);
    XmlSerializer serializer =new XmlSerializer(typeof(GameSettings)); 
    gameSettings = (GameSettings)serializer.Deserialize(stream); 
    return gameSettings; 
} 

Read方法以要读取的设置文件作为参数,然后使用File类打开文件,使用XmlSerializer将XML文件转换为GameSettings类型的对象。你可以使用同样的方式将GameSettings数据保存到XML文件中。下面是SettingsManager类的Save方法的代码:

public static void Save(string settingsFilename, GameSettings gameSettings) 
{
    Stream stream = File.OpenWrite(settingsFilename); 
    XmlSerializer serializer = newXmlSerializer(typeof(GameSettings)); 
    serializer.Serialize(stream, gameSettings); 
} 

最后你还要创建一个方法将KeyboardSettings结构转换到一个dictionary中,这个字典将手柄按钮映射到键盘。因为InputHelper类不使用KeyboardSettings而是将手柄按钮映射到键盘,所以它需要这个dictionary。创建这个dictionary很简单:为每个手柄按钮添加一个项,映射到存储在KeyboardSettings结构的键上。下面是GetKeyboardDictionary的代码,用来将KeyboardSettings转换到dictionary中:

public static Dictionary<Buttons, Keys> GetKeyboardDictionary(KeyboardSettings keyboard)
{
    Dictionary<Buttons, Keys> dictionary =new Dictionary<Buttons, Keys>(); 
    dictionary.Add(Buttons.A, keyboard.A); dictionary.Add(Buttons.B, keyboard.B);
    dictionary.Add(Buttons.X, keyboard.X); dictionary.Add(Buttons.Y, keyboard.Y); 
    dictionary.Add(Buttons.LeftShoulder, keyboard.LeftShoulder); 
    dictionary.Add(Buttons.RightShoulder, keyboard.RightShoulder); 
    dictionary.Add(Buttons.LeftTrigger, keyboard.LeftTrigger); 
    dictionary.Add(Buttons.RightTrigger, keyboard.RightTrigger); 
    dictionary.Add(Buttons.LeftStick, keyboard.LeftStick); 
    dictionary.Add(Buttons.RightStick, keyboard.RightStick); 
    dictionary.Add(Buttons.Back, keyboard.Back); 
    dictionary.Add(Buttons.Start, keyboard.Start); 
    dictionary.Add(Buttons.DPadDown, keyboard.DPadDown); 
    dictionary.Add(Buttons.DPadLeft, keyboard.DPadLeft); 
    dictionary.Add(Buttons.DPadRight, keyboard.DPadRight); 
    dictionary.Add(Buttons.DPadUp, keyboard.DPadUp); 
    dictionary.Add(Buttons.LeftThumbstickDown,keyboard.LeftThumbstickDown); 
    dictionary.Add(Buttons.LeftThumbstickLeft,keyboard.LeftThumbstickLeft); 
    dictionary.Add(Buttons.LeftThumbstickRight,keyboard.LeftThumbstickRight); 
    dictionary.Add(Buttons.LeftThumbstickUp,keyboard.LeftThumbstickUp); 
    dictionary.Add(Buttons.RightThumbstickDown,keyboard.RightThumbstickDown); 
    dictionary.Add(Buttons.RightThumbstickLeft,keyboard.RightThumbstickLeft); 
    dictionary.Add(Buttons.RightThumbstickRight,keyboard.RightThumbstickRight); 
    dictionary.Add(Buttons.RightThumbstickUp, keyboard.RightThumbstickUp); 
    return dictionary; 
} 

发布时间:2009/5/31 下午4:16:58  阅读次数:4651

2006 - 2024,推荐分辨率 1024*768 以上,推荐浏览器 Chrome、Edge 等现代浏览器,截止 2021 年 12 月 5 日的访问次数:1872 万 9823 站长邮箱

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号