Game Development Reference
In-Depth Information
using
System.Collections.Generic;
1
2
3
class
GameSettingsManager
{
4
protected
Dictionary<
string
,
string
> stringSettings;
5
6
7
public
GameSettingsManager()
{
8
stringSettings =
new
Dictionary<
string
,
string
>();
9
}
10
11
12
public void
SetValue(
string
key,
string value
)
{
13
stringSettings[key] =
value
;
14
}
15
16
17
public string
GetValue(
string
key)
{
18
if
(stringSettings.ContainsKey(key))
19
return
stringSettings[key];
20
else
21
return
"";
22
}
23
}
24
Listing 20.2
A basic class for handling game settings
protected static
GameSettingsManager gameSettingsManager;
...
public static
GameSettingsManager GameSettingsManager
{
get
{
return
gameSettingsManager; }
}
We now add two methods to the manager for storing (
SetValue
) and for accessing
(
GetValue
) settings. The implementation of these methods is rather straightforward,
see Listing
20.2
for the complete class.
Updating the hint setting depending on the state of the on/off button is now very
easy. We simply add these lines of code to the
Update
method of the
PenguinPairs
class:
if
(onOffButton.On)
GameSettingsManager.SetValue("hints", "on");
else
GameSettingsManager.SetValue("hints", "off");