Skip to content

Commit 4245fc1

Browse files
committed
#22 Redesign Game class and Input Handler
1 parent 49a8da8 commit 4245fc1

File tree

19 files changed

+306
-397
lines changed

19 files changed

+306
-397
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ sourceSets{
2323
dependencies{
2424
implementation 'org.apache.commons:commons-text:+'
2525
implementation 'org.apache.commons:commons-lang3:3.+'
26+
implementation 'org.jetbrains:annotations:23.0.0'
2627
testImplementation 'junit:junit:4.13'
2728
implementation files('res/jars/JSplashScreen.jar')
2829
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
<version>3.+</version>
2727
<scope>runtime</scope>
2828
</dependency>
29+
<dependency>
30+
<groupId>org.jetbrains</groupId>
31+
<artifactId>annotations</artifactId>
32+
<version>23.0.0</version>
33+
<scope>runtime</scope>
34+
</dependency>
2935
<dependency>
3036
<groupId>junit</groupId>
3137
<artifactId>junit</artifactId>

src/com/redomar/game/Game.java

Lines changed: 95 additions & 177 deletions
Large diffs are not rendered by default.

src/com/redomar/game/InputHandler.java

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

33
import com.redomar.game.lib.SleepThread;
4-
import com.redomar.game.script.PopUp;
54
import com.redomar.game.script.PrintTypes;
6-
import com.redomar.game.script.Printing;
5+
import com.redomar.game.script.Printer;
76

87
import java.awt.event.KeyEvent;
98
import java.awt.event.KeyListener;
109
import java.awt.im.InputContext;
10+
import java.util.HashMap;
11+
import java.util.Map;
1112

1213
public class InputHandler implements KeyListener {
1314

14-
private final boolean isAzertyCountry;
15-
private final Printing print = new Printing();
16-
private final PopUp popup = new PopUp();
15+
private final boolean frenchKeyboardLayout;
16+
private final Printer inputPrinter = new Printer(PrintTypes.INPUT);
1717
private final Key UP_KEY = new Key();
1818
private final Key DOWN_KEY = new Key();
1919
private final Key LEFT_KEY = new Key();
2020
private final Key RIGHT_KEY = new Key();
21-
private boolean ignoreInput = false;
22-
private boolean toggleMusic = false;
21+
private boolean musicPlaying = false;
22+
2323

2424
public InputHandler(Game game) {
2525
InputContext context = InputContext.getInstance();
26-
// Important to know whether the keyboard is in Azerty or Qwerty.
27-
// Azerty countries used QZSD instead of WASD keys.
28-
isAzertyCountry = context.getLocale().getCountry().equals("BE") || context.getLocale().getCountry().equals("FR");
26+
frenchKeyboardLayout = context.getLocale().getCountry().equals("BE") || context.getLocale().getCountry().equals("FR");
2927
game.addKeyListener(this);
3028
}
3129

@@ -34,120 +32,94 @@ public void keyPressed(KeyEvent e) {
3432
}
3533

3634
public void keyReleased(KeyEvent e) {
37-
toggleKey(e.getKeyCode(), false);
38-
}
39-
40-
public void keyTyped(KeyEvent e) {
41-
42-
}
43-
44-
private void toggleKey(int keyCode, boolean isPressed) {
45-
if (!isIgnoreInput()) {
46-
if (keyCode == KeyEvent.VK_Z && isAzertyCountry || keyCode == KeyEvent.VK_W && !isAzertyCountry || keyCode == KeyEvent.VK_UP) {
47-
UP_KEY.toggle(isPressed);
48-
}
49-
50-
if (keyCode == KeyEvent.VK_Q && isAzertyCountry || keyCode == KeyEvent.VK_A && !isAzertyCountry || keyCode == KeyEvent.VK_LEFT) {
51-
LEFT_KEY.toggle(isPressed);
52-
}
53-
54-
if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) {
55-
DOWN_KEY.toggle(isPressed);
56-
}
57-
58-
if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
59-
RIGHT_KEY.toggle(isPressed);
35+
int keyCode = e.getKeyCode();
36+
toggleKey(keyCode, false);
37+
if (keyCode == KeyEvent.VK_BACK_QUOTE) {
38+
if (!Game.isClosing()) {
39+
Game.setDevMode(!Game.isDevMode());
40+
new Thread(new SleepThread());
41+
inputPrinter.print(String.format("Debug Mode %s", Game.isDevMode() ? "Enabled" : "Disabled"));
6042
}
6143
}
62-
if (isIgnoreInput()) {
63-
UP_KEY.toggle(false);
64-
DOWN_KEY.toggle(false);
65-
LEFT_KEY.toggle(false);
66-
RIGHT_KEY.toggle(false);
67-
}
6844

6945
if (keyCode == KeyEvent.VK_M) {
70-
if (!toggleMusic) {
46+
if (!musicPlaying) {
7147
Game.getBackgroundMusic().play();
72-
print.print("Playing Music", PrintTypes.MUSIC);
73-
toggleMusic = true;
48+
musicPlaying = true;
49+
} else {
50+
Game.getBackgroundMusic().stop();
51+
musicPlaying = false;
52+
}
53+
}
54+
55+
if (keyCode == KeyEvent.VK_N) {
56+
if (!Game.isNpc()) {
57+
Game.setNpc(true);
58+
Game.npcSpawn();
59+
inputPrinter.print("Dummy has been spawned", PrintTypes.GAME);
7460
}
7561
}
62+
}
7663

77-
if (keyCode == KeyEvent.VK_COMMA) {
78-
Game.getBackgroundMusic().stop();
79-
if (toggleMusic) print.print("Stopping Music", PrintTypes.MUSIC);
80-
toggleMusic = false;
64+
public void keyTyped(KeyEvent e) {
65+
66+
}
67+
68+
private void toggleKey(int keyCode, boolean isPressed) {
69+
Map<Integer, Runnable> keyCodeActions = new HashMap<>();
70+
71+
keyCodeActions.put(KeyEvent.VK_S, () -> DOWN_KEY.toggle(isPressed));
72+
keyCodeActions.put(KeyEvent.VK_D, () -> RIGHT_KEY.toggle(isPressed));
73+
keyCodeActions.put(KeyEvent.VK_UP, () -> UP_KEY.toggle(isPressed));
74+
keyCodeActions.put(KeyEvent.VK_LEFT, () -> LEFT_KEY.toggle(isPressed));
75+
keyCodeActions.put(KeyEvent.VK_DOWN, () -> DOWN_KEY.toggle(isPressed));
76+
keyCodeActions.put(KeyEvent.VK_RIGHT, () -> RIGHT_KEY.toggle(isPressed));
77+
78+
if (frenchKeyboardLayout) {
79+
keyCodeActions.put(KeyEvent.VK_Q, () -> LEFT_KEY.toggle(isPressed));
80+
keyCodeActions.put(KeyEvent.VK_Z, () -> UP_KEY.toggle(isPressed));
81+
keyCodeActions.put(KeyEvent.VK_A, this::quitGame);
82+
} else {
83+
keyCodeActions.put(KeyEvent.VK_A, () -> LEFT_KEY.toggle(isPressed));
84+
keyCodeActions.put(KeyEvent.VK_W, () -> UP_KEY.toggle(isPressed));
85+
keyCodeActions.put(KeyEvent.VK_Q, this::quitGame);
8186
}
8287

88+
if (keyCodeActions.containsKey(keyCode)) keyCodeActions.get(keyCode).run();
8389

84-
if (keyCode == KeyEvent.VK_W && isAzertyCountry || keyCode == KeyEvent.VK_Z && !isAzertyCountry) {
85-
// if (map == 0){
86-
// Game.getGame().setMap("/levels/water_level.png");
87-
// map++;
88-
// } else{
89-
// Game.getGame().setMap("/levels/custom_level.png");
90-
// map--;
91-
// }
90+
if (keyCode == KeyEvent.VK_W && frenchKeyboardLayout || keyCode == KeyEvent.VK_Z && !frenchKeyboardLayout) {
9291
if (Game.getMap() == 2) {
9392
if (Game.isNpc()) {
9493
Game.setNpc(false);
9594
}
9695
Game.setChangeLevel(true);
9796
}
9897
}
99-
if (keyCode == KeyEvent.VK_N) {
100-
if (Game.getPlayer().isMoving()) {
101-
setIgnoreInput(true);
102-
int n = popup.Warn("Stop moving before spawning dummy AI");
103-
if (n == 0) {
104-
setIgnoreInput(false);
105-
}
106-
return;
107-
}
108-
if (!Game.isNpc()) {
109-
Game.setNpc(true);
110-
Game.npcSpawn();
111-
print.print("Dummy has been spawned", PrintTypes.GAME);
112-
}
113-
}
98+
11499
if (keyCode == KeyEvent.VK_K) {
115100
if (Game.isNpc()) {
116101
Game.setNpc(false);
117102
Game.npcKill();
118-
print.print("Dummy has been removed", PrintTypes.GAME);
103+
inputPrinter.print("Dummy has been removed", PrintTypes.GAME);
119104
}
120105
}
121106

122-
if (keyCode == KeyEvent.VK_A && isAzertyCountry || keyCode == KeyEvent.VK_Q && !isAzertyCountry)
123-
this.quitGame();
124-
125-
if (keyCode == KeyEvent.VK_BACK_QUOTE) {
126-
if (!Game.isClosing() && !Game.isDevMode()) {
127-
Game.setDevMode(true);
128-
new Thread(new SleepThread());
129-
}
130-
}
131107
}
132108

133109
private void quitGame() {
134110
Game.setClosing(true);
135-
if (!print.removeLog()) System.err.println("Could not delete Log file");
111+
if (!inputPrinter.removeLog()) System.err.println("Could not delete Log file");
136112
try {
137113
Thread.sleep(1000);
138114
} catch (InterruptedException e) {
139115
e.printStackTrace();
140116
}
141-
Game.getLevel().removeEntity(Game.getPlayer().getSanitisedUsername());
142-
Game.setRunning(false);
117+
Game.getLevel().removeEntity(Game.getPlayer().getName());
118+
Game.getGame().stop();
143119
Game.getFrame().dispose();
144120
System.exit(0);
145121
}
146122

147-
public void untoggle(boolean toggle) {
148-
this.ignoreInput = toggle;
149-
}
150-
151123
public Key getUP_KEY() {
152124
return UP_KEY;
153125
}
@@ -164,14 +136,6 @@ public Key getRIGHT_KEY() {
164136
return RIGHT_KEY;
165137
}
166138

167-
public boolean isIgnoreInput() {
168-
return ignoreInput;
169-
}
170-
171-
private void setIgnoreInput(boolean ignoreInput) {
172-
this.ignoreInput = ignoreInput;
173-
}
174-
175139
public static class Key {
176140
private int numTimesPressed = 0;
177141
private boolean pressed = false;

src/com/redomar/game/UML.ucls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@
442442
<operations public="true" package="true" protected="true" private="true" static="true"/>
443443
</display>
444444
</class>
445-
<class id="50" language="java" name="com.redomar.game.script.Printing" project="JavaGame"
445+
<class id="50" language="java" name="com.redomar.game.script.Printer" project="JavaGame"
446446
file="/JavaGame/src/com/redomar/game/script/Printing.java" binary="false" corner="BOTTOM_RIGHT">
447447
<position height="207" width="196" x="2158" y="3609"/>
448448
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,92 @@
11
package com.redomar.game.audio;
22

33
import com.redomar.game.script.PrintTypes;
4-
import com.redomar.game.script.Printing;
4+
import com.redomar.game.script.Printer;
5+
import org.jetbrains.annotations.NotNull;
56

67
import javax.sound.sampled.*;
78
import java.io.File;
89
import java.util.Arrays;
10+
import java.util.Objects;
911

1012

1113
public class AudioHandler {
1214

15+
private final Printer musicPrinter = new Printer(PrintTypes.MUSIC);
1316
private Clip clip;
1417
private boolean active = false;
15-
private final Printing p = new Printing();
18+
private boolean music = false;
1619

17-
public AudioHandler(String path){
20+
public AudioHandler(String path) {
1821
check(path);
1922
}
2023

21-
public AudioHandler(File file){
24+
public AudioHandler(@NotNull File file) {
2225
check(file.toString());
2326
}
2427

25-
private void check(String path){
28+
public AudioHandler(String path, boolean music) {
29+
this.music = music;
30+
check(path);
31+
}
32+
33+
private void check(String path) {
2634
try {
27-
if(!path.equals("")){
35+
if (!path.equals("")) {
2836
initiate(path);
2937
} else {
3038
throw new NullPointerException();
3139
}
32-
} catch (NullPointerException e){
33-
p.print("Destination Cannot be empty", PrintTypes.ERROR);
40+
} catch (NullPointerException e) {
41+
musicPrinter.print("Destination Cannot be empty", PrintTypes.ERROR);
3442
throw e;
3543
}
3644
}
3745

38-
private void initiate(String path){
39-
try{
40-
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(AudioHandler.class.getResourceAsStream(path));
46+
private void initiate(String path) {
47+
try {
48+
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Objects.requireNonNull(AudioHandler.class.getResourceAsStream(path)));
4149
AudioFormat baseFormat = audioInputStream.getFormat();
42-
AudioFormat decodeFormat = new AudioFormat(
43-
AudioFormat.Encoding.PCM_SIGNED,
44-
baseFormat.getSampleRate(), 16,
45-
baseFormat.getChannels(),
46-
baseFormat.getChannels() * 2,
47-
baseFormat.getSampleRate(),
48-
false
49-
);
50-
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(
51-
decodeFormat, audioInputStream);
50+
AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
51+
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodeFormat, audioInputStream);
5252
clip = AudioSystem.getClip();
5353
clip.open(decodedAudioInputStream);
54-
} catch (Exception e){
54+
} catch (Exception e) {
5555
System.err.println(Arrays.toString(e.getStackTrace()));
56-
p.print("Audio Failed to initiate", PrintTypes.ERROR);
56+
musicPrinter.print("Audio Failed to initiate", PrintTypes.ERROR);
5757
}
5858
}
5959

60-
public void play(){
61-
try{
62-
if(clip == null) return;
63-
stop();
60+
public void play() {
61+
try {
62+
if (clip == null) return;
6463
clip.setFramePosition(0);
6564
clip.start();
65+
if (music) musicPrinter.print("Playing Music");
6666
active = true;
6767
} catch (Exception e) {
68-
p.print("Audio Failed to play", PrintTypes.ERROR);
68+
musicPrinter.print("Audio Failed to play", PrintTypes.ERROR);
6969
throw e;
7070
}
7171
}
7272

73-
public void setVolume(float velocity){
73+
public void setVolume(float velocity) throws NullPointerException {
7474
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
7575
volume.setValue(velocity);
76-
7776
}
7877

7978
public void stop() {
8079
if (clip.isRunning()) clip.stop();
80+
if (music) musicPrinter.print("Stopping Music");
8181
active = false;
8282
}
8383

84-
public void close(){
84+
public void close() {
8585
stop();
8686
clip.close();
8787
}
8888

89-
public boolean getActive(){
89+
public boolean getActive() {
9090
return this.active;
9191
}
92-
9392
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.redomar.game.gfx.Screen;
44
import com.redomar.game.level.LevelHandler;
5+
import com.redomar.game.script.PrintTypes;
6+
import com.redomar.game.script.Printer;
57

68
public abstract class Entity {
79

810
protected double x, y;
911
protected String name;
1012
protected LevelHandler level;
13+
protected Printer entityPrinter = new Printer(PrintTypes.ENTITY);
1114

1215
public Entity(LevelHandler level) {
1316
init(level);

0 commit comments

Comments
 (0)