Refactor: rewrite lamdba execution to more elegant version
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -5,14 +5,14 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javafx.animation.PauseTransition;
|
import javafx.animation.PauseTransition;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
import javafx.scene.media.AudioClip;
|
import javafx.scene.media.AudioClip;
|
||||||
import javafx.util.Duration;
|
import javafx.util.Duration;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages sound effects for the Casono game. Provides methods to play sounds
|
* Manages sound effects for the Casono game. Provides methods to play sounds for card reveals and
|
||||||
* for card reveals and
|
|
||||||
* button clicks.
|
* button clicks.
|
||||||
*/
|
*/
|
||||||
public class SoundManager {
|
public class SoundManager {
|
||||||
@@ -25,8 +25,7 @@ public class SoundManager {
|
|||||||
private float volume = DEFAULT_VOLUME;
|
private float volume = DEFAULT_VOLUME;
|
||||||
private static final long CARD_REVEAL_STAGGER_MS = 120; // Delay between card reveal sounds
|
private static final long CARD_REVEAL_STAGGER_MS = 120; // Delay between card reveal sounds
|
||||||
|
|
||||||
private SoundManager() {
|
private SoundManager() {}
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the singleton instance of SoundManager. */
|
/** Returns the singleton instance of SoundManager. */
|
||||||
public static SoundManager getInstance() {
|
public static SoundManager getInstance() {
|
||||||
@@ -34,8 +33,7 @@ public class SoundManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pre-loads critical sounds into cache to avoid delays on first play. Should be
|
* Pre-loads critical sounds into cache to avoid delays on first play. Should be called at
|
||||||
* called at
|
|
||||||
* application startup.
|
* application startup.
|
||||||
*/
|
*/
|
||||||
public void preloadSounds() {
|
public void preloadSounds() {
|
||||||
@@ -58,15 +56,11 @@ public class SoundManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays multiple card reveal sounds in sequence with slight stagger/overlap.
|
* Plays multiple card reveal sounds in sequence with slight stagger/overlap. Used when multiple
|
||||||
* Used when multiple
|
* cards are revealed at once (e.g., Flop reveals 3 cards, Turn reveals 1 card). Sounds play
|
||||||
* cards are revealed at once (e.g., Flop reveals 3 cards, Turn reveals 1 card).
|
* with a slight delay between them to simulate a person revealing cards one by one.
|
||||||
* Sounds play
|
|
||||||
* with a slight delay between them to simulate a person revealing cards one by
|
|
||||||
* one.
|
|
||||||
*
|
*
|
||||||
* @param count The number of cards being revealed. Each card gets its own sound
|
* @param count The number of cards being revealed. Each card gets its own sound with stagger.
|
||||||
* with stagger.
|
|
||||||
*/
|
*/
|
||||||
public void playCardRevealMultiple(int count) {
|
public void playCardRevealMultiple(int count) {
|
||||||
if (count <= 0) {
|
if (count <= 0) {
|
||||||
@@ -82,17 +76,21 @@ public class SoundManager {
|
|||||||
long delayMs = cardIndex * CARD_REVEAL_STAGGER_MS;
|
long delayMs = cardIndex * CARD_REVEAL_STAGGER_MS;
|
||||||
|
|
||||||
PauseTransition pause = new PauseTransition(Duration.millis(delayMs));
|
PauseTransition pause = new PauseTransition(Duration.millis(delayMs));
|
||||||
pause.setOnFinished(
|
pause.setOnFinished(this::onCardRevealDelayFinished);
|
||||||
event -> Platform.runLater(
|
pause.play();
|
||||||
() -> {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onCardRevealDelayFinished(ActionEvent event) {
|
||||||
|
Platform.runLater(this::playCardRevealSafely);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void playCardRevealSafely() {
|
||||||
try {
|
try {
|
||||||
playCardReveal();
|
playCardReveal();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.error("Error playing card reveal sound", e);
|
LOGGER.error("Error playing card reveal sound", e);
|
||||||
}
|
}
|
||||||
}));
|
|
||||||
pause.play();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Plays a card shuffle sound. */
|
/** Plays a card shuffle sound. */
|
||||||
@@ -106,8 +104,7 @@ public class SoundManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays a generic sound by key. The sound file should exist at:
|
* Plays a generic sound by key. The sound file should exist at: /sounds/{category}/{key}.mp3 or
|
||||||
* /sounds/{category}/{key}.mp3 or
|
|
||||||
* .wav
|
* .wav
|
||||||
*/
|
*/
|
||||||
public void playSound(String soundKey) {
|
public void playSound(String soundKey) {
|
||||||
@@ -122,10 +119,7 @@ public class SoundManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Gets or loads a sound from cache. Categorizes sounds automatically based on key prefix. */
|
||||||
* Gets or loads a sound from cache. Categorizes sounds automatically based on
|
|
||||||
* key prefix.
|
|
||||||
*/
|
|
||||||
private AudioClip getOrLoadSound(String soundKey) {
|
private AudioClip getOrLoadSound(String soundKey) {
|
||||||
if (soundCache.containsKey(soundKey)) {
|
if (soundCache.containsKey(soundKey)) {
|
||||||
return soundCache.get(soundKey);
|
return soundCache.get(soundKey);
|
||||||
|
|||||||
Reference in New Issue
Block a user