Add docs to Acoustic

This commit is contained in:
Felix
2024-11-16 16:33:21 +01:00
parent 8c10e69eff
commit e128d9e4cc
8 changed files with 206 additions and 6 deletions

View File

@@ -29,10 +29,12 @@ public class AcousticHandler {
private ArrayList<GameSound> sounds = new ArrayList<>();
public AcousticHandler(MdgaApp app) {
this.app = app;
}
/**
* This method updates the acousticHandler and should be called every frame
*/
public void update() {
updateVolumeAndTrack();
@@ -52,6 +54,11 @@ public void update() {
}
}
/**
* This method instantly plays a sound
*
* @param sound the sound to be played
*/
public void playSound(MdgaSound sound) {
ArrayList<SoundAssetDelayVolume> assets = new ArrayList<SoundAssetDelayVolume>();
switch (sound) {
@@ -72,6 +79,11 @@ public void playSound(MdgaSound sound) {
}
}
/**
* This method fades the played music to fit the state.
*
* @param state the state of which the corresponding music should be played to be played
*/
public void playState(MdgaState state) {
if(this.state == state) {
return;
@@ -105,10 +117,23 @@ public void playState(MdgaState state) {
scheduled = new GameMusic(app, asset, getMusicVolumeTotal(), asset.getSubVolume(), asset.getLoop());
}
/**
* Performs linear interpolation between two float values.
*
* @param start The starting value.
* @param end The ending value.
* @param t The interpolation factor, typically between 0 and 1.
* @return The interpolated value between start and end.
*/
private float lerp(float start, float end, float t) {
return start + t * (end - start);
}
/**
* Updates the current volume and handles track crossfading logic.
* This method is responsible for fading out the currently playing track,
* fading in the scheduled track, and handling crossfade between the two tracks.
*/
private void updateVolumeAndTrack() {
if (playing == null && scheduled != null && !fading) {
playing = scheduled;
@@ -157,6 +182,10 @@ private void updateVolumeAndTrack() {
}
}
/**
* Adds a list of game tracks to the gameTracks collection and shuffles them.
* This method adds predefined game tracks to the track list and shuffles the order.
*/
private void addGameTracks() {
Random random = new Random();
@@ -166,6 +195,10 @@ private void addGameTracks() {
Collections.shuffle(gameTracks, random);
}
/**
* Updates the current game tracks. If the currently playing track is nearing its end,
* a new track will be scheduled to play. If the list of game tracks is empty, it will be refreshed.
*/
private void updateGameTracks() {
if(playing.nearEnd(10)) {
if (gameTracks.isEmpty()) {
@@ -182,35 +215,75 @@ private void updateGameTracks() {
}
}
/**
* Retrieves the main volume level.
*
* @return The current main volume level.
*/
public float getMainVolume() {
return mainVolume;
}
/**
* Retrieves the music volume level.
*
* @return The current music volume level.
*/
public float getMusicVolume() {
return musicVolume;
}
/**
* Retrieves the sound volume level.
*
* @return The current sound volume level.
*/
public float getSoundVolume() {
return soundVolume;
}
/**
* Sets the main volume level.
*
* @param mainVolume The desired main volume level.
*/
public void setMainVolume(float mainVolume) {
this.mainVolume = mainVolume;
}
/**
* Sets the music volume level.
*
* @param musicVolume The desired music volume level.
*/
public void setMusicVolume(float musicVolume) {
this.musicVolume = musicVolume;
}
/**
* Sets the sound volume level.
*
* @param soundVolume The desired sound volume level.
*/
public void setSoundVolume(float soundVolume) {
this.soundVolume = soundVolume;
}
/**
* Calculates the total music volume by multiplying the music volume by the main volume.
*
* @return The total music volume.
*/
float getMusicVolumeTotal() {
return getMusicVolume() * getMainVolume();
}
/**
* Calculates the total sound volume by multiplying the sound volume by the main volume.
*
* @return The total sound volume.
*/
float getSoundVolumeTotal() {
return getSoundVolume() * getMainVolume();
}

View File

@@ -5,12 +5,25 @@
import com.jme3.audio.AudioSource;
import pp.mdga.client.MdgaApp;
/**
* Represents a game music track, including its playback controls and volume settings.
* This class manages the playback of a music track, allowing for playing, pausing,
* volume adjustment, and tracking the current status of the music.
*/
class GameMusic {
private float volume;
private final float subVolume;
private final AudioNode music;
/**
* Constructs a new GameMusic object.
*
* @param app The instance of the application, used to access the asset manager.
* @param asset The music asset to be played.
* @param volume The total volume of the music, adjusted by the main volume.
* @param subVolume A relative volume that modifies the base music volume, typically a percentage.
* @param loop A flag indicating whether the music should loop once it finishes.
*/
GameMusic(MdgaApp app, MusicAsset asset, float volume, float subVolume, boolean loop) {
this.volume = volume;
this.subVolume = subVolume;
@@ -23,6 +36,11 @@ class GameMusic {
music.setLooping(loop);
}
/**
* Plays the current music track.
* If the music is already initialized, it starts playback.
* If the music is not available, no action is performed.
*/
void play() {
if(null == music) {
return;
@@ -31,6 +49,10 @@ void play() {
music.play();
}
/**
* Pauses the current music track.
* If the music is not available or is not playing, no action is performed.
*/
void pause() {
if(null == music) {
return;
@@ -39,11 +61,23 @@ void pause() {
music.stop();
}
/**
* Checks if the current music track is playing.
*
* @return true if the music is playing, false otherwise.
*/
boolean isPlaying() {
return music.getStatus() == AudioSource.Status.Playing;
}
/**
* Checks if the current music track is near the end.
*
* @param thresholdSeconds The threshold in seconds. If the remaining time is less than or equal to this value,
* the track is considered near the end.
* @return true if the track is near its end (within the threshold), false otherwise.
*/
boolean nearEnd(float thresholdSeconds) {
if (music == null || !isPlaying()) {
return false;
@@ -61,6 +95,12 @@ boolean nearEnd(float thresholdSeconds) {
return remainingTime <= thresholdSeconds;
}
/**
* Updates the volume of the music.
* If the volume has changed, it will adjust the music's volume accordingly.
*
* @param newVolume The new total volume for the music.
*/
void update(float newVolume) {
if(volume != newVolume) {
volume = newVolume;

View File

@@ -6,6 +6,11 @@
import com.jme3.system.NanoTimer;
import pp.mdga.client.MdgaApp;
/**
* Represents a game sound effect, with control over playback, volume, and timing.
* This class manages the playback of a sound effect, including starting playback after a delay,
* adjusting volume, and tracking whether the sound has finished playing.
*/
class GameSound {
private float volume;
final private float subVolume;
@@ -18,6 +23,15 @@ class GameSound {
private float delay = 0.0f;
private NanoTimer timer = null;
/**
* Constructs a new GameSound object.
*
* @param app The instance of the application, used to access the asset manager.
* @param asset The sound asset to be played.
* @param volume The total volume of the sound, adjusted by the main volume.
* @param subVolume A relative volume that modifies the base sound volume, typically a percentage.
* @param delay The delay before the sound starts playing, in seconds.
*/
GameSound(MdgaApp app, SoundAsset asset, float volume, float subVolume, float delay) {
this.volume = volume;
this.subVolume = subVolume;
@@ -32,10 +46,21 @@ class GameSound {
timer = new NanoTimer();
}
/**
* Checks if the sound is currently playing.
*
* @return true if the sound is playing, false otherwise.
*/
boolean isPlaying() {
return !finished;
}
/**
* Updates the sound playback, adjusting the volume if necessary, and starting
* the sound after the specified delay.
*
* @param newVolume The new total volume for the sound.
*/
void update(float newVolume) {
if(!playing && timer.getTimeInSeconds() > delay) {
sound.play();

View File

@@ -1,5 +1,13 @@
package pp.mdga.client.Acoustic;
/**
* Enum representing the various sound effects used in the game.
* Each sound corresponds to an event or action in the game and may consist of one or more
* audio files, potentially with time delays between them.
*
* These sounds are used to play specific audio cues, such as when a dice is rolled,
* a turn starts or ends, a piece is moved or lost, and various other events in the game.
*/
public enum MdgaSound {
DICE_ROLL,
TURN_START,

View File

@@ -1,5 +1,11 @@
package pp.mdga.client.Acoustic;
/**
* Enum representing various music assets used in the game.
* Each constant corresponds to a specific music track, along with its properties such as file path,
* looping behavior, and relative volume (subVolume).
* These music assets are used to control the music that plays in different parts of the game, such as menus and in-game music.
*/
enum MusicAsset {
MAIN_MENU("Spaceship.wav", 1.0f),
LOBBY("DeadPlanet.wav", 1.0f),
@@ -15,26 +21,55 @@ enum MusicAsset {
private final boolean loop;
private final float subVolume;
/**
* Constructs a new MusicAsset object with the specified name and sub-volume.
* The track will not loop by default.
*
* @param name The name of the music file.
* @param subVolume A relative volume that modifies the base volume of the track (typically a percentage).
*/
MusicAsset(String name, float subVolume) {
this.path = "music/" + name;
this.loop = false;
this.subVolume = subVolume;
}
/**
* Constructs a new MusicAsset object with the specified name, loop flag, and sub-volume.
*
* @param name The name of the music file.
* @param loop If true, the track will loop; otherwise, it will play once.
* @param subVolume A relative volume that modifies the base volume of the track (typically a percentage).
*/
MusicAsset(String name, boolean loop, float subVolume) {
this.path = "music/" + name;
this.loop = loop;
this.subVolume = subVolume;
}
/**
* Gets the file path of the music track.
*
* @return The path to the music file (relative to the music folder).
*/
public String getPath() {
return path;
}
/**
* Gets whether the music track should loop.
*
* @return true if the track should loop, false otherwise.
*/
public boolean getLoop() {
return loop;
}
/**
* Gets the relative volume (subVolume) for the music track.
*
* @return The relative volume for the track, typically a value between 0.0 and 1.0.
*/
public float getSubVolume() {
return subVolume;
}

View File

@@ -1,5 +1,11 @@
package pp.mdga.client.Acoustic;
/**
* Enum representing various sound assets used in the game.
* Each constant corresponds to a specific sound effect used throughout the game.
* These sounds are associated with various events and actions, such as dice rolls,
* turn changes, piece movements, and game outcomes.
*/
enum SoundAsset {
DICE_ROLL(""),
TURN_START(""),
@@ -15,10 +21,20 @@ enum SoundAsset {
private final String path;
/**
* Constructs a new SoundAsset object with the specified name.
*
* @param name The name of the sound file.
*/
SoundAsset(String name) {
this.path = "sound/" + name;
}
/**
* Gets the file path of the sound effect.
*
* @return The path to the sound file (relative to the sound folder).
*/
public String getPath() {
return path;
}

View File

@@ -1,4 +1,7 @@
package pp.mdga.client.Acoustic;
record SoundAssetDelayVolume(SoundAsset asset, float subVolume, float delay) {
}
/**
* A record that encapsulates a sound asset along with its playback settings:
* the relative volume (subVolume) and a delay before it starts playing.
*/
record SoundAssetDelayVolume(SoundAsset asset, float subVolume, float delay) { }

View File

@@ -20,8 +20,8 @@ public class BoardView {
private final MdgaApp app;
private PileControl drawPile = new PileControl();
private PileControl discardPile = new PileControl();
private PileControl drawPile = null;
private PileControl discardPile = null;
private ArrayList<NodeControl> infield = new ArrayList<NodeControl>(40);
private ArrayList<PieceControl> pieces;