Skip to content

Commit a306731

Browse files
committed
#22 Refactored Menu and Printing; reduced clutter on game launch
1 parent 75256df commit a306731

File tree

8 files changed

+134
-248
lines changed

8 files changed

+134
-248
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>junit</groupId>
3131
<artifactId>junit</artifactId>
32-
<version>4.13.1</version>
32+
<version>4.13</version>
3333
<scope>test</scope>
3434
</dependency>
3535
</dependencies>

src/com/redomar/game/HelpMenu.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
package com.redomar.game;
22

3+
import javax.imageio.ImageIO;
4+
import javax.swing.*;
35
import java.awt.*;
46
import java.awt.image.BufferedImage;
57
import java.io.IOException;
6-
import javax.imageio.ImageIO;
7-
import javax.swing.*;
8+
import java.net.URL;
9+
import java.util.concurrent.atomic.AtomicReference;
810

911
/**
1012
* Credit to Gagandeep Bali @ stackoverflow
1113
*/
1214
public class HelpMenu {
1315

14-
private MyPanel contentPane;
16+
private final JFrame frame = new JFrame("Help Menu");
17+
18+
private static void run() {
19+
Runnable runnable = () -> new HelpMenu().displayGUI();
20+
EventQueue.invokeLater(runnable);
21+
}
1522

1623
private void displayGUI() {
17-
JFrame frame = new JFrame("Help Menu");
1824
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
1925

20-
contentPane = new MyPanel();
26+
MyPanel contentPane = new MyPanel();
2127

2228
frame.setContentPane(contentPane);
2329
frame.setLocationRelativeTo(null);
@@ -26,21 +32,31 @@ private void displayGUI() {
2632
frame.setVisible(true);
2733
}
2834

29-
private class MyPanel extends JPanel {
35+
public void helpMenuLaunch() {
36+
run();
37+
}
38+
39+
public void helpMenuClose() {
40+
frame.setVisible(false);
41+
frame.dispose();
42+
}
43+
44+
private static class MyPanel extends JPanel {
3045

3146
private BufferedImage image;
3247

3348
private MyPanel() {
3449
try {
35-
image = ImageIO.read(MyPanel.class.getResource("/controls/controls.png"));
50+
AtomicReference<URL> controlsImageResource = new AtomicReference<>(MyPanel.class.getResource("/controls/controls.png"));
51+
image = ImageIO.read(controlsImageResource.get());
3652
} catch (IOException ioe) {
3753
ioe.printStackTrace();
3854
}
3955
}
4056

4157
@Override
4258
public Dimension getPreferredSize() {
43-
return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight());
59+
return image == null ? new Dimension(400, 300) : new Dimension(image.getWidth(), image.getHeight());
4460
}
4561

4662
@Override
@@ -49,9 +65,4 @@ protected void paintComponent(Graphics g) {
4965
g.drawImage(image, 0, 0, this);
5066
}
5167
}
52-
53-
public static void run() {
54-
Runnable runnable = () -> new HelpMenu().displayGUI();
55-
EventQueue.invokeLater(runnable);
56-
}
5768
}

src/com/redomar/game/InputHandler.java

Lines changed: 34 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,21 @@
1111

1212
public class InputHandler implements KeyListener {
1313

14-
private boolean isAzertyCountry;
15-
private Key up = new Key();
16-
private Key down = new Key();
17-
private Key left = new Key();
18-
private Key right = new Key();
19-
private Printing print = new Printing();
20-
private int map;
14+
private final boolean isAzertyCountry;
15+
private final Printing print = new Printing();
16+
private final PopUp popup = new PopUp();
17+
private final Key UP_KEY = new Key();
18+
private final Key DOWN_KEY = new Key();
19+
private final Key LEFT_KEY = new Key();
20+
private final Key RIGHT_KEY = new Key();
2121
private boolean ignoreInput = false;
2222
private boolean toggleMusic = false;
23-
private PopUp popup = new PopUp();
2423

2524
public InputHandler(Game game) {
2625
InputContext context = InputContext.getInstance();
2726
// Important to know whether the keyboard is in Azerty or Qwerty.
2827
// Azerty countries used QZSD instead of WASD keys.
29-
isAzertyCountry = context.getLocale().getCountry().equals("BE")
30-
|| context.getLocale().getCountry().equals("FR");
28+
isAzertyCountry = context.getLocale().getCountry().equals("BE") || context.getLocale().getCountry().equals("FR");
3129
game.addKeyListener(this);
3230
}
3331

@@ -45,33 +43,31 @@ public void keyTyped(KeyEvent e) {
4543

4644
private void toggleKey(int keyCode, boolean isPressed) {
4745
if (!isIgnoreInput()) {
48-
if (keyCode == KeyEvent.VK_Z && isAzertyCountry || keyCode == KeyEvent.VK_W && !isAzertyCountry
49-
|| keyCode == KeyEvent.VK_UP) {
50-
up.toggle(isPressed);
46+
if (keyCode == KeyEvent.VK_Z && isAzertyCountry || keyCode == KeyEvent.VK_W && !isAzertyCountry || keyCode == KeyEvent.VK_UP) {
47+
UP_KEY.toggle(isPressed);
5148
}
5249

53-
if (keyCode == KeyEvent.VK_Q && isAzertyCountry || keyCode == KeyEvent.VK_A && !isAzertyCountry
54-
|| keyCode == KeyEvent.VK_LEFT) {
55-
left.toggle(isPressed);
50+
if (keyCode == KeyEvent.VK_Q && isAzertyCountry || keyCode == KeyEvent.VK_A && !isAzertyCountry || keyCode == KeyEvent.VK_LEFT) {
51+
LEFT_KEY.toggle(isPressed);
5652
}
5753

5854
if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) {
59-
down.toggle(isPressed);
55+
DOWN_KEY.toggle(isPressed);
6056
}
6157

6258
if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
63-
right.toggle(isPressed);
59+
RIGHT_KEY.toggle(isPressed);
6460
}
6561
}
6662
if (isIgnoreInput()) {
67-
up.toggle(false);
68-
down.toggle(false);
69-
left.toggle(false);
70-
right.toggle(false);
63+
UP_KEY.toggle(false);
64+
DOWN_KEY.toggle(false);
65+
LEFT_KEY.toggle(false);
66+
RIGHT_KEY.toggle(false);
7167
}
7268

7369
if (keyCode == KeyEvent.VK_M) {
74-
if(!toggleMusic){
70+
if (!toggleMusic) {
7571
Game.getBackgroundMusic().play();
7672
print.print("Playing Music", PrintTypes.MUSIC);
7773
toggleMusic = true;
@@ -80,9 +76,8 @@ private void toggleKey(int keyCode, boolean isPressed) {
8076

8177
if (keyCode == KeyEvent.VK_COMMA) {
8278
Game.getBackgroundMusic().stop();
83-
if(toggleMusic)
84-
print.print("Stopping Music", PrintTypes.MUSIC);
85-
toggleMusic = false;
79+
if (toggleMusic) print.print("Stopping Music", PrintTypes.MUSIC);
80+
toggleMusic = false;
8681
}
8782

8883

@@ -137,14 +132,13 @@ private void toggleKey(int keyCode, boolean isPressed) {
137132

138133
private void quitGame() {
139134
Game.setClosing(true);
140-
print.removeLog();
135+
if (!print.removeLog()) System.err.println("Could not delete Log file");
141136
try {
142137
Thread.sleep(1000);
143138
} catch (InterruptedException e) {
144139
e.printStackTrace();
145140
}
146-
Game.getLevel().removeEntity(
147-
Game.getPlayer().getSanitisedUsername());
141+
Game.getLevel().removeEntity(Game.getPlayer().getSanitisedUsername());
148142
Game.setRunning(false);
149143
Game.getFrame().dispose();
150144
System.exit(0);
@@ -154,44 +148,20 @@ public void untoggle(boolean toggle) {
154148
this.ignoreInput = toggle;
155149
}
156150

157-
public int getMap() {
158-
return map;
151+
public Key getUP_KEY() {
152+
return UP_KEY;
159153
}
160154

161-
public void setMap(int map) {
162-
this.map = map;
155+
public Key getDOWN_KEY() {
156+
return DOWN_KEY;
163157
}
164158

165-
public Key getUp() {
166-
return up;
159+
public Key getLEFT_KEY() {
160+
return LEFT_KEY;
167161
}
168162

169-
public void setUp(Key up) {
170-
this.up = up;
171-
}
172-
173-
public Key getDown() {
174-
return down;
175-
}
176-
177-
public void setDown(Key down) {
178-
this.down = down;
179-
}
180-
181-
public Key getLeft() {
182-
return left;
183-
}
184-
185-
public void setLeft(Key left) {
186-
this.left = left;
187-
}
188-
189-
public Key getRight() {
190-
return right;
191-
}
192-
193-
public void setRight(Key right) {
194-
this.right = right;
163+
public Key getRIGHT_KEY() {
164+
return RIGHT_KEY;
195165
}
196166

197167
public boolean isIgnoreInput() {
@@ -202,10 +172,11 @@ private void setIgnoreInput(boolean ignoreInput) {
202172
this.ignoreInput = ignoreInput;
203173
}
204174

205-
public class Key {
175+
public static class Key {
206176
private int numTimesPressed = 0;
207177
private boolean pressed = false;
208178

179+
@Deprecated
209180
public int getNumTimesPressed() {
210181
return numTimesPressed;
211182
}
@@ -221,6 +192,7 @@ void toggle(boolean isPressed) {
221192
}
222193
}
223194

195+
@Deprecated
224196
public void off() {
225197
pressed = false;
226198
numTimesPressed = 0;

src/com/redomar/game/entities/Player.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ public void tick() {
5757
double ya = 0;
5858

5959
if (input != null) {
60-
if (input.getUp().isPressed() && input.isIgnoreInput() == false) {
60+
if (input.getUP_KEY().isPressed() && input.isIgnoreInput() == false) {
6161
ya -= speed;
6262
}
63-
if (input.getDown().isPressed() && input.isIgnoreInput() == false) {
63+
if (input.getDOWN_KEY().isPressed() && input.isIgnoreInput() == false) {
6464
ya += speed;
6565
}
66-
if (input.getLeft().isPressed() && input.isIgnoreInput() == false) {
66+
if (input.getLEFT_KEY().isPressed() && input.isIgnoreInput() == false) {
6767
xa -= speed;
6868
}
69-
if (input.getRight().isPressed() && input.isIgnoreInput() == false) {
69+
if (input.getRIGHT_KEY().isPressed() && input.isIgnoreInput() == false) {
7070
xa += speed;
7171
}
7272
}

0 commit comments

Comments
 (0)