Skip to content

Commit 26bed52

Browse files
authored
Alpha 1.8.3 - New Audio Manager
Final
2 parents c680658 + 9664b4a commit 26bed52

File tree

19 files changed

+301
-191
lines changed

19 files changed

+301
-191
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ language: java
33
# blocklist
44
branches:
55
except:
6-
- develop
7-
- unitTesting
6+
- develop

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
JavaGame Alpha v1.8.2 [![Travis-CI Build Status](https://travis-ci.org/redomar/JavaGame.svg?branch=master)](https://travis-ci.org/redomar/JavaGame)
2-
=====================
3-
Acknowledgement: Inspired from [vanZeben](https://github.com/vanZeben)
1+
JavaGame [![GitHub release](https://img.shields.io/github/release/redomar/JavaGame.svg?style=flat-square&label=Alpha)](https://github.com/redomar/JavaGame/releases/latest) [![Travis-CI Build Status](https://img.shields.io/travis/redomar/JavaGame.svg?style=flat-square)](https://travis-ci.org/redomar/JavaGame) [![GitHub license](https://img.shields.io/badge/license-AGPLv3-red.svg?style=flat-square)](https://raw.githubusercontent.com/Redomar/JavaGame/master/LICENSE)
2+
==
43

54
#####What is JavaGame?
65
JavaGame is a game project that have been working on since May 2013. I have added many features to the game over the last year and I plan on adding even more features. This game is purely for my own sake to practice my skills in Java.
@@ -10,9 +9,9 @@ Well i'm still not sure what exactly i'm going to do with it, and I haven't thou
109

1110
#####Play the Game
1211
* For latest version get
13-
* [v1.8](https://github.com/redomar/JavaGame/releases/tag/v1.8)
12+
* [![GitHub release](https://img.shields.io/github/release/redomar/JavaGame.svg?style=flat-square&label=Alpha)](https://github.com/redomar/JavaGame/releases/latest)
1413
* For multiplayer enabled get
15-
* [v1.5.4](https://github.com/redomar/JavaGame/releases/tag/v1.5.4)
14+
* [![GitHub release](https://img.shields.io/badge/Alpha-v1.5.4-cc0000.svg)](https://github.com/redomar/JavaGame/releases/v1.5.4)
1615

1716
#####Your version naming is all wrong
1817
Yes, I recently noticed that there is a standard called Semantic Versioning that I should follow. Currently my project isn't organised as well as I hoped so starting from the Beta I will follow Semantic Versioning schema.
@@ -36,3 +35,5 @@ Watch this video [here](http://youtu.be/_3nCgac3KKM) or checkout the [GitHub Pag
3635
* Commit your changes (```git commit -m "Change Title"```)
3736
* Push to the branch (```git push origin my_branch```)
3837
* Open a [Pull Request](https://github.com/redomar/JavaGame/pull/new/master)
38+
39+
Inspired from [vanZeben](https://github.com/vanZeben).

build.gradle

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@ repositories{
44

55
apply plugin: 'java'
66

7-
sourceSets.main.java.srcDir 'src'
8-
sourceSets.main.resources.srcDirs 'res'
7+
sourceSets{
8+
main{
9+
java.srcDir 'src'
10+
resources.srcDir 'res'
11+
}
12+
test{
13+
java.srcDir 'test'
14+
}
15+
}
916

1017
dependencies{
1118
compile 'org.apache.commons:commons-lang3:3.4'
12-
compile 'javazoom:jlayer:1.0.1'
13-
compile 'junit:junit:4.12'
19+
compile('com.googlecode.soundlibs:mp3spi:1.9.5-1'){
20+
exclude module: 'junit'
21+
}
22+
testCompile 'junit:junit:4.12'
1423
compile files('res/jars/JSplashScreen.jar')
1524
}
1625

res/sfx/smallProjectile.wav

9.95 KB
Binary file not shown.

src/com/redomar/game/Game.java

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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;
67
import com.redomar.game.gfx.Screen;
78
import com.redomar.game.gfx.SpriteSheet;
89
import com.redomar.game.level.LevelHandler;
910
import com.redomar.game.lib.Font;
10-
import com.redomar.game.lib.Music;
1111
import com.redomar.game.lib.Time;
1212
import com.redomar.game.script.PrintTypes;
1313
import com.redomar.game.script.Printing;
@@ -24,7 +24,7 @@ public class Game extends Canvas implements Runnable {
2424

2525
// Setting the size and name of the frame/canvas
2626
private static final long serialVersionUID = 1L;
27-
private static final String game_Version = "v1.8.2 Alpha";
27+
private static final String game_Version = "v1.8.3 Alpha";
2828
private static final int WIDTH = 160;
2929
private static final int HEIGHT = (WIDTH / 3 * 2);
3030
private static final int SCALE = 3;
@@ -47,7 +47,7 @@ public class Game extends Canvas implements Runnable {
4747
private static boolean closingMode;
4848

4949
private static JFrame frame;
50-
50+
private static AudioHandler backgroundMusic;
5151
private static boolean running = false;
5252
private static InputHandler input;
5353
private static MouseHandler mouse;
@@ -66,18 +66,15 @@ public class Game extends Canvas implements Runnable {
6666
private Player player;
6767
private Dummy dummy;
6868
private Vendor vendor;
69-
private Music music = new Music();
7069
private Font font = new Font();
71-
private Thread musicThread = new Thread(music, "MUSIC");
7270
private String nowPlaying;
7371
private boolean notActive = true;
74-
private boolean noAudioDevice = false;
7572
private int trigger = 0;
7673
private Printing print = new Printing();
7774

7875
/**
7976
* @author Redomar
80-
* @version Alpha 1.8.2
77+
* @version Alpha 1.8.3
8178
*/
8279
public Game() {
8380
context = InputContext.getInstance();
@@ -264,14 +261,22 @@ public static void setAlternateCols(boolean[] alternateCols) {
264261
Game.alternateCols = alternateCols;
265262
}
266263

267-
public static void setAternateColsR(boolean alternateCols) {
264+
public static void setAlternateColsR(boolean alternateCols) {
268265
Game.alternateCols[1] = alternateCols;
269266
}
270267

271-
public static void setAternateColsS(boolean alternateCols) {
268+
public static void setAlternateColsS(boolean alternateCols) {
272269
Game.alternateCols[0] = alternateCols;
273270
}
274271

272+
public static void setBackgroundMusic(AudioHandler backgroundMusic) {
273+
Game.backgroundMusic = backgroundMusic;
274+
}
275+
276+
public static AudioHandler getBackgroundMusic(){
277+
return Game.backgroundMusic;
278+
}
279+
275280
public static InputHandler getInput() {
276281
return input;
277282
}
@@ -425,22 +430,6 @@ public void render() {
425430
}
426431
}
427432

428-
if (!noAudioDevice) {
429-
if (input.isPlayMusic() && notActive == true) {
430-
int musicOption = JOptionPane.showConfirmDialog(this,
431-
"You are about to turn on music and can be VERY loud",
432-
"Music Options", 2, 2);
433-
if (musicOption == 0) {
434-
musicThread.start();
435-
notActive = false;
436-
} else {
437-
// System.out.println("[GAME] Canceled music option");
438-
print.print(" Canceled music option", PrintTypes.GAME);
439-
input.setPlayMusic(false);
440-
}
441-
}
442-
}
443-
444433
if (isChangeLevel() == true && getTickCount() % 60 == 0) {
445434
Game.setChangeLevel(true);
446435
setChangeLevel(false);
@@ -478,7 +467,7 @@ public void render() {
478467
g.drawString(
479468
"Welcome "
480469
+ WordUtils.capitalizeFully(player
481-
.getSantizedUsername()), 3, getHeight() - 17);
470+
.getSanitisedUsername()), 3, getHeight() - 17);
482471
g.setColor(Color.ORANGE);
483472

484473
if (context.getLocale().getCountry().equals("BE")
@@ -491,35 +480,9 @@ public void render() {
491480
}
492481
g.setColor(Color.YELLOW);
493482
g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3));
494-
g.setColor(Color.WHITE);
495-
if (noAudioDevice == true) {
496-
g.setColor(Color.RED);
497-
g.drawString("MUSIC is OFF | no audio device for playback", 3,
498-
getHeight() - 3);
499-
trigger++;
500-
if (trigger == 25) {
501-
JOptionPane.showMessageDialog(this, "No Audio device found",
502-
"Audio Issue", 0);
503-
}
504-
} else if (notActive == true) {
505-
g.setColor(Color.RED);
506-
g.drawString("MUSIC is OFF | press 'M' to start", 3,
507-
getHeight() - 3);
508-
} else {
509-
g.setColor(Color.GREEN);
510-
g.drawString("MUSIC is ON | You cannot turn off the music", 3,
511-
getHeight() - 3);
512-
g.setColor(Color.WHITE);
513-
setNowPlaying(WordUtils.capitalize(music.getSongName()[music
514-
.getSongNumber()].substring(7,
515-
(music.getSongName()[music.getSongNumber()].length() - 4))));
516-
if (getNowPlaying().startsWith("T")) {
517-
g.drawString(nowPlaying, getWidth() - (nowPlaying.length() * 9)
518-
+ 12, getHeight() - 17);
519-
} else {
520-
g.drawString(nowPlaying, getWidth() - (nowPlaying.length() * 9)
521-
+ 8, getHeight() - 17);
522-
}
483+
g.setColor(Color.GREEN);
484+
if(backgroundMusic.getActive()) {
485+
g.drawString("MUSIC is ON ", 3, getHeight() - 3);
523486
}
524487
g.dispose();
525488
bs.show();

src/com/redomar/game/InputHandler.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class InputHandler implements KeyListener {
1717
private Key left = new Key();
1818
private Key right = new Key();
1919
private Printing print = new Printing();
20-
private boolean PlayMusic = false;
2120
private int map;
2221
private boolean ignoreInput = false;
2322
private PopUp popup = new PopUp();
@@ -69,10 +68,16 @@ private void toggleKey(int keyCode, boolean isPressed) {
6968
left.toggle(false);
7069
right.toggle(false);
7170
}
71+
7272
if (keyCode == KeyEvent.VK_M) {
73-
this.setPlayMusic(true);
73+
Game.getBackgroundMusic().play();
74+
}
75+
76+
if (keyCode == KeyEvent.VK_COMMA) {
77+
Game.getBackgroundMusic().stop();
7478
}
7579

80+
7681
if (keyCode == KeyEvent.VK_W && isAzertyCountry || keyCode == KeyEvent.VK_Z && !isAzertyCountry) {
7782
// if (map == 0){
7883
// Game.getGame().setMap("/levels/water_level.png");
@@ -124,13 +129,14 @@ private void toggleKey(int keyCode, boolean isPressed) {
124129

125130
private void quitGame() {
126131
Game.setClosing(true);
132+
print.removeLog();
127133
try {
128134
Thread.sleep(1000);
129135
} catch (InterruptedException e) {
130136
e.printStackTrace();
131137
}
132138
Game.getLevel().removeEntity(
133-
Game.getPlayer().getSantizedUsername());
139+
Game.getPlayer().getSanitisedUsername());
134140
Game.setRunning(false);
135141
Game.getFrame().dispose();
136142
System.exit(1);
@@ -148,14 +154,6 @@ public void setMap(int map) {
148154
this.map = map;
149155
}
150156

151-
boolean isPlayMusic() {
152-
return PlayMusic;
153-
}
154-
155-
void setPlayMusic(boolean playMusic) {
156-
PlayMusic = playMusic;
157-
}
158-
159157
public Key getUp() {
160158
return up;
161159
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.redomar.game.audio;
2+
3+
import javax.sound.sampled.*;
4+
5+
/**
6+
* For uncompressed files like .wav
7+
*/
8+
public class AudioEffect{
9+
10+
private Clip clip;
11+
private boolean active = false;
12+
13+
public AudioEffect(String path){
14+
try{
15+
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
16+
AudioFormat baseformat = audioInputStream.getFormat();
17+
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(
18+
baseformat, audioInputStream);
19+
clip = AudioSystem.getClip();
20+
clip.open(decodedAudioInputStream);
21+
} catch (Exception e){
22+
System.err.println(e.getStackTrace());
23+
}
24+
}
25+
26+
public void play(){
27+
if(clip == null) return;
28+
stop();
29+
clip.setFramePosition(0);
30+
clip.start();
31+
active = true;
32+
}
33+
34+
public void setVolume(float velocity){
35+
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
36+
volume.setValue(velocity);
37+
38+
}
39+
40+
public void stop() {
41+
if (clip.isRunning()) clip.stop();
42+
active = false;
43+
}
44+
45+
public void close(){
46+
stop();
47+
clip.close();
48+
}
49+
50+
public boolean getActive(){
51+
return this.active;
52+
}
53+
54+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.redomar.game.audio;
2+
3+
import javax.sound.sampled.*;
4+
5+
6+
public class AudioHandler {
7+
8+
private Clip clip;
9+
private boolean active = false;
10+
11+
public AudioHandler(String path){
12+
try{
13+
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
14+
AudioFormat baseformat = audioInputStream.getFormat();
15+
AudioFormat decodeFormat = new AudioFormat(
16+
AudioFormat.Encoding.PCM_SIGNED,
17+
baseformat.getSampleRate(), 16,
18+
baseformat.getChannels(),
19+
baseformat.getChannels() * 2,
20+
baseformat.getSampleRate(),
21+
false
22+
);
23+
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(
24+
decodeFormat, audioInputStream);
25+
clip = AudioSystem.getClip();
26+
clip.open(decodedAudioInputStream);
27+
} catch (Exception e){
28+
System.err.println(e.getStackTrace());
29+
}
30+
}
31+
32+
public void play(){
33+
if(clip == null) return;
34+
stop();
35+
clip.setFramePosition(0);
36+
clip.start();
37+
active = true;
38+
}
39+
40+
public void setVolume(float velocity){
41+
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
42+
volume.setValue(velocity);
43+
44+
}
45+
46+
public void stop() {
47+
if (clip.isRunning()) clip.stop();
48+
active = false;
49+
}
50+
51+
public void close(){
52+
stop();
53+
clip.close();
54+
}
55+
56+
public boolean getActive(){
57+
return this.active;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)