Skip to content
Snippets Groups Projects
Commit a18f8680 authored by Rebekka Aashaug Stangvik's avatar Rebekka Aashaug Stangvik
Browse files

added backgroundmusic and logic to control it

parent d8db86f8
No related branches found
No related tags found
1 merge request!19Resolve "Add settings button"
File added
assets/bgWithSettings.png

56.3 KiB

......@@ -10,6 +10,8 @@ import com.wordbattle.game.network.FirebaseInterface;
import com.wordbattle.game.states.MainMenuState;
import com.wordbattle.game.states.StateManager;
import manager.MusicManager;
public class WordBattle extends ApplicationAdapter {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
......@@ -28,6 +30,8 @@ public class WordBattle extends ApplicationAdapter {
stateManager = new StateManager();
ScreenUtils.clear(1, 0, 0, 1);
stateManager.setState(new MainMenuState(stateManager, _FBIC)); // Set the main menu as the initial state
MusicManager.initialize();
MusicManager.playMusic();
}
@Override
public void render() {
......
......@@ -10,6 +10,8 @@ import com.wordbattle.game.states.SettingsState;
import com.wordbattle.game.view.MainMenuView;
import com.wordbattle.game.view.SettingsView;
import manager.MusicManager;
public class SettingsController {
private SettingsState state;
private SettingsView settingsView;
......@@ -34,6 +36,10 @@ public class SettingsController {
Rectangle switchMusicBounds = settingsView.getSwitchMusicBounds();
Rectangle switchSoundBounds = settingsView.getSwitchSoundBounds();
//test with manager
if (switchMusicBounds.contains(touchPos.x, touchPos.y)) {
MusicManager.toggleMusic();
}
if (goBackButtonBounds.contains(touchPos.x, touchPos.y)) {
System.out.println("Go back button is pressed");
......@@ -45,10 +51,7 @@ public class SettingsController {
}
if (switchMusicBounds.contains(touchPos.x, touchPos.y)) {
settingsView.toggleSwitch();
//TODO
}
if (switchSoundBounds.contains(touchPos.x, touchPos.y)) {
settingsView.toggleSwitchSound();
......
......@@ -9,6 +9,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.wordbattle.game.WordBattle;
import manager.MusicManager;
public class SettingsView {
......@@ -85,9 +86,11 @@ public class SettingsView {
spriteBatch.draw(backgroundTexture, 0, 0, WordBattle.WIDTH, WordBattle.HEIGHT);
spriteBatch.draw(quitGameTexture, quitGameButtonBounds.x, quitGameButtonBounds.y,
quitGameButtonBounds.width, quitGameButtonBounds.height);
Texture switchMusicTexture = switchMusicState ? switchOnMusicTexture : switchOffMusicTexture;
//test with manager
Texture switchMusicTexture = MusicManager.isMusicOn() ? switchOnMusicTexture : switchOffMusicTexture;
spriteBatch.draw(switchMusicTexture, switchMusicBounds.x, switchMusicBounds.y,
switchMusicBounds.width, switchMusicBounds.height);
Texture switchSoundTexture = switchSoundState ? switchOnSoundTexture : switchOffSoundTexture;
spriteBatch.draw(switchSoundTexture, switchSoundBounds.x, switchSoundBounds.y,
switchSoundBounds.width, switchSoundBounds.height);
......
package manager;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
public class MusicManager {
private static Music backgroundMusic;
private static boolean isMusicOn = true;
public static void initialize() {
backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("bgMusic.mp3"));
backgroundMusic.setLooping(true);
}
public static void playMusic() {
if (backgroundMusic != null && !backgroundMusic.isPlaying() && isMusicOn) {
backgroundMusic.play();
}
}
public static void stopMusic() {
if (backgroundMusic != null && backgroundMusic.isPlaying()) {
backgroundMusic.stop();
}
}
public static void toggleMusic() {
isMusicOn = !isMusicOn;
if (isMusicOn) {
playMusic();
} else {
stopMusic();
}
}
public static void dispose() {
if (backgroundMusic != null) {
backgroundMusic.dispose();
}
}
public static boolean isMusicOn() {
return isMusicOn;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment