-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameSettings.java
251 lines (210 loc) · 9.07 KB
/
GameSettings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.io.File;
import java.io.IOException;
import java.awt.Font;
import java.awt.Color;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.JComboBox;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.FloatControl;
public class GameSettings extends JFrame {
private Clip clip;
private int currentVolume = (int)(Planewar.volume * 100);
private String currentDifficulty = Planewar.Dfficulty;
private String currentWindowSize = Planewar.windowSize;
// Constructor to set up the GUI components
public GameSettings() {
setTitle("Game Settings");
setSize(400, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
// Set patterns
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
//better layout setting
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10); //things' distance
// custom styles
JLabel volumeLabel = new JLabel("Volume:");
volumeLabel.setFont(new Font("Arial", Font.BOLD, 14));
JSlider volumeSlider = new JSlider(0, 100, currentVolume);
volumeSlider.setMajorTickSpacing(10);
volumeSlider.setMinorTickSpacing(10); //let it be multiples of 10
volumeSlider.setPaintTicks(true);
volumeSlider.setPaintLabels(true);
volumeSlider.setSnapToTicks(true);
JLabel difficultyLabel = new JLabel("Difficulty:");
difficultyLabel.setFont(new Font("Arial", Font.BOLD, 14));
String[] difficulties = {"Easy", "Medium", "Hard", "Hell"};
JComboBox<String> difficultyComboBox = new JComboBox<>(difficulties);
difficultyComboBox.setFont(new Font("Arial", Font.PLAIN, 14));
difficultyComboBox.setSelectedItem(currentDifficulty);
JLabel windowSizeLabel = new JLabel("Window Size:");
windowSizeLabel.setFont(new Font("Arial", Font.BOLD, 14));
String[] windowSizes = {"600x600", "800x800", "1000x1000"};
JComboBox<String> windowSizeComboBox = new JComboBox<>(windowSizes);
windowSizeComboBox.setFont(new Font("Arial", Font.PLAIN, 14));
windowSizeComboBox.setSelectedItem(currentWindowSize);
JButton saveButton = new JButton("Save");
saveButton.setFont(new Font("Arial", Font.BOLD, 14));
saveButton.setBackground(Color.GREEN);
saveButton.setForeground(Color.WHITE);
JButton cancelButton = new JButton("Cancel");
cancelButton.setFont(new Font("Arial", Font.BOLD, 14));
cancelButton.setBackground(Color.RED);
cancelButton.setForeground(Color.WHITE);
// layout constraints
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(volumeLabel, gbc);
gbc.gridx = 1;
panel.add(volumeSlider, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(difficultyLabel, gbc);
gbc.gridx = 1;
panel.add(difficultyComboBox, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(windowSizeLabel, gbc);
gbc.gridx = 1;
panel.add(windowSizeComboBox, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
panel.add(saveButton, gbc);
gbc.gridx = 1;
panel.add(cancelButton, gbc);
// Add the panel to the frame
add(panel);
// Define button actions
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentVolume = volumeSlider.getValue();
String difficulty = (String) difficultyComboBox.getSelectedItem();
String windowSize = (String) windowSizeComboBox.getSelectedItem();
Planewar.volume = (float)currentVolume/100;
Planewar.Dfficulty = difficulty;
if(difficulty.equals("Easy")){
BossObj.life = 5;
BossObj.basiclife = 5;
Planewar.bossSpeed = 3;
Planewar.bulletSpeed = 3;
Planewar.enemySpeed = 3;
Planewar.shellSpeed = 8;
Planewar.bulletProductivity = 25;
Planewar.enemyProductivity = 25;
Planewar.shellProductivity = 10;
}
if(difficulty.equals("Midium")){
BossObj.life = 10;
BossObj.basiclife = 10;
Planewar.bossSpeed = 5;
Planewar.bulletSpeed = 5;
Planewar.enemySpeed = 5;
Planewar.shellSpeed = 5;
Planewar.bulletProductivity = 15;
Planewar.enemyProductivity = 15;
Planewar.shellProductivity = 15;
}
if(difficulty.equals("Hard")){
BossObj.life = 15;
BossObj.basiclife = 15;
Planewar.bossSpeed = 8;
Planewar.bulletSpeed = 8;
Planewar.enemySpeed = 8;
Planewar.shellSpeed = 4;
Planewar.bulletProductivity = 10;
Planewar.enemyProductivity = 10;
Planewar.shellProductivity = 20;
}
if(difficulty.equals("Hell")){
BossObj.life = 30;
BossObj.basiclife = 30;
Planewar.bossSpeed = 10;
Planewar.bulletSpeed = 10;
Planewar.enemySpeed = 10;
Planewar.shellSpeed = 5;
Planewar.bulletProductivity = 8;
Planewar.enemyProductivity = 5;
Planewar.shellProductivity = 25;
}
String[] dimensions = windowSize.split("x");
int width = Integer.parseInt(dimensions[0]);
int height = Integer.parseInt(dimensions[1]);
Planewar.width = width;
Planewar.height = height;
Planewar.windowSize = windowSize;
Planewar.mainFrame.setSize(width,height);
Planewar.mainFrame.repaint();
Planewar.reinitializeComponents(Planewar.mainFrame);
dispose();
Planewar.open = SoundUtil.playSoundWithVolume(GameUtil.rickMusic, true, Planewar.volume); //close open music
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Close the settings window without saving
dispose();
Planewar.open = SoundUtil.playSoundWithVolume(GameUtil.rickMusic, true, Planewar.volume); //close open music
}
});
// Initialize the clip and add a change listener to the slider
initClip(GameUtil.testSound);
volumeSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
currentVolume = volumeSlider.getValue();
adjustClipVolume(currentVolume);
playSound(); //remind sound for user
}
});
}
// initialize the clip
private void initClip(String soundFile) {
try {
File soundPath = new File(soundFile);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundPath);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// adjust clip volume
private void adjustClipVolume(int volume) {
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
float mid = (max - min) / 2.0f + min;
float range = max - min;
float gain = (volume - 50) * (range / 2) / 50 + mid;
volumeControl.setValue(gain);
}
// play a sound
private void playSound() {
if (clip != null) {
clip.setFramePosition(0); // Rewind to the beginning
clip.start();
}
}
}