Skip to content

Commit

Permalink
Alpha 1.8.3 - New Audio Manager
Browse files Browse the repository at this point in the history
Final
  • Loading branch information
redomar authored Aug 28, 2016
2 parents c680658 + 9664b4a commit 26bed52
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 191 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ language: java
# blocklist
branches:
except:
- develop
- unitTesting
- develop
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
JavaGame Alpha v1.8.2 [![Travis-CI Build Status](https://travis-ci.org/redomar/JavaGame.svg?branch=master)](https://travis-ci.org/redomar/JavaGame)
=====================
Acknowledgement: Inspired from [vanZeben](https://github.com/vanZeben)
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)
==

#####What is JavaGame?
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.
Expand All @@ -10,9 +9,9 @@ Well i'm still not sure what exactly i'm going to do with it, and I haven't thou

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

#####Your version naming is all wrong
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.
Expand All @@ -36,3 +35,5 @@ Watch this video [here](http://youtu.be/_3nCgac3KKM) or checkout the [GitHub Pag
* Commit your changes (```git commit -m "Change Title"```)
* Push to the branch (```git push origin my_branch```)
* Open a [Pull Request](https://github.com/redomar/JavaGame/pull/new/master)

Inspired from [vanZeben](https://github.com/vanZeben).
17 changes: 13 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ repositories{

apply plugin: 'java'

sourceSets.main.java.srcDir 'src'
sourceSets.main.resources.srcDirs 'res'
sourceSets{
main{
java.srcDir 'src'
resources.srcDir 'res'
}
test{
java.srcDir 'test'
}
}

dependencies{
compile 'org.apache.commons:commons-lang3:3.4'
compile 'javazoom:jlayer:1.0.1'
compile 'junit:junit:4.12'
compile('com.googlecode.soundlibs:mp3spi:1.9.5-1'){
exclude module: 'junit'
}
testCompile 'junit:junit:4.12'
compile files('res/jars/JSplashScreen.jar')
}

Expand Down
Binary file added res/sfx/smallProjectile.wav
Binary file not shown.
73 changes: 18 additions & 55 deletions src/com/redomar/game/Game.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.redomar.game;

import com.redomar.game.audio.AudioHandler;
import com.redomar.game.entities.Dummy;
import com.redomar.game.entities.Player;
import com.redomar.game.entities.Vendor;
import com.redomar.game.gfx.Screen;
import com.redomar.game.gfx.SpriteSheet;
import com.redomar.game.level.LevelHandler;
import com.redomar.game.lib.Font;
import com.redomar.game.lib.Music;
import com.redomar.game.lib.Time;
import com.redomar.game.script.PrintTypes;
import com.redomar.game.script.Printing;
Expand All @@ -24,7 +24,7 @@ public class Game extends Canvas implements Runnable {

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

private static JFrame frame;

private static AudioHandler backgroundMusic;
private static boolean running = false;
private static InputHandler input;
private static MouseHandler mouse;
Expand All @@ -66,18 +66,15 @@ public class Game extends Canvas implements Runnable {
private Player player;
private Dummy dummy;
private Vendor vendor;
private Music music = new Music();
private Font font = new Font();
private Thread musicThread = new Thread(music, "MUSIC");
private String nowPlaying;
private boolean notActive = true;
private boolean noAudioDevice = false;
private int trigger = 0;
private Printing print = new Printing();

/**
* @author Redomar
* @version Alpha 1.8.2
* @version Alpha 1.8.3
*/
public Game() {
context = InputContext.getInstance();
Expand Down Expand Up @@ -264,14 +261,22 @@ public static void setAlternateCols(boolean[] alternateCols) {
Game.alternateCols = alternateCols;
}

public static void setAternateColsR(boolean alternateCols) {
public static void setAlternateColsR(boolean alternateCols) {
Game.alternateCols[1] = alternateCols;
}

public static void setAternateColsS(boolean alternateCols) {
public static void setAlternateColsS(boolean alternateCols) {
Game.alternateCols[0] = alternateCols;
}

public static void setBackgroundMusic(AudioHandler backgroundMusic) {
Game.backgroundMusic = backgroundMusic;
}

public static AudioHandler getBackgroundMusic(){
return Game.backgroundMusic;
}

public static InputHandler getInput() {
return input;
}
Expand Down Expand Up @@ -425,22 +430,6 @@ public void render() {
}
}

if (!noAudioDevice) {
if (input.isPlayMusic() && notActive == true) {
int musicOption = JOptionPane.showConfirmDialog(this,
"You are about to turn on music and can be VERY loud",
"Music Options", 2, 2);
if (musicOption == 0) {
musicThread.start();
notActive = false;
} else {
// System.out.println("[GAME] Canceled music option");
print.print(" Canceled music option", PrintTypes.GAME);
input.setPlayMusic(false);
}
}
}

if (isChangeLevel() == true && getTickCount() % 60 == 0) {
Game.setChangeLevel(true);
setChangeLevel(false);
Expand Down Expand Up @@ -478,7 +467,7 @@ public void render() {
g.drawString(
"Welcome "
+ WordUtils.capitalizeFully(player
.getSantizedUsername()), 3, getHeight() - 17);
.getSanitisedUsername()), 3, getHeight() - 17);
g.setColor(Color.ORANGE);

if (context.getLocale().getCountry().equals("BE")
Expand All @@ -491,35 +480,9 @@ public void render() {
}
g.setColor(Color.YELLOW);
g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3));
g.setColor(Color.WHITE);
if (noAudioDevice == true) {
g.setColor(Color.RED);
g.drawString("MUSIC is OFF | no audio device for playback", 3,
getHeight() - 3);
trigger++;
if (trigger == 25) {
JOptionPane.showMessageDialog(this, "No Audio device found",
"Audio Issue", 0);
}
} else if (notActive == true) {
g.setColor(Color.RED);
g.drawString("MUSIC is OFF | press 'M' to start", 3,
getHeight() - 3);
} else {
g.setColor(Color.GREEN);
g.drawString("MUSIC is ON | You cannot turn off the music", 3,
getHeight() - 3);
g.setColor(Color.WHITE);
setNowPlaying(WordUtils.capitalize(music.getSongName()[music
.getSongNumber()].substring(7,
(music.getSongName()[music.getSongNumber()].length() - 4))));
if (getNowPlaying().startsWith("T")) {
g.drawString(nowPlaying, getWidth() - (nowPlaying.length() * 9)
+ 12, getHeight() - 17);
} else {
g.drawString(nowPlaying, getWidth() - (nowPlaying.length() * 9)
+ 8, getHeight() - 17);
}
g.setColor(Color.GREEN);
if(backgroundMusic.getActive()) {
g.drawString("MUSIC is ON ", 3, getHeight() - 3);
}
g.dispose();
bs.show();
Expand Down
20 changes: 9 additions & 11 deletions src/com/redomar/game/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class InputHandler implements KeyListener {
private Key left = new Key();
private Key right = new Key();
private Printing print = new Printing();
private boolean PlayMusic = false;
private int map;
private boolean ignoreInput = false;
private PopUp popup = new PopUp();
Expand Down Expand Up @@ -69,10 +68,16 @@ private void toggleKey(int keyCode, boolean isPressed) {
left.toggle(false);
right.toggle(false);
}

if (keyCode == KeyEvent.VK_M) {
this.setPlayMusic(true);
Game.getBackgroundMusic().play();
}

if (keyCode == KeyEvent.VK_COMMA) {
Game.getBackgroundMusic().stop();
}


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

private void quitGame() {
Game.setClosing(true);
print.removeLog();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Game.getLevel().removeEntity(
Game.getPlayer().getSantizedUsername());
Game.getPlayer().getSanitisedUsername());
Game.setRunning(false);
Game.getFrame().dispose();
System.exit(1);
Expand All @@ -148,14 +154,6 @@ public void setMap(int map) {
this.map = map;
}

boolean isPlayMusic() {
return PlayMusic;
}

void setPlayMusic(boolean playMusic) {
PlayMusic = playMusic;
}

public Key getUp() {
return up;
}
Expand Down
54 changes: 54 additions & 0 deletions src/com/redomar/game/audio/AudioEffect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.redomar.game.audio;

import javax.sound.sampled.*;

/**
* For uncompressed files like .wav
*/
public class AudioEffect{

private Clip clip;
private boolean active = false;

public AudioEffect(String path){
try{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
AudioFormat baseformat = audioInputStream.getFormat();
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(
baseformat, audioInputStream);
clip = AudioSystem.getClip();
clip.open(decodedAudioInputStream);
} catch (Exception e){
System.err.println(e.getStackTrace());
}
}

public void play(){
if(clip == null) return;
stop();
clip.setFramePosition(0);
clip.start();
active = true;
}

public void setVolume(float velocity){
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(velocity);

}

public void stop() {
if (clip.isRunning()) clip.stop();
active = false;
}

public void close(){
stop();
clip.close();
}

public boolean getActive(){
return this.active;
}

}
60 changes: 60 additions & 0 deletions src/com/redomar/game/audio/AudioHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.redomar.game.audio;

import javax.sound.sampled.*;


public class AudioHandler {

private Clip clip;
private boolean active = false;

public AudioHandler(String path){
try{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
AudioFormat baseformat = audioInputStream.getFormat();
AudioFormat decodeFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseformat.getSampleRate(), 16,
baseformat.getChannels(),
baseformat.getChannels() * 2,
baseformat.getSampleRate(),
false
);
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(
decodeFormat, audioInputStream);
clip = AudioSystem.getClip();
clip.open(decodedAudioInputStream);
} catch (Exception e){
System.err.println(e.getStackTrace());
}
}

public void play(){
if(clip == null) return;
stop();
clip.setFramePosition(0);
clip.start();
active = true;
}

public void setVolume(float velocity){
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(velocity);

}

public void stop() {
if (clip.isRunning()) clip.stop();
active = false;
}

public void close(){
stop();
clip.close();
}

public boolean getActive(){
return this.active;
}

}
Loading

0 comments on commit 26bed52

Please sign in to comment.