Skip to content

Commit 8afbab5

Browse files
committed
Small audio fix and more
1 parent 8fc2b4c commit 8afbab5

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

src/com/redomar/game/Game.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,24 @@ public void run() {
394394
* This method updates the logic of the game.
395395
*/
396396
public void tick() {
397+
boolean beforeMusic = musicPlaying;
397398
setTickCount(getTickCount() + 1);
398-
Either<Exception, Boolean> musicKeyAction = input.toggleActionWithCheckedRunnable(input.getM_KEY(), musicPlaying, () -> Game.getBackgroundMusic().play(), () -> Game.getBackgroundMusic().stop());
399+
Either<Exception, Boolean> musicKeyAction = input.toggleActionWithCheckedRunnable(
400+
input.getM_KEY(),
401+
musicPlaying,
402+
() -> Game.getBackgroundMusic().play(),
403+
() -> Game.getBackgroundMusic().stop()
404+
);
399405
musicKeyAction.either(exception -> {
400406
printer.cast().print("Failed to play music", PrintTypes.MUSIC);
401407
printer.exception(exception.toString());
402408
musicPlaying = false;
403-
}, isPlaying -> musicPlaying = isPlaying);
409+
}, isPlaying -> {
410+
musicPlaying = isPlaying;
411+
if (musicPlaying && !Game.getBackgroundMusic().getActive()) {
412+
input.overWriteKey(input.getM_KEY(), false);
413+
}
414+
});
404415

405416
level.tick();
406417
}
@@ -498,6 +509,7 @@ public void render() {
498509
*/
499510
private void status(Graphics2D g, boolean TerminalMode, boolean TerminalQuit) {
500511
if (TerminalMode) {
512+
new Night(g, screen).render(player.getPlayerAbsX(), player.getPlayerAbsY());
501513
// make the background transparent
502514
g.setColor(new Color(0, 0, 0, 100));
503515
g.fillRect(0, 0, 195, 165);
@@ -532,7 +544,7 @@ private void status(Graphics2D g, boolean TerminalMode, boolean TerminalQuit) {
532544
g.drawLine(0, getHeight()/2-8, getWidth(), getHeight()/2-8);
533545
g.setColor(Color.yellow);
534546
g.fillRect(player.getPlayerAbsX(), player.getPlayerAbsY(), 1, 1);
535-
new Night(g, screen).render(player.getPlayerAbsX(), player.getPlayerAbsY());
547+
536548

537549
}
538550
// If the game is shutting off

src/com/redomar/game/audio/AudioHandler.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ private void check(String path) {
4545
}
4646

4747
/**
48-
* Initialises an audio clip from the specified file path.
48+
* Initialises an audio clip by loading an audio file from the specified path. This method sets up the audio stream and prepares the clip for playback.
4949
*
50-
* @param path the file path of the audio clip
50+
* @param path the relative file path to the audio clip resource. The path must be accessible from the classpath and should not be null.
5151
*/
52-
private void initiate(String path) {
52+
private void initiate(@NotNull String path) {
5353
try {
5454
InputStream inputStream = new BufferedInputStream(Objects.requireNonNull(AudioHandler.class.getResourceAsStream(path)));
5555
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputStream);
@@ -59,6 +59,12 @@ private void initiate(String path) {
5959
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodeFormat, audioInputStream);
6060
clip = AudioSystem.getClip();
6161
clip.open(decodedAudioInputStream);
62+
clip.addLineListener(event -> {
63+
if (event.getType() == LineEvent.Type.STOP) {
64+
stop();
65+
}
66+
});
67+
6268
} catch (IOException e) {
6369
musicPrinter.cast().exception("Audio file not found " + path);
6470
musicPrinter.cast().exception(e.getMessage());
@@ -93,18 +99,19 @@ public void stop() {
9399
if (clip == null) throw new RuntimeException("Empty clip");
94100
if (clip.isRunning()) {
95101
clip.stop();
96-
clip.close();
102+
if (!music) clip.close();
97103
}
104+
if (music & active) musicPrinter.print("Stopping Music");
98105
} catch (Exception e) {
99106
musicPrinter.print("Audio Handler Clip not found");
100107
} finally {
101-
if (music) musicPrinter.print("Stopping Music");
102108
active = false;
103109
}
104110
}
105111

106112
public void close() {
107113
stop();
114+
clip.close();
108115
}
109116

110117
public boolean getActive() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public void tick() {
3535
double ya = 0;
3636

3737
// Calculate and set player's absolute X and Y positions
38-
setPlayerAbsX((int) ((getX() - Game.getScreen().getxOffset()) * 2) + 8);
39-
setPlayerAbsY((int) ((getY() - Game.getScreen().getyOffset()) * 2) + 7);
38+
setPlayerAbsX((((int) getX() - Game.getScreen().getxOffset()) * 2) + 8);
39+
setPlayerAbsY((((int) getY() - Game.getScreen().getyOffset()) * 2) + 7);
4040

4141

4242
if (inputHandler != null) {
4343

44-
speed = inputHandler.getSHIFTED().isPressed() ? 2 : 1;
44+
speed = inputHandler.getSHIFTED().isPressed() ? 2.5D : 1D;
4545

4646
if (inputHandler.getUP_KEY().isPressed()) {
4747
ya -= speed;

src/com/redomar/game/event/InputHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ private void toggleKey(int keyCode, boolean isPressed) {
133133

134134
}
135135

136+
public void overWriteKey(KeyHandler key, boolean isPressed) {
137+
key.setPressedToggle(isPressed);
138+
}
139+
136140
private void quitGame() {
137141
Game.setClosing(true);
138142
try {

0 commit comments

Comments
 (0)