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

Added setting view,controller and state

parent 6fee2ad9
No related branches found
No related tags found
1 merge request!19Resolve "Add settings button"
assets/correctBg.png

87.6 KiB

assets/offButton.png

23 KiB

assets/onButton.png

24.5 KiB

assets/quitGameBtn.png

22 KiB

assets/settingsBg1.png

59.5 KiB

...@@ -12,6 +12,7 @@ import com.wordbattle.game.states.CreateGameState; ...@@ -12,6 +12,7 @@ import com.wordbattle.game.states.CreateGameState;
import com.wordbattle.game.states.JoinGameState; import com.wordbattle.game.states.JoinGameState;
import com.wordbattle.game.states.HowToPlayState; import com.wordbattle.game.states.HowToPlayState;
import com.wordbattle.game.states.MainMenuState; import com.wordbattle.game.states.MainMenuState;
import com.wordbattle.game.states.SettingsState;
import com.wordbattle.game.states.StartState; import com.wordbattle.game.states.StartState;
import com.wordbattle.game.states.StartingGameState; import com.wordbattle.game.states.StartingGameState;
import com.wordbattle.game.states.StateManager; import com.wordbattle.game.states.StateManager;
...@@ -46,11 +47,13 @@ public class MainMenuController { ...@@ -46,11 +47,13 @@ public class MainMenuController {
Rectangle joinGameButtonBounds = mainMenuView.getJoinGameButtonBounds(); Rectangle joinGameButtonBounds = mainMenuView.getJoinGameButtonBounds();
Rectangle newGameButtonBounds = mainMenuView.getNewGameButtonBounds(); Rectangle newGameButtonBounds = mainMenuView.getNewGameButtonBounds();
Rectangle howToPlayButtonBounds = mainMenuView.getHowToPlayButtonBounds(); Rectangle howToPlayButtonBounds = mainMenuView.getHowToPlayButtonBounds();
Rectangle settingsBounds = mainMenuView.getSettingsBounds();
// Debugging button bounds // Debugging button bounds
System.out.println("Join Game Button Bounds: " + joinGameButtonBounds.x +"," + joinGameButtonBounds.y); System.out.println("Join Game Button Bounds: " + joinGameButtonBounds.x +"," + joinGameButtonBounds.y);
System.out.println("New Game Button Bounds: " + newGameButtonBounds); System.out.println("New Game Button Bounds: " + newGameButtonBounds);
System.out.println("How to play Button Bounds: " + howToPlayButtonBounds); System.out.println("How to play Button Bounds: " + howToPlayButtonBounds);
System.out.println("How to play Button Bounds: " + howToPlayButtonBounds);
// Button checks // Button checks
if (joinGameButtonBounds.contains(touchPos.x, touchPos.y)) { if (joinGameButtonBounds.contains(touchPos.x, touchPos.y)) {
...@@ -67,6 +70,10 @@ public class MainMenuController { ...@@ -67,6 +70,10 @@ public class MainMenuController {
System.out.println("How to play Pressed"); System.out.println("How to play Pressed");
state.getStateManager().setState(new HowToPlayState(state.getStateManager(), _FBIC)); state.getStateManager().setState(new HowToPlayState(state.getStateManager(), _FBIC));
} }
if (settingsBounds.contains(touchPos.x, touchPos.y)) {
System.out.println("Settings Pressed");
state.getStateManager().setState(new SettingsState(state.getStateManager(), _FBIC));
}
} }
} }
......
package com.wordbattle.game.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.wordbattle.game.network.FirebaseInterface;
import com.wordbattle.game.states.MainMenuState;
import com.wordbattle.game.states.SettingsState;
import com.wordbattle.game.view.MainMenuView;
import com.wordbattle.game.view.SettingsView;
public class SettingsController {
private SettingsState state;
private SettingsView settingsView;
FirebaseInterface _FBIC;
public SettingsController(SettingsState state, FirebaseInterface _FBIC){
this.state = state;
this.settingsView = new SettingsView(state.getCam());
this._FBIC = _FBIC;
}
public void handleInput(){
if (Gdx.input.justTouched()) {
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
System.out.println("Screen touch: " + touchPos.x + ", " + touchPos.y);
state.getCam().unproject(touchPos);
System.out.println("World touch: " + touchPos.x + ", " + touchPos.y);
Rectangle goBackButtonBounds = settingsView.getGoBackButtonBounds();
Rectangle quitGameButtonBounds = settingsView.getQuitGameButtonBounds();
Rectangle switchMusicBounds = settingsView.getSwitchMusicBounds();
Rectangle switchSoundBounds = settingsView.getSwitchSoundBounds();
if (goBackButtonBounds.contains(touchPos.x, touchPos.y)) {
System.out.println("Go back button is pressed");
state.getStateManager().setState(new MainMenuState(state.getStateManager(), _FBIC));
}
if (quitGameButtonBounds.contains(touchPos.x, touchPos.y)) {
System.out.println("Go back button is pressed");
state.getStateManager().setState(new MainMenuState(state.getStateManager(), _FBIC));
}
if (switchMusicBounds.contains(touchPos.x, touchPos.y)) {
settingsView.toggleSwitch();
//TODO
}
if (switchSoundBounds.contains(touchPos.x, touchPos.y)) {
settingsView.toggleSwitchSound();
// TODO
}
}
}
public void update(float dt) {
handleInput();
}
public void render(SpriteBatch sb) {
settingsView.render(sb);
}
public void dispose() {
settingsView.dispose();
}
public void enter() {
}
public void exit() {
dispose();
}
}
package com.wordbattle.game.states;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.wordbattle.game.WordBattle;
import com.wordbattle.game.controller.HowToPlayController;
import com.wordbattle.game.controller.SettingsController;
import com.wordbattle.game.network.FirebaseInterface;
public class SettingsState extends BaseState{
private SettingsController controller;
private FirebaseInterface _FBIC;
public SettingsState(StateManager gsm, FirebaseInterface _FBIC){
super(gsm);
this._FBIC = _FBIC;
this.controller = new SettingsController(this, _FBIC);
cam.setToOrtho(false, WordBattle.WIDTH, WordBattle.HEIGHT);
}
public StateManager getStateManager() {
return gsm;
}
@Override
public void handleInput() {
}
@Override
public void update(float dt) {
controller.update(dt);
cam.update();
}
@Override
public void render(SpriteBatch sb) {
controller.render(sb);
}
@Override
public void enter() {
}
@Override
public void exit() {
}
@Override
public void dispose() {
}
}
...@@ -17,6 +17,7 @@ public class MainMenuView { ...@@ -17,6 +17,7 @@ public class MainMenuView {
private Rectangle joinGameButtonBounds; private Rectangle joinGameButtonBounds;
private Rectangle newGameButtonBounds; private Rectangle newGameButtonBounds;
private Rectangle howToPlayButtonBounds; private Rectangle howToPlayButtonBounds;
private Rectangle settingsBounds;
private OrthographicCamera cam; private OrthographicCamera cam;
...@@ -25,7 +26,7 @@ public class MainMenuView { ...@@ -25,7 +26,7 @@ public class MainMenuView {
public MainMenuView(OrthographicCamera cam) { public MainMenuView(OrthographicCamera cam) {
this.cam = cam; this.cam = cam;
this.backgroundTexture = new Texture("backgroundWithLogo.png"); this.backgroundTexture = new Texture("correctBg.png");
this.joinGameButton = new Texture("join_game.png"); this.joinGameButton = new Texture("join_game.png");
this.newGameButton = new Texture("new_game.png"); this.newGameButton = new Texture("new_game.png");
this.howToPlayButton = new Texture("how_to1.png"); this.howToPlayButton = new Texture("how_to1.png");
...@@ -52,6 +53,14 @@ public class MainMenuView { ...@@ -52,6 +53,14 @@ public class MainMenuView {
howToPlayButton.getWidth(), howToPlayButton.getWidth(),
howToPlayButton.getHeight() howToPlayButton.getHeight()
); );
settingsBounds = new Rectangle(
WordBattle.WIDTH - 100,
WordBattle.HEIGHT - 100,
100,
100
);
} }
public void render(SpriteBatch spriteBatch) { public void render(SpriteBatch spriteBatch) {
...@@ -83,6 +92,7 @@ public class MainMenuView { ...@@ -83,6 +92,7 @@ public class MainMenuView {
public Rectangle getHowToPlayButtonBounds() { public Rectangle getHowToPlayButtonBounds() {
return howToPlayButtonBounds; return howToPlayButtonBounds;
} }
public Rectangle getSettingsBounds() { return settingsBounds; }
......
package com.wordbattle.game.view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
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;
public class SettingsView {
private Texture backgroundTexture;
private Texture quitGameTexture;
private Texture switchOnMusicTexture;
private Texture switchOffMusicTexture;
private boolean switchMusicState = false;
private Texture switchOnSoundTexture;
private Texture switchOffSoundTexture;
private boolean switchSoundState = false;
private Rectangle switchMusicBounds;
private Rectangle switchSoundBounds;
private OrthographicCamera cam;
private Rectangle goBackButtonBounds;
private Rectangle quitGameButtonBounds;
public SettingsView(OrthographicCamera cam) {
this.cam = cam;
this.backgroundTexture = new Texture("settingsBg1.png");
this.quitGameTexture = new Texture("quitGameBtn.png");
this.switchOnMusicTexture = new Texture("onButton.png");
this.switchOffMusicTexture = new Texture("offButton.png");
this.switchOnSoundTexture = new Texture("onButton.png");
this.switchOffSoundTexture = new Texture("offButton.png");
goBackButtonBounds = new Rectangle(
0,
WordBattle.HEIGHT - 100,
100,
100
);
float buttonWidth = 300;
float buttonHeight = 100;
quitGameButtonBounds = new Rectangle(
(WordBattle.WIDTH - buttonWidth) / 2,
WordBattle.HEIGHT / 3 - buttonHeight / 2,
buttonWidth,
buttonHeight
);
//Music button
float switchWidth = 100;
float switchHeight = 45;
float switchX = WordBattle.WIDTH - switchWidth - 85;
float switchY = WordBattle.HEIGHT - switchHeight - 236;
switchMusicBounds = new Rectangle(
switchX,
switchY,
switchWidth,
switchHeight
);
//Sound button
float switchSoundX = switchX;
float switchSoundY = switchY - 122;
switchSoundBounds = new Rectangle(
switchSoundX,
switchSoundY,
switchWidth,
switchHeight
);
}
public void render(SpriteBatch spriteBatch) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
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;
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);
spriteBatch.end();
}
public Rectangle getGoBackButtonBounds() {
return goBackButtonBounds;
}
public Rectangle getQuitGameButtonBounds() {
return quitGameButtonBounds;
}
public Rectangle getSwitchMusicBounds() {
return switchMusicBounds;
}
public void toggleSwitch() {
switchMusicState = !switchMusicState;
// Perform actions based on the switch state
if (switchMusicState) {
// play music
} else {
// stop music
}
}
public Rectangle getSwitchSoundBounds() {
return switchSoundBounds;
}
public void toggleSwitchSound() {
switchSoundState = !switchSoundState;
if (switchSoundState) {
// sound on
} else {
// sound off
}
}
public void dispose() {
backgroundTexture.dispose();
quitGameTexture.dispose();
switchOnMusicTexture.dispose();
switchOffMusicTexture.dispose();
switchOnSoundTexture.dispose();
switchOffSoundTexture.dispose();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment