Skip to content

Commit

Permalink
audio sounds can now be played even if already playing.
Browse files Browse the repository at this point in the history
  • Loading branch information
asalga committed Dec 2, 2013
1 parent 54392fc commit 1097dd0
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 27 deletions.
13 changes: 11 additions & 2 deletions BoardModel.pde
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class BoardModel{

private int numGemsAllowedAtOnce;
private int numGemsOnBoard;
private ScreenGameplay screenGameplay;

/*
Instead of the board knowing how to draw itself, it
Expand Down Expand Up @@ -48,7 +49,8 @@ public class BoardModel{

/*
*/
public BoardModel(){
public BoardModel(ScreenGameplay s){
screenGameplay = s;
numGemsOnBoard = 0;
numGemsAllowedAtOnce = 0;
board = new Token[BOARD_ROWS][BOARD_COLS];
Expand Down Expand Up @@ -433,7 +435,9 @@ public class BoardModel{

if(numTokensArrivedAtDest > 0){
markTokensForRemoval(START_ROW_INDEX, BOARD_ROWS-1);
removeMarkedTokens(true);
if(removeMarkedTokens(true) > 2){
screenGameplay.test();
}
dropTokens();
}
}
Expand Down Expand Up @@ -572,6 +576,7 @@ public class BoardModel{
board[r][c].setSelect(false);
}
}

return numRemoved;
}

Expand All @@ -580,6 +585,10 @@ public class BoardModel{
public void setNumGemsAllowedAtOnce(int num){
numGemsAllowedAtOnce = num;
}

public void reduceGemCount(int i){
numGemsOnBoard -= i;
}

public int getNumGems(){
return numGemsOnBoard;
Expand Down
3 changes: 3 additions & 0 deletions Horadrix.pde
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ public void keyReleased(){
screens.curr.keyReleased();
}

public void stop(){
println("STOP TEST");
}
42 changes: 28 additions & 14 deletions ScreenGameplay.pde
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class ScreenGameplay implements IScreen, Subject{

private float opacity = 0.0;

public void test(){
//soundManager.playMatchSound();
}

/**
*/
public void addObserver(LayerObserver o){
Expand Down Expand Up @@ -78,7 +82,7 @@ public class ScreenGameplay implements IScreen, Subject{
ScreenGameplay(){
timer = new Ticker();

allowInputWhenTokensFalling = false;
allowInputWhenTokensFalling = true;

LayerObserver hudLayer = new HUDLayer(this);

Expand All @@ -98,7 +102,9 @@ public class ScreenGameplay implements IScreen, Subject{
Layer hudLayer = new HUDLayer();
observers.add(hudLayer);*/

boardModel = new BoardModel();
Keyboard.lockKeys(new int[]{KEY_M});

boardModel = new BoardModel(this);
boardModel.setNumGemsAllowedAtOnce(2);

debug = new Debugger();
Expand Down Expand Up @@ -241,7 +247,9 @@ public class ScreenGameplay implements IScreen, Subject{

debug.clear();

levelCountDownTimer.tick();
if(levelCountDownTimer != null){
levelCountDownTimer.tick();
}

// Now, update the two tokens that the user has swapped
if(swapToken1 != null){
Expand Down Expand Up @@ -273,7 +281,7 @@ public class ScreenGameplay implements IScreen, Subject{
swapToken1.setReturning(true);
swapToken2.setReturning(true);

soundManager.playFailSwapSound();
//soundManager.playFailSwapSound();
}
// Swap was valid
else{
Expand All @@ -282,9 +290,11 @@ public class ScreenGameplay implements IScreen, Subject{
int test = boardModel.markTokensForRemoval(8, 15);

boardModel.removeMarkedTokens(true);
soundManager.playMatchSound();

boardModel.dropTokens();
deselectCurrentTokens();
// !!!

//soundManager.playSuccessSwapSound();
}
}
Expand Down Expand Up @@ -318,7 +328,7 @@ public class ScreenGameplay implements IScreen, Subject{

if(dyingToken.hasGem()){
gemsWonByPlayer++;
//numGemsOnBoard--;
boardModel.reduceGemCount(1);
}

addToScore(dyingToken.getScore());
Expand Down Expand Up @@ -545,25 +555,29 @@ public class ScreenGameplay implements IScreen, Subject{
}

if(Keyboard.isKeyDown(KEY_L)){
gemsWonByPlayer = 50;
//gemsWonByPlayer = 50;
soundManager.playMatchSound();
//goToNextLevel();
}

if(Keyboard.isKeyDown(KEY_M)){
soundManager.setMute(!soundManager.isMuted());
}

if(Keyboard.isKeyDown(KEY_S)){
if(currToken1 != null){
currToken1.setHasGem(true);
//if(currToken1 != null){
// currToken1.setHasGem(true);
//setType(6);
}
//}
soundManager.playFailSwapSound();
}
}

/**
*/
void keyReleased(){
Keyboard.setKeyDown(keyCode, false);

soundManager.setMute(!soundManager.isMuted());


isPaused = Keyboard.isKeyDown(KEY_P);
if(isPaused == false){
timer.resume();
Expand Down Expand Up @@ -627,7 +641,7 @@ public class ScreenGameplay implements IScreen, Subject{
}

// Easy way to clear the dying tokens which we don't want animating in the next level.
boardModel = new BoardModel();
boardModel = new BoardModel(this);
boardModel.setNumGemsAllowedAtOnce(2);

boardModel.generateNewBoardWithDyingAnimation(false);
Expand Down
2 changes: 1 addition & 1 deletion ScreenSet.pde
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ScreenSet{
curr = s;
}

/**
/*
TODO: add awesome transition effect
*/
public void transitionTo(String s){
Expand Down
2 changes: 1 addition & 1 deletion ScreenSplash.pde
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ScreenSplash implements IScreen{

public void update(){
ticker.tick();
if(ticker.getTotalTime() > 0.5f){
if(ticker.getTotalTime() > 0.01f){
//screens.transitionTo("gameplay");
screens.transitionTo("story");
}
Expand Down
2 changes: 2 additions & 0 deletions ScreenStory.pde
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ScreenStory implements IScreen{

public void update(){

screens.transitionTo("gameplay");

if(extro == false){
textPos += (target - textPos) * easing;
}
Expand Down
69 changes: 60 additions & 9 deletions SoundManager.pde
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,62 @@ public class SoundManager{
boolean muted = false;
Minim minim;

AudioPlayer failSwap;
AudioPlayer successSwap;

PlayerQueue matchPlayer;
PlayerQueue successSwapPlayer;
PlayerQueue failSwapPlayer;

/*
Handles the issue where we want to play multiple audio streams from the same clip.
*/
private class PlayerQueue{
private ArrayList <AudioPlayer> players;
private String path;

public PlayerQueue(String audioPath){
path = audioPath;
players = new ArrayList<AudioPlayer>();
appendPlayer();
}

public void close(){
for(int i = 0; i < players.size(); i++){
players.get(i).close();
}
}

public void play(){
int freePlayerIndex = -1;
for(int i = 0; i < players.size(); i++){
if(players.get(i).isPlaying() == false){
freePlayerIndex = i;
break;
}
}

if(freePlayerIndex == -1){
appendPlayer();
freePlayerIndex = players.size()-1;
}

players.get(freePlayerIndex).play();
players.get(freePlayerIndex).rewind();
}

private void appendPlayer(){
AudioPlayer player = minim.loadFile(path);
players.add(player);
}
}

public void init(){
}

public SoundManager(PApplet applet){
minim = new Minim(applet);

failSwap = minim.loadFile("audio/fail_swap.wav");
successSwap = minim.loadFile("audio/success_swap.wav");
successSwapPlayer = new PlayerQueue("audio/success_swap.wav");
failSwapPlayer = new PlayerQueue("audio/fail_swap.wav");
matchPlayer = new PlayerQueue("audio/peg.wav");
}

public void setMute(boolean isMuted){
Expand All @@ -40,16 +84,23 @@ public class SoundManager{
}

public void playSuccessSwapSound(){
play(successSwap);
successSwapPlayer.play();
}

public void playMatchSound(){
matchPlayer.play();
}

public void playFailSwapSound(){
play(failSwap);
failSwapPlayer.play();
}

public void stop(){
// dropPiece.close();
// minim.stop();
failSwapPlayer.close();
successSwapPlayer.close();
matchPlayer.close();
minim.stop();

// super.stop();
}
}
Binary file added data/audio/match.ogg
Binary file not shown.
Binary file added data/audio/match.wav
Binary file not shown.
Empty file added sm.pde
Empty file.

0 comments on commit 1097dd0

Please sign in to comment.