playMoveSound added

This commit is contained in:
charles.duteil 2025-05-13 15:27:12 +02:00
parent c666978058
commit 1aaf4cd6d5
1 changed files with 41 additions and 0 deletions

View File

@ -1,4 +1,8 @@
package backend;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.util.ArrayList;
@ -22,12 +26,17 @@ private Integer selectedY = null;
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
/* ====== AUDIO STATE ====== */
private Clip moveClip;
private boolean soundEnabled = false;
public Board(int colNum, int lineNum) {
this.Width=colNum;
this.Height=lineNum;
this.pieces= new ArrayList <> ();
initMoveSound();
@ -199,6 +208,8 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
throw new IllegalArgumentException("Unknown piece type");
}
pieces.add(newPiece);
playMoveSound();
turnNumber++;
turnIsWhite = !turnIsWhite;
@ -329,4 +340,34 @@ public ArrayList<Piece> getAllPieces() {
public boolean isInBounds(int row, int col) {
return row >= 0 && row < 8 && col >= 0 && col < 8;
}
private void initMoveSound() {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(
Board.class.getResource("/sounds/move.wav"));
if (ais == null) { // resource not found
System.err.println("move.wav not on class-path!");
return;
}
moveClip = AudioSystem.getClip();
moveClip.open(ais);
soundEnabled = true; // <<< MISSING LINE
System.out.println("Move-sound loaded OK");
} catch (Exception e) {
e.printStackTrace();
soundEnabled = false;
}
}
private void playMoveSound() {
if (!soundEnabled || moveClip == null) return;
if (moveClip.isRunning()) {
moveClip.stop();
}
moveClip.setFramePosition(0);
moveClip.start();
}
}