let there be sound

This commit is contained in:
musilto9 2020-04-23 00:00:02 +02:00
parent 25a20341ea
commit 489ddd6f28
28 changed files with 298 additions and 14 deletions

View File

@ -92,6 +92,22 @@ public class ChestCell extends InteractionCell {
else if (data.equals("<item>")) { else if (data.equals("<item>")) {
putItemToInventory(); putItemToInventory();
} }
else if (data.contains("<voice>")) {
String[] voice = data.split(" ");
if (!voice[1].equals("null")) {
dialog.setVoiceover(true);
dialog.setVoice(voice[1]);
}
else {
dialog.setVoiceover(false);
}
readNextLine();
}
else if (data.contains("<sound>")) {
String[] sound = data.split(" ");
dialog.getSoundManager().playSound(sound[1], Float.parseFloat(sound[2]));
readNextLine();
}
else { else {
if (data.contains("<br/>")) if (data.contains("<br/>"))
data = "<html>" + data + "</html>"; data = "<html>" + data + "</html>";

View File

@ -11,12 +11,15 @@ public class DoorCell extends InteractionCell {
private boolean isOpen; private boolean isOpen;
private boolean isLocked; private boolean isLocked;
private boolean directionVert; private boolean directionVert;
private String sound;
private float volume;
public DoorCell(int x, int y, boolean directionVert, String message, Dialog dialog, ImageCell imageCell, WindowOfFreeMove window) { public DoorCell(int x, int y, boolean directionVert, String message, Dialog dialog, ImageCell imageCell, WindowOfFreeMove window) {
super(x, y, message, dialog, imageCell, window, true); super(x, y, message, dialog, imageCell, window, true);
this.isOpen = false; this.isOpen = false;
this.isLocked = true; this.isLocked = true;
this.directionVert = directionVert; this.directionVert = directionVert;
setSound();
} }
public DoorCell(int x, int y, boolean directionVert, ImageCell imageCell, WindowOfFreeMove window) { public DoorCell(int x, int y, boolean directionVert, ImageCell imageCell, WindowOfFreeMove window) {
@ -24,6 +27,7 @@ public class DoorCell extends InteractionCell {
this.isOpen = false; this.isOpen = false;
this.isLocked = false; this.isLocked = false;
this.directionVert = directionVert; this.directionVert = directionVert;
setSound();
} }
@Override @Override
@ -37,15 +41,32 @@ public class DoorCell extends InteractionCell {
@Override @Override
public void useItem(Item item) { public void useItem(Item item) {
isLocked = false; isLocked = false;
setSound();
super.useItem(item); super.useItem(item);
} }
private void setSound() {
if (isOpen) {
sound = "qubodup-DoorOpen01-changed.wav";
volume = -8.0f;
}
else if (!isOpen && !isLocked) {
sound = "qubodup-DoorClose02-changed.wav";
volume = -8.0f;
}
else if (isLocked) {
sound = "doorLocked.wav";
volume = -10.0f;
}
}
public boolean isOpen() { public boolean isOpen() {
return isOpen; return isOpen;
} }
public void setOpen(boolean open) { public void setOpen(boolean open) {
isOpen = open; isOpen = open;
setSound();
} }
public boolean isLocked() { public boolean isLocked() {
@ -54,5 +75,10 @@ public class DoorCell extends InteractionCell {
public void setLocked(boolean locked) { public void setLocked(boolean locked) {
isLocked = locked; isLocked = locked;
setSound();
} }
public String getSound() { return sound; }
public float getVolume() { return volume; }
} }

View File

@ -13,7 +13,6 @@ import java.awt.*;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class InteractionCell extends Cell { public class InteractionCell extends Cell {
protected String message; protected String message;
@ -78,6 +77,22 @@ public class InteractionCell extends Cell {
} }
readNextLine(); readNextLine();
} }
else if (data.contains("<voice>")) {
String[] voice = data.split(" ");
if (!voice[1].equals("null")) {
dialog.setVoiceover(true);
dialog.setVoice(voice[1]);
}
else {
dialog.setVoiceover(false);
}
readNextLine();
}
else if (data.contains("<sound>")) {
String[] sound = data.split(" ");
dialog.getSoundManager().playSound(sound[1], Float.parseFloat(sound[2]));
readNextLine();
}
else if (data.contains("<cameraFree>")) { else if (data.contains("<cameraFree>")) {
String[] cameraInfo = data.split(" "); String[] cameraInfo = data.split(" ");
window.getCamera().setCameraStart(); window.getCamera().setCameraStart();
@ -219,6 +234,7 @@ public class InteractionCell extends Cell {
} }
else if (i == 11) { else if (i == 11) {
window.getMusicManager().playMusic(); window.getMusicManager().playMusic();
window.getMusicManager().fadeIn(10);
timer.stop(); timer.stop();
} }
} }

View File

@ -99,14 +99,19 @@ public class Person extends Cell {
else if (isDoorInFront(x,y,directionSight)) { else if (isDoorInFront(x,y,directionSight)) {
DoorCell door = map.findDoorByXY(x, y, directionSight); DoorCell door = map.findDoorByXY(x, y, directionSight);
if (!door.isLocked() && !isInEvent) { if (!door.isLocked() && !isInEvent) {
if (door.isOpen()) if (door.isOpen()) {
door.setOpen(false); door.setOpen(false);
else map.getSoundManager().playSound(door.getSound(), door.getVolume());
}
else {
door.setOpen(true); door.setOpen(true);
map.getSoundManager().playSound(door.getSound(), door.getVolume());
}
} }
else if (!isInEvent) { else if (!isInEvent) {
isInEvent = true; isInEvent = true;
door.startEvent(); door.startEvent();
map.getSoundManager().playSound(door.getSound(), door.getVolume());
} }
else { else {
door.readNextLine(); door.readNextLine();
@ -215,6 +220,9 @@ public class Person extends Cell {
else { else {
isMoving = false; isMoving = false;
} }
if (!map.getName().equals("tutorial")){
map.getSoundManager().playSoundRepeatedly("wood01.wav", 20, -7.0f);
}
} else { } else {
distanceOfAnimation = getSize(); distanceOfAnimation = getSize();
if (map.getName().equals("tutorial")) if (map.getName().equals("tutorial"))

View File

@ -1,6 +1,7 @@
package ruscz.game.gameOfProd.Dialog; package ruscz.game.gameOfProd.Dialog;
import ruscz.game.gameOfProd.Image.ImageDialog; import ruscz.game.gameOfProd.Image.ImageDialog;
import ruscz.game.gameOfProd.Sounds.SoundManager;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import javax.swing.*; import javax.swing.*;
@ -16,6 +17,9 @@ public class Dialog {
private DialogBar dialogBar; private DialogBar dialogBar;
private int charNumber; private int charNumber;
private boolean timerIsRunning; private boolean timerIsRunning;
private SoundManager soundManager;
private String voice;
private boolean voiceover;
Dialog(String text, DialogBar dialogBar) { Dialog(String text, DialogBar dialogBar) {
this.text = text; this.text = text;
@ -35,6 +39,9 @@ public class Dialog {
public Dialog(DialogBar dialogBar) { public Dialog(DialogBar dialogBar) {
this.dialogBar = dialogBar; this.dialogBar = dialogBar;
charNumber = 0; charNumber = 0;
soundManager = new SoundManager();
voice = "voice1";
voiceover = true;
} }
@ -80,9 +87,11 @@ public class Dialog {
if (text.contains("<html>")) if (text.contains("<html>"))
charNumber = 6; charNumber = 6;
dialogBar.setVisible(true); dialogBar.setVisible(true);
timer = new Timer(20, new ActionListener() { timer = new Timer(30, new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (voiceover)
soundManager.playSound(voice + ".wav", -20.0f);
showByChar(); showByChar();
dialogBar.repaint(); dialogBar.repaint();
timerIsRunning = timer.isRunning(); timerIsRunning = timer.isRunning();
@ -123,5 +132,11 @@ public class Dialog {
public boolean getTimerIsRunning() { public boolean getTimerIsRunning() {
return timerIsRunning; return timerIsRunning;
} }
public void setVoice(String voice) { this.voice = voice; }
public SoundManager getSoundManager() { return soundManager; }
public void setVoiceover(boolean voiceover) { this.voiceover = voiceover; }
} }

View File

@ -3,8 +3,7 @@ package ruscz.game.gameOfProd.Map;
import ruscz.game.gameOfProd.Cells.*; import ruscz.game.gameOfProd.Cells.*;
import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Dialog.Dialog;
import ruscz.game.gameOfProd.Image.ImageCell; import ruscz.game.gameOfProd.Image.ImageCell;
import ruscz.game.gameOfProd.Inventory.Item; import ruscz.game.gameOfProd.Sounds.SoundManager;
import ruscz.game.gameOfProd.Inventory.ItemAction;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.io.File; import java.io.File;
@ -26,6 +25,7 @@ public class Map {
private int idExit; private int idExit;
private ImageCell imageCell; private ImageCell imageCell;
private int[] canvasSize; private int[] canvasSize;
private SoundManager soundManager;
private static Scanner scanner; private static Scanner scanner;
@ -42,6 +42,7 @@ public class Map {
this.mapArray = mapArray; this.mapArray = mapArray;
person = new Person(this); person = new Person(this);
canvasSize = new int[2]; canvasSize = new int[2];
soundManager = new SoundManager();
} }
public static Map findMapByName(String nameOfMap, WindowOfFreeMove window) { public static Map findMapByName(String nameOfMap, WindowOfFreeMove window) {
@ -264,6 +265,7 @@ public class Map {
public int[] getCanvasSize() { return canvasSize; } public int[] getCanvasSize() { return canvasSize; }
public SoundManager getSoundManager() { return soundManager; }
public boolean isBusyCell(int x, int y) { public boolean isBusyCell(int x, int y) {
for (Block cell : block) { for (Block cell : block) {

View File

@ -1,29 +1,37 @@
package ruscz.game.gameOfProd.Sounds; package ruscz.game.gameOfProd.Sounds;
import javax.sound.sampled.*; import javax.sound.sampled.*;
import javax.swing.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
public class MusicManager { public class MusicManager {
private String musicName; private String musicName;
private float volume;
private float maxVolume;
private FloatControl gainControl;
private Timer timerIn;
private Timer timerOut;
private Clip clip;
public MusicManager(String name){ public MusicManager(String name, float volume){
musicName = name; musicName = name;
this.maxVolume = volume;
this.volume = -40.0f;
} }
public void playMusic(){ public void playMusic(){
AudioInputStream audioIn; AudioInputStream audioIn;
try { try {
audioIn = AudioSystem.getAudioInputStream(new File("src/main/resources/" + musicName)); audioIn = AudioSystem.getAudioInputStream(new File("src/main/resources/music/" + musicName));
Clip clip;
clip = AudioSystem.getClip(); clip = AudioSystem.getClip();
if (clip != null) { if (clip != null) {
clip.open(audioIn); clip.open(audioIn);
} }
if (clip != null) { if (clip != null) {
FloatControl gainControl = gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-25.0f); gainControl.setValue(volume);
clip.loop(1000); clip.loop(1000);
clip.start(); clip.start();
} }
@ -31,4 +39,35 @@ public class MusicManager {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void stopMusic() {
timerOut.stop();
clip.stop();
volume = -40.0f;
}
public void fadeIn(int delay) {
timerIn = new Timer(delay, actionEvent -> {
if (volume < maxVolume) {
volume += 0.1f;
gainControl.setValue(volume);
}
});
timerIn.start();
}
public void fadeOut(int delay) {
timerOut = new Timer(delay, actionEvent -> {
if (volume > -79.0f) {
volume -= 0.1f;
gainControl.setValue(volume);
}
else {
stopMusic();
}
System.out.println(volume);
});
timerIn.stop();
timerOut.start();
}
} }

View File

@ -0,0 +1,53 @@
package ruscz.game.gameOfProd.Sounds;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
public class SoundManager {
private FloatControl gainControl;
private Clip clip;
private int delay;
public SoundManager() {
this.delay = 0;
}
public void playSound(String name, float volume){
AudioInputStream audioIn;
try {
audioIn = AudioSystem.getAudioInputStream(new File("src/main/resources/sounds/" + name));
clip = AudioSystem.getClip();
if (clip != null) {
clip.open(audioIn);
}
if (clip != null) {
gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(volume);
clip.start();
}
} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
public void playSoundRepeatedly(String name, int delay, float volume) {
if (this.delay == delay) {
AudioInputStream audioIn;
try {
audioIn = AudioSystem.getAudioInputStream(new File("src/main/resources/sounds/" + name));
clip = AudioSystem.getClip();
clip.open(audioIn);
gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(volume);
clip.start();
} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
e.printStackTrace();
}
this.delay = 0;
}
else {
this.delay++;
}
}
}

View File

@ -174,7 +174,7 @@ public class WindowOfFreeMove extends JLayeredPane {
mapManager.createMap(); mapManager.createMap();
isReadyToPaint = true; isReadyToPaint = true;
musicManager = new MusicManager("Town3.wav"); musicManager = new MusicManager("Town3.wav", -25.0f);
// musicManager.playMusic(); // musicManager.playMusic();
Timer timer = new Timer(10, actionEvent -> { Timer timer = new Timer(10, actionEvent -> {

View File

@ -1,7 +1,10 @@
<image> boy2-face.png <image> boy2-face.png
<voice> voice5
... no, I've not heard anything from him. ... no, I've not heard anything from him.
<image> girl1-face.png <image> girl1-face.png
I mean he would't just vanish, right? <voice> voice2
I mean he wouldn't just vanish, right?
<image> boy2-face.png <image> boy2-face.png
<voice> voice5
Beats me. Beats me.
<end> <end>

View File

@ -1,3 +1,4 @@
<image> boy3-face.png <image> boy3-face.png
<voice> voice3
*slaps rack*<br/>this bad boy can fit so many minecraft servers *slaps rack*<br/>this bad boy can fit so many minecraft servers
<end> <end>

View File

@ -1,8 +1,9 @@
<cameraFree> 100 0 35 <cameraFree> 100 0 35
<wait> <wait>
<image> boss-face.png <image> boss-face.png
<voice> voice4
Oh, hi Mark. Oh, hi Mark.
I know, this is on very short notice<br/>but as you've probably heard,<br/>we kinda need someone on the ruscz.game.gameOfProd.Game of Prod. I know, this is on very short notice<br/>but as you've probably heard,<br/>we kinda need someone on the Game of Prod.
And that someone is gonna be,<br/>you guessed it, you. You start by tomorrow. And that someone is gonna be,<br/>you guessed it, you. You start by tomorrow.
You can go now. You can go now.
Bye. Bye.

View File

@ -1,3 +1,5 @@
<voice> null
<sound> metalPot2.wav -10.0f
Just like the trash you are.. Just like the trash you are..
<item> <item>
<end> <end>

View File

@ -0,0 +1,95 @@
<map>
<mapName>m1</mapName>
<events>
</events>
</map>
<map>
<mapName>m2</mapName>
<events>
<trigger>
<x>7</x>
<y>2</y>
<active>true</active>
</trigger>
</events>
</map>
<map>
<mapName>m3</mapName>
<events>
<trigger>
<x>2</x>
<y>5</y>
<active>true</active>
</trigger>
</events>
</map>
<map>
<mapName>m4</mapName>
<events>
<trigger>
<x>3</x>
<y>3</y>
<active>true</active>
</trigger>
</events>
</map>
<map>
<mapName>m6</mapName>
<events>
<trigger>
<x>11</x>
<y>11</y>
<active>true</active>
</trigger>
</events>
</map>
<map>
<mapName>tutorial</mapName>
<events>
<trigger>
<x>11</x>
<y>6</y>
<active>false</active>
</trigger>
</events>
</map>
<map>
<mapName>openspace</mapName>
<events>
<trigger>
<x>7</x>
<y>4</y>
<active>false</active>
</trigger>
<chest>
<x>15</x>
<y>7</y>
<active>true</active>
</chest>
</events>
</map>
<map>
<mapName>office</mapName>
<events>
<chest>
<x>1</x>
<y>9</y>
<active>true</active>
</chest>
</events>
</map>
<map>
<mapName>boss</mapName>
<events>
<trigger>
<x>4</x>
<y>18</y>
<active>false</active>
</trigger>
</events>
</map>
<map>
<mapName>treasure</mapName>
<events>
</events>
</map>

View File

@ -0,0 +1,6 @@
<location>
<map>openspace</map>
<x>8</x>
<y>15</y>
<direction>3</direction>
</location>

View File

@ -0,0 +1 @@
TODO: Village

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.