Skip to content

Commit

Permalink
added music and commentary
Browse files Browse the repository at this point in the history
  • Loading branch information
matiduda committed May 31, 2022
1 parent d5195f0 commit dad9999
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ replay_pid*
.vscode/

*Zone.Identifier

# Music and commentary files
src/main/resources/sound/com/*
src/main/resources/sound/music/*
3 changes: 3 additions & 0 deletions src/main/java/fifa/CollisionDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ public void handle(long now) {
if (playSound) {
Sound.kick(KICK_VOLUME);
}

// Play music
Sound.music();
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/fifa/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public static void goalDetected(int angle) {
if (nextRoundDelay > 0)
return;

Sound.commentary();

nextRoundDelay = GAME_TIME_MULTIPLIER * AFTER_GOAL_DELAY_IN_SEC;

// Finish round
Expand All @@ -125,6 +127,7 @@ public static void goalDetected(int angle) {

int rnd = new Random().nextInt(winInfos.length);
info.setText(String.format("%s %s", loserName, winInfos[rnd]));

timeToStart.setVisible(false);
textBoxTimeBack.setVisible(false);

Expand Down
112 changes: 100 additions & 12 deletions src/main/java/fifa/Sound.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,122 @@
package fifa;

import java.net.URL;
import java.util.HashMap;
import java.util.Random;

import javafx.scene.media.AudioClip;

public class Sound {

private static AudioClip kick = null;
private static final double MUSIC_VOLUME = 0.15;

boolean correctlyLoadedSounds = true;
private static AudioClip kick = null;

Sound() {
private static boolean correctlyLoadedKick = false;
private static boolean correctlyLoadedComm = false;
private static boolean correctlyLoadedTrck = false;

// The music and commentary are optional
// and will be downloaded by a shell script

// Check if sounds are present (they are optional and will be downloaded by a
// shell script)
private final static int NUMBER_OF_COM_ENTRIES = 37;
private static HashMap<Integer, AudioClip> commentary = new HashMap<Integer, AudioClip>();

private final static int NUMBER_OF_TRACK_ENTRIES = 9;
private static HashMap<Integer, AudioClip> tracks = new HashMap<Integer, AudioClip>();
private static int currentTrackInd = 0;
private static AudioClip currentTrack = null;

public Sound() {
loadKickSound();
loadCommentary();
loadTracks();

if(!correctlyLoadedKick)
System.out.println("Could not locate the kick sound.");
if(!correctlyLoadedComm)
System.out.println("Could not load commentary. Check README.md for download info!");
if(!correctlyLoadedTrck)
System.out.println("Could not load game music. Check README.md for download info!");
}

public static void kick(double volume) {
if (!kick.isPlaying() && correctlyLoadedKick)
kick.play(volume, 0.5, 1, 0.5, 0);
}

public static void commentary() {
if (correctlyLoadedComm) {
int r = new Random().nextInt(NUMBER_OF_COM_ENTRIES);
commentary.get(r).play();
}
}

public static void music() {
if(!correctlyLoadedTrck)
return;
if (currentTrack != null && currentTrack.isPlaying())
return;
if(currentTrackInd == NUMBER_OF_TRACK_ENTRIES)
currentTrackInd = 0;

currentTrack = tracks.get(currentTrackInd);
currentTrackInd++;
currentTrack.play(MUSIC_VOLUME);
// System.out.println(String"Playing " + currentTrackInd - 1);
}

// --------- Loading sounds ---------

private void loadKickSound() {
String kickSource = getClass().getResource("/sound/kick.wav").toString();
URL res = getClass().getResource("/sound/kick.wav");

if (kickSource == null) {
correctlyLoadedSounds = false;
if(res == null)
return;

String kickSource = res.toString();

if (kickSource != null) {
kick = new AudioClip(kickSource);
correctlyLoadedKick = true;
}
}

private void loadCommentary() {

for(int i = 0; i < NUMBER_OF_COM_ENTRIES; i++) {
String filename = String.format("/sound/com/COM%d.wav", i + 1);

URL res = getClass().getResource(filename);
if(res == null)
return;

kick = new AudioClip(kickSource);
String commSource = res.toString();

if (commSource == null)
return;

commentary.put(i, new AudioClip(commSource));
}
correctlyLoadedComm = true;
}

public static void kick(double volume) {
if (!kick.isPlaying())
kick.play(volume, 0.5, 1, 0.5, 0);
private void loadTracks() {

for(int i = 0; i < NUMBER_OF_TRACK_ENTRIES; i++) {
String filename = String.format("/sound/music/TRACK%d.wav", i + 1);

URL res = getClass().getResource(filename);
if(res == null)
return;

String trackSource = res.toString();

if (trackSource == null)
return;

tracks.put(i, new AudioClip(trackSource));
}
correctlyLoadedTrck = true;
}
}

0 comments on commit dad9999

Please sign in to comment.