3
3
import java .awt .event .KeyEvent ;
4
4
import java .util .prefs .BackingStoreException ;
5
5
import java .util .prefs .Preferences ;
6
-
7
6
import project_16x16 .ui .Notifications ;
8
7
9
8
/**
10
- * Handle loading/saving of player settings (game options)
9
+ * Handle loading/saving of player settings (game options).
11
10
*/
12
11
public class Options {
13
12
13
+ // preferences are stored in the Windows Registry
14
14
private static final Preferences options = Preferences .userNodeForPackage (Options .class );
15
15
16
+ /*
17
+ * TODO might be best to combine the key name (see current enum) with the
18
+ * default key value and the current/live value into one object. this way,
19
+ * string values in buttons can update live and one could call .save(), which
20
+ * would save the new mapping AND set the in-game mapping in one operation!
21
+ */
22
+
16
23
/**
17
- * Define the option as an enum, then create the variable.
24
+ * Defines all configurable options in the game
18
25
*/
19
26
public static enum Option {
20
- moveLeftKey , moveRightKey , jumpKey , dashKey , targetFPS , snapSize , debugMode , gainBGM , gainSFX , muteBGM , muteSFX , testKey ;
27
+ // @formatter:off
28
+ // Movement controls
29
+ MOVE_LEFT_KEY ,
30
+ MOVE_RIGHT_KEY ,
31
+ JUMP_KEY ,
32
+ DASH_KEY ,
33
+
34
+ // Debug and development controls
35
+ FRAME_RATE_LOW_KEY ,
36
+ FRAME_RATE_HIGH_KEY ,
37
+ FRAME_RATE_DEFAULT_KEY ,
38
+ TOGGLE_DEADZONE_KEY ,
39
+ TOGGLE_DEBUG_KEY ,
40
+
41
+ // Camera controls
42
+ CAMERA_TO_MOUSE_KEY ,
43
+ CAMERA_TO_PLAYER_KEY ,
44
+ CAMERA_SHAKE_KEY ,
45
+
46
+ // UI controls
47
+ NOTIFY_KEY ,
48
+ TOGGLE_FULLSCREEN_KEY ,
49
+
50
+ // Life system controls
51
+ LIFE_CAP_INCREASE_KEY ,
52
+ LIFE_CAP_DECREASE_KEY ,
53
+ LIFE_INCREASE_KEY ,
54
+ LIFE_DECREASE_KEY ,
55
+
56
+ // Other settings
57
+ TARGET_FPS ,
58
+ SNAP_SIZE ,
59
+ DEBUG_MODE ,
60
+ GAIN_BGM ,
61
+ GAIN_SFX ,
62
+ MUTE_BGM ,
63
+ MUTE_SFX ,
64
+
65
+ TEST_KEY ,
66
+ // @formatter:on
21
67
}
22
68
23
- public static int moveLeftKey = options .getInt (Option .moveLeftKey .toString (), KeyEvent .VK_A );
24
- public static int moveRightKey = options .getInt (Option .moveRightKey .toString (), KeyEvent .VK_D );
25
- public static int jumpKey = options .getInt (Option .jumpKey .toString (), KeyEvent .VK_SPACE );
26
- public static int dashKey = options .getInt (Option .dashKey .toString (), KeyEvent .VK_SHIFT );
27
-
28
- public static int targetFrameRate = options .getInt (Option .targetFPS .toString (), 60 );
29
- public static int snapSize = options .getInt (Option .snapSize .toString (), 32 );
30
- public static int debugMode = options .getInt (Option .debugMode .toString (), 2 );
31
-
32
- public static float gainBGM = options .getFloat (Option .gainBGM .toString (), 0 );
33
- public static float gainSFX = options .getFloat (Option .gainSFX .toString (), 0 );
34
- public static boolean muteBGM = options .getBoolean (Option .muteBGM .toString (), false );
35
- public static boolean muteSFX = options .getBoolean (Option .muteSFX .toString (), false );
36
-
37
- protected static final int frameRateLow = KeyEvent .VK_X ;
38
- protected static final int frameRateHigh = KeyEvent .VK_Z ;
39
- protected static final int frameRateDefault = KeyEvent .VK_N ;
40
- protected static final int toggleDeadzone = KeyEvent .VK_V ;
41
- protected static final int toggleDebug = KeyEvent .VK_TAB ;
42
- protected static final int cameraToMouse = KeyEvent .VK_C ;
43
- protected static final int cameraToPlayer = KeyEvent .VK_F ;
44
- protected static final int shake = KeyEvent .VK_G ;
45
- protected static final int notify = KeyEvent .VK_H ;
46
-
47
- public static final int lifeCapInc = KeyEvent .VK_P ;
48
- public static final int lifeCapDec = KeyEvent .VK_O ;
49
- public static final int lifeDec = KeyEvent .VK_K ;
50
- public static final int lifeInc = KeyEvent .VK_L ;
51
- public static final int fullscreen = KeyEvent .VK_F11 ;
52
-
53
- public static void save (Option Option , float value ) {
54
- options .putFloat (Option .toString (), value );
55
- try {
56
- options .flush ();
57
- } catch (BackingStoreException e ) {
58
- Notifications .addNotification ("Options Error" , "Could not flush user preferences to registry." );
59
- }
69
+ /**
70
+ * Defines default key mappings and their associated actions
71
+ */
72
+ public static class DefaultKeys {
73
+ // Movement controls
74
+ public static final int MOVE_LEFT = KeyEvent .VK_A ;
75
+ public static final int MOVE_RIGHT = KeyEvent .VK_D ;
76
+ public static final int JUMP = KeyEvent .VK_SPACE ;
77
+ public static final int DASH = KeyEvent .VK_SHIFT ;
78
+
79
+ // Debug and development controls
80
+ public static final int FRAME_RATE_LOW = KeyEvent .VK_X ;
81
+ public static final int FRAME_RATE_HIGH = KeyEvent .VK_Z ;
82
+ public static final int FRAME_RATE_DEFAULT = KeyEvent .VK_N ;
83
+ public static final int TOGGLE_DEADZONE = KeyEvent .VK_V ;
84
+ public static final int TOGGLE_DEBUG = KeyEvent .VK_TAB ;
85
+
86
+ // Camera controls
87
+ public static final int CAMERA_TO_MOUSE = KeyEvent .VK_C ;
88
+ public static final int CAMERA_TO_PLAYER = KeyEvent .VK_F ;
89
+ public static final int CAMERA_SHAKE = KeyEvent .VK_G ;
90
+
91
+ // UI controls
92
+ public static final int NOTIFY = KeyEvent .VK_H ;
93
+ public static final int TOGGLE_FULLSCREEN = KeyEvent .VK_F11 ;
94
+
95
+ // Life system controls
96
+ public static final int LIFE_CAP_INCREASE = KeyEvent .VK_P ;
97
+ public static final int LIFE_CAP_DECREASE = KeyEvent .VK_O ;
98
+ public static final int LIFE_INCREASE = KeyEvent .VK_L ;
99
+ public static final int LIFE_DECREASE = KeyEvent .VK_K ;
60
100
}
61
101
62
- public static void save (Option Option , int value ) {
63
- options .putInt (Option .toString (), value );
64
- try {
65
- options .flush ();
66
- } catch (BackingStoreException e ) {
67
- Notifications .addNotification ("Options Error" , "Could not flush user preferences to registry." );
68
- }
102
+ /**
103
+ * Game settings with their default values
104
+ */
105
+ // Movement key bindings
106
+ public static int moveLeftKey = options .getInt (Option .MOVE_LEFT_KEY .toString (), DefaultKeys .MOVE_LEFT );
107
+ public static int moveRightKey = options .getInt (Option .MOVE_RIGHT_KEY .toString (), DefaultKeys .MOVE_RIGHT );
108
+ public static int jumpKey = options .getInt (Option .JUMP_KEY .toString (), DefaultKeys .JUMP );
109
+ public static int dashKey = options .getInt (Option .DASH_KEY .toString (), DefaultKeys .DASH );
110
+
111
+ // Debug and development key bindings
112
+ public static int frameRateLowKey = options .getInt (Option .FRAME_RATE_LOW_KEY .toString (), DefaultKeys .FRAME_RATE_LOW );
113
+ public static int frameRateHighKey = options .getInt (Option .FRAME_RATE_HIGH_KEY .toString (), DefaultKeys .FRAME_RATE_HIGH );
114
+ public static int frameRateDefaultKey = options .getInt (Option .FRAME_RATE_DEFAULT_KEY .toString (),
115
+ DefaultKeys .FRAME_RATE_DEFAULT );
116
+ public static int toggleDeadzoneKey = options .getInt (Option .TOGGLE_DEADZONE_KEY .toString (), DefaultKeys .TOGGLE_DEADZONE );
117
+ public static int toggleDebugKey = options .getInt (Option .TOGGLE_DEBUG_KEY .toString (), DefaultKeys .TOGGLE_DEBUG );
118
+
119
+ // Camera key bindings
120
+ public static int cameraToMouseKey = options .getInt (Option .CAMERA_TO_MOUSE_KEY .toString (), DefaultKeys .CAMERA_TO_MOUSE );
121
+ public static int cameraToPlayerKey = options .getInt (Option .CAMERA_TO_PLAYER_KEY .toString (), DefaultKeys .CAMERA_TO_PLAYER );
122
+ public static int cameraShakeKey = options .getInt (Option .CAMERA_SHAKE_KEY .toString (), DefaultKeys .CAMERA_SHAKE );
123
+
124
+ // UI key bindings
125
+ public static int notifyKey = options .getInt (Option .NOTIFY_KEY .toString (), DefaultKeys .NOTIFY );
126
+ public static int toggleFullscreenKey = options .getInt (Option .TOGGLE_FULLSCREEN_KEY .toString (),
127
+ DefaultKeys .TOGGLE_FULLSCREEN );
128
+
129
+ // Life system key bindings
130
+ public static int lifeCapIncreaseKey = options .getInt (Option .LIFE_CAP_INCREASE_KEY .toString (), DefaultKeys .LIFE_CAP_INCREASE );
131
+ public static int lifeCapDecreaseKey = options .getInt (Option .LIFE_CAP_DECREASE_KEY .toString (), DefaultKeys .LIFE_CAP_DECREASE );
132
+ public static int lifeIncreaseKey = options .getInt (Option .LIFE_INCREASE_KEY .toString (), DefaultKeys .LIFE_INCREASE );
133
+ public static int lifeDecreaseKey = options .getInt (Option .LIFE_DECREASE_KEY .toString (), DefaultKeys .LIFE_DECREASE );
134
+
135
+ // Game settings
136
+ public static int targetFrameRate = options .getInt (Option .TARGET_FPS .toString (), 60 );
137
+ public static int snapSize = options .getInt (Option .SNAP_SIZE .toString (), 32 );
138
+ public static int debugMode = options .getInt (Option .DEBUG_MODE .toString (), 2 );
139
+
140
+ // Audio settings
141
+ public static float gainBGM = options .getFloat (Option .GAIN_BGM .toString (), 0 );
142
+ public static float gainSFX = options .getFloat (Option .GAIN_SFX .toString (), 0 );
143
+ public static boolean muteBGM = options .getBoolean (Option .MUTE_BGM .toString (), false );
144
+ public static boolean muteSFX = options .getBoolean (Option .MUTE_SFX .toString (), false );
145
+
146
+ /**
147
+ * Saves a float value to the preferences
148
+ */
149
+ public static void save (Option option , float value ) {
150
+ options .putFloat (option .toString (), value );
151
+ flushOptions ();
152
+ }
153
+
154
+ /**
155
+ * Saves an integer value to the preferences
156
+ */
157
+ public static void save (Option option , int value ) {
158
+ options .putInt (option .toString (), value );
159
+ flushOptions ();
160
+ }
161
+
162
+ /**
163
+ * Saves a boolean value to the preferences
164
+ */
165
+ public static void save (Option option , boolean value ) {
166
+ options .putBoolean (option .toString (), value );
167
+ flushOptions ();
69
168
}
70
169
71
- public static void save (Option Option , boolean value ) {
72
- options .putBoolean (Option .toString (), value );
170
+ /**
171
+ * Flushes changes to the system's preference store
172
+ */
173
+ private static void flushOptions () {
73
174
try {
74
175
options .flush ();
75
176
} catch (BackingStoreException e ) {
76
177
Notifications .addNotification ("Options Error" , "Could not flush user preferences to registry." );
77
178
}
78
179
}
79
-
80
180
}
0 commit comments