Skip to content

Commit 437d9d8

Browse files
committed
Added background music and projectile sound effect
1 parent 6ac82fa commit 437d9d8

File tree

6 files changed

+60
-29
lines changed

6 files changed

+60
-29
lines changed

res/sfx/smallProjectile.wav

9.95 KB
Binary file not shown.

src/com/redomar/game/Game.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.redomar.game;
22

3+
import com.redomar.game.audio.AudioHandler;
34
import com.redomar.game.entities.Dummy;
45
import com.redomar.game.entities.Player;
56
import com.redomar.game.entities.Vendor;
@@ -46,7 +47,7 @@ public class Game extends Canvas implements Runnable {
4647
private static boolean closingMode;
4748

4849
private static JFrame frame;
49-
50+
private static AudioHandler backgroundMusic;
5051
private static boolean running = false;
5152
private static InputHandler input;
5253
private static MouseHandler mouse;
@@ -260,14 +261,22 @@ public static void setAlternateCols(boolean[] alternateCols) {
260261
Game.alternateCols = alternateCols;
261262
}
262263

263-
public static void setAternateColsR(boolean alternateCols) {
264+
public static void setAlternateColsR(boolean alternateCols) {
264265
Game.alternateCols[1] = alternateCols;
265266
}
266267

267-
public static void setAternateColsS(boolean alternateCols) {
268+
public static void setAlternateColsS(boolean alternateCols) {
268269
Game.alternateCols[0] = alternateCols;
269270
}
270271

272+
public static void setBackgroundMusic(AudioHandler backgroundMusic) {
273+
Game.backgroundMusic = backgroundMusic;
274+
}
275+
276+
public static AudioHandler getBackgroundMusic(){
277+
return Game.backgroundMusic;
278+
}
279+
271280
public static InputHandler getInput() {
272281
return input;
273282
}
@@ -471,7 +480,10 @@ public void render() {
471480
}
472481
g.setColor(Color.YELLOW);
473482
g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3));
474-
g.setColor(Color.WHITE);
483+
g.setColor(Color.GREEN);
484+
if(backgroundMusic.getActive()) {
485+
g.drawString("MUSIC is ON ", 3, getHeight() - 3);
486+
}
475487
g.dispose();
476488
bs.show();
477489
}

src/com/redomar/game/InputHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,16 @@ private void toggleKey(int keyCode, boolean isPressed) {
6868
left.toggle(false);
6969
right.toggle(false);
7070
}
71+
7172
if (keyCode == KeyEvent.VK_M) {
72-
//music here
73+
Game.getBackgroundMusic().play();
74+
}
75+
76+
if (keyCode == KeyEvent.VK_COMMA) {
77+
Game.getBackgroundMusic().stop();
7378
}
7479

