Skip to content

Commit

Permalink
Finished adding the MusicManager that SimonC4 starting with menu and …
Browse files Browse the repository at this point in the history
…ingame music.
  • Loading branch information
Rulasmur committed Mar 12, 2016
1 parent b3c6cfb commit 67e431d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
6 changes: 6 additions & 0 deletions main/src/org/destinationsol/SolApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.destinationsol.common.SolMath;
import org.destinationsol.game.DebugOptions;
import org.destinationsol.game.SolGame;
import org.destinationsol.game.sound.MusicManager;
import org.destinationsol.menu.MenuScreens;
import org.destinationsol.ui.*;

Expand Down Expand Up @@ -56,10 +57,13 @@ public SolApplication() {

@Override
public void create() {

myReallyMobile = Gdx.app.getType() == Application.ApplicationType.Android || Gdx.app.getType() == Application.ApplicationType.iOS;
if (myReallyMobile) DebugOptions.read(null);
myOptions = new GameOptions(isMobile(), null);

MusicManager.getInstance().PlayMenuMusic(myOptions);

myTextureManager = new TextureManager();
myCommonDrawer = new CommonDrawer();
myUiDrawer = new UiDrawer(myTextureManager, myCommonDrawer);
Expand Down Expand Up @@ -151,11 +155,13 @@ public void loadNewGame(boolean tut, boolean usePrevShip) {
if (myGame != null) throw new AssertionError("Starting a new game with unfinished current one");
myInputMan.setScreen(this, myMenuScreens.loading);
myMenuScreens.loading.setMode(tut, usePrevShip);
MusicManager.getInstance().PlayGameMusic(myOptions);
}

public void startNewGame(boolean tut, boolean usePrevShip) {
myGame = new SolGame(this, usePrevShip, myTextureManager, tut, myCommonDrawer);
myInputMan.setScreen(this, myGame.getScreens().mainScreen);
MusicManager.getInstance().PlayGameMusic(myOptions);
}

public SolInputManager getInputMan() {
Expand Down
119 changes: 119 additions & 0 deletions main/src/org/destinationsol/game/sound/MusicManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2015-2016 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.destinationsol.game.sound;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import org.destinationsol.GameOptions;
import org.destinationsol.files.FileManager;

import java.util.ArrayList;

/**
* Singleton class that is responsible for playing all music throughout the game.
*/
public final class MusicManager {
private static MusicManager instance = null;
private static final String DIR = "res/sounds/";
private final Music menuMusic;
private ArrayList<Music> gameMusic = new ArrayList<Music>();;
private Music currentlyPlaying = null;

/**
* Returns the singleton instance of this class.
* @return The instance.
*/
public static MusicManager getInstance() {
if(instance == null) {
instance = new MusicManager();
}

return instance;
}

/**
* Initalise the MusicManager class.
*/
private MusicManager() {
menuMusic = Gdx.audio.newMusic(FileManager.getInstance().getStaticFile("res/sounds/music/dreadnaught.ogg"));
gameMusic.add(Gdx.audio.newMusic(FileManager.getInstance().getStaticFile("res/sounds/music/cimmerian dawn.ogg")));
gameMusic.add(Gdx.audio.newMusic(FileManager.getInstance().getStaticFile("res/sounds/music/into the dark.ogg")));
gameMusic.add(Gdx.audio.newMusic(FileManager.getInstance().getStaticFile("res/sounds/music/space theatre.ogg")));
menuMusic.setLooping(true);
}

/**
* Start playing the music menu from the beginning of the track. The menu music loops continuously.
*/
public void PlayMenuMusic(GameOptions options) {
if(currentlyPlaying != null )
{
if(currentlyPlaying != menuMusic || (currentlyPlaying == menuMusic && !currentlyPlaying.isPlaying()))
{
StopMusic();
playMusic(menuMusic, options);
}
}else
{
StopMusic();
playMusic(menuMusic, options);
}

}

public void PlayGameMusic(final GameOptions options) {
StopMusic();
if(currentlyPlaying != null && gameMusic.contains(currentlyPlaying))
{
int index = gameMusic.indexOf(currentlyPlaying) +1;
if(gameMusic.size()-1 >= index)
{
playMusic(gameMusic.get(index), options);
currentlyPlaying.setOnCompletionListener(new Music.OnCompletionListener() {
@Override
public void onCompletion(Music music) {
PlayGameMusic(options);
}
});

}else
{
playMusic(gameMusic.get(0), options);
}
}else
{
playMusic(gameMusic.get(0), options);
}
}

public void playMusic(Music music, GameOptions options)
{
currentlyPlaying = music;
currentlyPlaying.setVolume(options.volMul);
currentlyPlaying.play();
System.out.println("playing music");
}
/**
* Stop playing all music.
*/
public void StopMusic() {
if(currentlyPlaying != null)
{
currentlyPlaying.stop();
}
}
}
5 changes: 4 additions & 1 deletion main/src/org/destinationsol/menu/MainScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.destinationsol.TextureManager;
import org.destinationsol.common.SolColor;
import org.destinationsol.game.DebugOptions;
import org.destinationsol.game.sound.MusicManager;
import org.destinationsol.ui.SolInputManager;
import org.destinationsol.ui.SolUiControl;
import org.destinationsol.ui.SolUiScreen;
Expand All @@ -45,10 +46,12 @@ public class MainScreen implements SolUiScreen {
private final SolUiControl myCreditsCtrl;
private final TextureAtlas.AtlasRegion myTitleTex;
private final boolean isMobile;
GameOptions gameOptions;

public MainScreen(MenuLayout menuLayout, TextureManager textureManager, boolean mobile, float r, GameOptions gameOptions) {
isMobile = mobile;
myControls = new ArrayList<SolUiControl>();
this.gameOptions = gameOptions;

myTutCtrl = new SolUiControl(menuLayout.buttonRect(-1, 1), true, Input.Keys.T);
myTutCtrl.setDisplayName("Tutorial");
Expand Down Expand Up @@ -123,7 +126,7 @@ public boolean isCursorOnBg(SolInputManager.Ptr ptr) {

@Override
public void onAdd(SolApplication cmp) {

MusicManager.getInstance().PlayMenuMusic(gameOptions);
}

@Override
Expand Down

0 comments on commit 67e431d

Please sign in to comment.