80+
7581
if (keyCode == KeyEvent.VK_W && isAzertyCountry || keyCode == KeyEvent.VK_Z && !isAzertyCountry) {
7682
// if (map == 0){
7783
// Game.getGame().setMap("/levels/water_level.png");

src/com/redomar/game/audio/AudioHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class AudioHandler {
77

88
private Clip clip;
9+
private boolean active = false;
910

1011
public AudioHandler(String path){
1112
try{
@@ -33,6 +34,7 @@ public void play(){
3334
stop();
3435
clip.setFramePosition(0);
3536
clip.start();
37+
active = true;
3638
}
3739

3840
public void setVolume(float velocity){
@@ -43,11 +45,16 @@ public void setVolume(float velocity){
4345

4446
public void stop() {
4547
if (clip.isRunning()) clip.stop();
48+
active = false;
4649
}
4750

4851
public void close(){
4952
stop();
5053
clip.close();
5154
}
5255

56+
public boolean getActive(){
57+
return this.active;
58+
}
59+
5360
}

src/com/redomar/game/entities/Player.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.redomar.game.Game;
44
import com.redomar.game.InputHandler;
5+
import com.redomar.game.audio.AudioEffect;
56
import com.redomar.game.entities.efx.Swim;
67
import com.redomar.game.entities.projectiles.Medium;
78
import com.redomar.game.entities.projectiles.Projectile;
@@ -28,6 +29,8 @@ public class Player extends Mob {
2829
private int[] swimColour;
2930
private int fireRate = 0;
3031

32+
private static AudioEffect shootSound;
33+
3134
public Player(LevelHandler level, int x, int y, InputHandler input,
3235
String userName, int shirtCol, int faceCol) {
3336
super(level, "Player", x, y, speed, collisionBoders);
@@ -72,6 +75,8 @@ public void tick() {
7275
if(fireRate <= 0){
7376
if(Game.getMouse().getButton()== 1){
7477
fireRate = Small.FIRE_RATE;
78+
shootSound.setVolume(-15);
79+
shootSound.play();
7580
}else if(Game.getMouse().getButton() == 3){
7681
fireRate = Medium.FIRE_RATE;
7782
}
@@ -202,6 +207,14 @@ public void render(Screen screen) {
202207
}
203208
}
204209

210+
public static AudioEffect getShootSound() {
211+
return shootSound;
212+
}
213+
214+
public static void setShootSound(AudioEffect shootSound) {
215+
Player.shootSound = shootSound;
216+
}
217+
205218
public String getUsername() {
206219
if (this.userName.isEmpty()) {
207220
return guestPlayerName;

src/com/redomar/game/menu/Menu.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.redomar.game.menu;
22

33
import com.redomar.game.Game;
4+
import com.redomar.game.audio.AudioEffect;
5+
import com.redomar.game.audio.AudioHandler;
6+
import com.redomar.game.entities.Player;
47
import com.redomar.game.lib.Font;
58
import com.redomar.game.lib.Mouse;
69
import com.thehowtotutorial.splashscreen.JSplash;
@@ -46,23 +49,13 @@ public static void play() {
4649
splash.toFront();
4750
splash.requestFocus();
4851
splash.splashOn();
49-
splash.setProgress(10, "Initializing Game");
50-
Thread.sleep(250);
51-
splash.setProgress(25, "Loading Classes");
52-
Thread.sleep(125);
53-
splash.setProgress(35, "Applying Configurations");
54-
Thread.sleep(125);
55-
splash.setProgress(40, "Loading Sprites");
56-
Thread.sleep(250);
57-
splash.setProgress(50, "Loading Textures");
58-
Thread.sleep(125);
59-
splash.setProgress(60, "Loading Map");
60-
Thread.sleep(500);
61-
splash.setProgress(80, "Configuring Map");
62-
Thread.sleep(125);
63-
splash.setProgress(90, "Pulling InputPanes");
64-
Thread.sleep(250);
65-
splash.setProgress(92, "Acquiring data: Multiplayer");
52+
splash.setProgress(10, "Initializing SFX");
53+
Player.setShootSound( new AudioEffect("/sfx/smallProjectile.wav"));
54+
splash.setProgress(30, "Loading Music");
55+
Game.setBackgroundMusic(new AudioHandler("/music/Towards The End.mp3"));
56+
splash.setProgress(40, "Setting Volume");
57+
Game.getBackgroundMusic().setVolume(-20);
58+
splash.setProgress(50, "Acquiring data: Multiplayer");
6659
Thread.sleep(125);
6760
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
6861
String multiMsg = "Sorry but multiplayer has been disabled on this version.\nIf you would like multiplayer checkout Alpha 1.6";
@@ -76,23 +69,23 @@ public static void play() {
7669
"Enter the name \nleave blank for local"));
7770
}
7871
Thread.sleep(125);
79-
splash.setProgress(94, "Acquiring data: Username");
72+
splash.setProgress(70, "Acquiring data: Username");
8073
String s = JOptionPane.showInputDialog(Game.getGame(),
8174
"Enter a name");
8275
if (s != null) {
8376
Game.setJdata_UserName(s);
8477
}
8578
Thread.sleep(125);
86-
splash.setProgress(96, "Collecting Player Data");
79+
splash.setProgress(90, "Collecting Player Data");
8780
Object[] options = {"African", "Caucasian"};
8881
int n = JOptionPane.showOptionDialog(frame,
8982
"Choose a race for the charater to be", "Choose a race",
9083
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
9184
null, options, options[0]);
9285
if (n == 0) {
93-
Game.setAternateColsR(true);
86+
Game.setAlternateColsR(true);
9487
} else {
95-
Game.setAternateColsR(false);
88+
Game.setAlternateColsR(false);
9689
}
9790
Thread.sleep(250);
9891
Object[] options1 = {"Orange", "Black"};
@@ -101,11 +94,11 @@ public static void play() {
10194
"Choose a shirt Colour", JOptionPane.YES_NO_OPTION,
10295
JOptionPane.QUESTION_MESSAGE, null, options1, options1[0]);
10396
if (n1 == 0) {
104-
Game.setAternateColsS(true);
97+
Game.setAlternateColsS(true);
10598
} else {
106-
Game.setAternateColsS(false);
99+
Game.setAlternateColsS(false);
107100
}
108-
splash.setProgress(97, "Connecting as" + Game.getJdata_UserName());
101+
splash.setProgress(100, "Connecting as" + Game.getJdata_UserName());
109102
Thread.sleep(250);
110103
splash.splashOff();
111104
frame = new DedicatedJFrame(WIDTH, HEIGHT, SCALE, NAME);

0 commit comments

Comments
 (0)