Commit f62a1fb2 authored by Turid Cecilie Dahl's avatar Turid Cecilie Dahl
Browse files

#98 Adds MenuStateTemplate + cleans

parent 784cd645
......@@ -16,7 +16,7 @@ import com.gameware.game.models.Player;
import com.gameware.game.states.GameStateManager;
import com.gameware.game.states.LoginState;
import com.gameware.game.states.MenuState;
import com.gameware.game.states.PlayStateComposite;
import com.gameware.game.states.PlayStateTemplate;
import java.io.IOException;
import java.lang.reflect.Constructor;
......@@ -44,7 +44,7 @@ public class GameWare extends ApplicationAdapter {
private Player player;
private List<Game> games = new ArrayList<>();
private Map<String, PlayStateComposite> gameIdToPlayState = new HashMap<>();
private Map<String, PlayStateTemplate> gameIdToPlayState = new HashMap<>();
//Singleton (lazy initialization)
private GameWare(){ }
......@@ -60,10 +60,6 @@ public class GameWare extends ApplicationAdapter {
@Override
public void create () {
if(instance == null){
instance = this;
}
gsm = GameStateManager.getInstance();
loadGameConfigs();
......@@ -121,11 +117,11 @@ public class GameWare extends ApplicationAdapter {
return games;
}
public Map<String, PlayStateComposite> getGameIdToPlayState(){
public Map<String, PlayStateTemplate> getGameIdToPlayState(){
return gameIdToPlayState;
}
public void updateGameMap(String id, PlayStateComposite state){
public void updateGameMap(String id, PlayStateTemplate state){
gameIdToPlayState.put(id, state);
}
......@@ -201,7 +197,7 @@ public class GameWare extends ApplicationAdapter {
try {
Class cl = Class.forName(className);
Constructor con = cl.getConstructor(GameStateManager.class);
PlayStateComposite state = (PlayStateComposite) con.newInstance(gsm);
PlayStateTemplate state = (PlayStateTemplate) con.newInstance(gsm);
gameIdToPlayState.put(id, state);
} catch (Exception e) {
e.printStackTrace();
......
......@@ -7,7 +7,7 @@ import com.gameware.game.sprites.Bubble;
import java.util.ArrayList;
public class BubbleWrapState extends PlayStateComposite {
public class BubbleWrapState extends PlayStateTemplate {
private Texture background;
private Texture unpopped;
private Texture popped1;
......
......@@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ColorRushState extends PlayStateComposite {
public class ColorRushState extends PlayStateTemplate {
private List<ColorRushTarget> activeTargets;
private List<ColorRushTarget> previousTargets;
......
......@@ -26,7 +26,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateJoinTournamentState extends State {
public class CreateJoinTournamentState extends MenuStateTemplate {
// Data
private List<Tournament> tournaments = new ArrayList<>();
......@@ -107,7 +107,7 @@ public class CreateJoinTournamentState extends State {
}
}
private void makeStage(){
protected void makeStage(){
Table rootTable = makeRootTable();
titleLabel.setFontScale(titleFontBigScale);
......
......@@ -27,7 +27,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateNewTournamentState extends State {
public class CreateNewTournamentState extends MenuStateTemplate {
// Data
private List<Game> games;
......@@ -124,7 +124,7 @@ public class CreateNewTournamentState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
rootTable = makeRootTable();
titleLabel.setFontScale(titleFontBigScale);
......
......@@ -18,7 +18,7 @@ import com.gameware.game.models.Tournament;
import java.util.ArrayList;
import java.util.List;
public class FinishedTournamentState extends State {
public class FinishedTournamentState extends MenuStateTemplate {
// Data
private Tournament tournament;
......@@ -56,7 +56,7 @@ public class FinishedTournamentState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = makeRootTable(backgroundFinTourn);
rootTable.defaults().spaceBottom(spacingMedium);
......
......@@ -12,7 +12,7 @@ import com.gameware.game.sprites.SlicingCircle;
import java.util.ArrayList;
import java.util.List;
public class FruitSlicerState extends PlayStateComposite {
public class FruitSlicerState extends PlayStateTemplate {
private int totalFruitsCut = 0;
private float startingEmitFrequency = 1f;
private float endingEmitFrequency = 0.2f;
......
......@@ -19,7 +19,7 @@ import com.gameware.game.sprites.LoadingText;
import java.io.IOException;
import java.util.NoSuchElementException;
public class LoginState extends State {
public class LoginState extends MenuStateTemplate {
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......@@ -65,7 +65,7 @@ public class LoginState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = super.makeRootTable();
// Add widgets
......
......@@ -16,7 +16,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.gameware.game.GameWare;
import com.gameware.game.sprites.LoadingText;
public class MenuState extends State{
public class MenuState extends MenuStateTemplate{
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......@@ -45,14 +45,13 @@ public class MenuState extends State{
private boolean multiBtnClicked = false;
private LoadingText loadingText = new LoadingText();
public MenuState(GameStateManager gsm) {
super(gsm);
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = super.makeRootTable();
// Add widgets
......
package com.gameware.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.gameware.game.GameWare;
public abstract class MenuStateTemplate extends State{
// Font
protected final float fontScale = 3f;
protected final float tinierTitleFontBigScale = 1.5f;
protected final float titleFontBigScale = 2.5f;
// Spacing
protected final float spacingLittle = Gdx.graphics.getHeight()/50;
protected final float spacingMedium = Gdx.graphics.getHeight()/15;
protected final float spacingLarge = spacingMedium*2;
// Padding
protected final int rootTablePadding = Gdx.graphics.getWidth()/15;
protected final int rootTablePaddingTop = Gdx.graphics.getWidth()/10;
protected final int padding = 50;
// Width & Height
protected final int buttonWidth = Gdx.graphics.getWidth()/3;
protected final int buttonHeight = Gdx.graphics.getHeight()/12;
protected final float questionMarkWidthAndHeight = Gdx.graphics.getWidth()/25;
protected final float imageWidthAndHeigh = Gdx.graphics.getWidth()/4;
// Textures
protected TextureRegionDrawable background = new TextureRegionDrawable(new TextureRegion(new Texture("state/bg1.jpg")));
protected TextureRegionDrawable backgroundScore = new TextureRegionDrawable(new TextureRegion(new Texture("state/bg_score.jpg")));
protected TextureRegionDrawable backgroundFinTourn = new TextureRegionDrawable(new TextureRegion(new Texture("state/bg1_finTourn.jpg")));
protected TextureRegionDrawable backgroundTableBlueRounded = new TextureRegionDrawable(new TextureRegion(new Texture("state/tableBGRounded.png")));
protected TextureRegionDrawable questionMark = new TextureRegionDrawable(new TextureRegion(new Texture("state/questionMarkIcon.png")));
protected Color backColor = Color.VIOLET;
protected Color greenColor = Color.GREEN;
// Sound Effects
protected Sound checkBoxSound;
public MenuStateTemplate(GameStateManager gsm){
super(gsm);
skin.getFont("font").getData().setScale(fontScale);
// Add sound effects
this.checkBoxSound = Gdx.audio.newSound(Gdx.files.internal("sfx/check_box.ogg"));
}
// Override Methods
@Override
protected void handleInput(){}
@Override
public void update(float dt){
if(firstTimeRunningUpdate) {
Gdx.input.setInputProcessor(stage);
Gdx.input.setCatchBackKey(true);
firstTimeRunningUpdate = false;
}
this.handleInput();
stage.act(dt);
}
@Override
public void render(SpriteBatch sb){
stage.draw();
}
@Override
public void dispose(){
stage.dispose();
}
@Override
public void reset(){}
// Abstract methods
protected abstract void makeStage();
// UI methods:
// Makes default rootTable
protected Table makeRootTable(TextureRegionDrawable background){
Table rootTable = new Table();
rootTable.setFillParent(true);
rootTable.setBackground(background);
rootTable.pad(rootTablePadding);
rootTable.padTop(rootTablePaddingTop);
rootTable.defaults().space(spacingLittle);
return rootTable;
}
// Makes default rootTable
protected Table makeRootTable(){
return makeRootTable(background);
}
protected TextureRegionDrawable makeColorBackground(Color color){
Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565);
bgPixmap.setColor(color);
bgPixmap.fill();
return new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap)));
}
// Dialog methods
protected Table makeRow(Label label, final Dialog dialog){
Table table = new Table();
table.add(label).spaceRight(spacingLittle/2);
Image questionMarkImage = new Image(questionMark);
questionMarkImage.addListener(new ClickListener(){
@Override
public void clicked(InputEvent e, float x, float y){
if(GameWare.getInstance().isSoundEffectsOn()){ checkBoxSound.play(); }
dialog.show(stage);
}
});
table.add(questionMarkImage).width(questionMarkWidthAndHeight).height(questionMarkWidthAndHeight).top();
return table;
}
protected Dialog makeDialog(Label label){
Dialog dialog = new Dialog("", skin, "dialog") {
public void result(Object obj) { }
};
label.setAlignment(Align.center);
label.setWrap(true);
dialog.getContentTable().add(label).width(Gdx.graphics.getWidth()/1.5f).pad(dialog.getHeight()/5f).padBottom(0);
dialog.button("Okay", true); //sends "true" as the result
dialog.getButtonTable().pad(dialog.getHeight()/2f);
return dialog;
}
protected TextButton.TextButtonStyle makeTextButtonStyle(Color background){
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.up = skin.newDrawable("button", background);
textButtonStyle.down = skin.newDrawable("button-down", background);
textButtonStyle.font = skin.getFont("font-big");
textButtonStyle.fontColor = Color.WHITE;
return textButtonStyle;
}
}
......@@ -11,7 +11,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Scaling;
import com.gameware.game.GameWare;
public class OptionsState extends State {
public class OptionsState extends MenuStateTemplate {
// Labels
private final Label titleLabel = new Label("Options", skin, "big");
......@@ -31,7 +31,7 @@ public class OptionsState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
// Make root table
Table rootTable = super.makeRootTable();
rootTable.defaults().spaceBottom(spacingMedium).expandY();
......
......@@ -15,25 +15,33 @@ import java.util.ArrayList;
import java.util.List;
public class PauseState extends State {
private ShapeRenderer sr;
private List<PauseCircle> pauseCircles;
// Data
private PlayStateTemplate pausedGame;
// Textures
private Texture background;
private Texture pauseText;
private Texture dimmingTexture;
private Texture countdownNumberTexture;
private List<Texture> countdownNumbers;
// Variables
private Color originalFontColor;
private float countdownDurationLeft;
private boolean countdownStarted;
private boolean userExited;
private boolean needsConfirmation;
// Widgets
private List<PauseCircle> pauseCircles;
private ConfirmationBox confirmationBox;
private PauseMenuButton resumeButton;
private PauseMenuButton exitButton;
private boolean needsConfirmation;
private PlayStateComposite pausedGame;
private LoadingText loadingText;
private boolean userExited;
private Color originalFontColor;
private List<Texture> countdownNumbers;
private float countdownDurationLeft;
private boolean countdownStarted = false;
private Texture countdownNumberTexture;
public PauseState(GameStateManager gsm, PlayStateComposite pausedGame) {
public PauseState(GameStateManager gsm, PlayStateTemplate pausedGame) {
super(gsm);
this.background = new Texture(Gdx.files.internal("pause/PauseBackground.jpg"));
......@@ -215,7 +223,5 @@ public class PauseState extends State {
}
@Override
public void reset() {
}
public void reset() { }
}
......@@ -19,23 +19,32 @@ import java.io.IOException;
import java.util.List;
public abstract class PlayStateComposite extends State {
private int score;
public abstract class PlayStateTemplate extends State {
// Data
private Round round = null;
private Tournament tournament = null;
private Round updatedRound = null;
private RoundCheck roundCheck = null;
// Game values
private int score;
protected float totalGameTime = 0f;
protected float currentDuration = 0f;
// Game objects
protected Texture screenshot = null;
private PauseButton pauseButton;
private LoadingText loadingText = new LoadingText();
private BitmapFont font;
// Game booleans
private boolean pauseButtonVisible = true;
private boolean timeLeftVisible = true;
private boolean currentScoreVisible = true;
protected float totalGameTime = 0f;
protected Texture screenshot = null;
protected float currentDuration = 0f;
public PlayStateComposite(GameStateManager gsm){
public PlayStateTemplate(GameStateManager gsm){
super(gsm);
// Default pause button (black color)
......@@ -47,6 +56,11 @@ public abstract class PlayStateComposite extends State {
font.getData().setScale((float) (Gdx.graphics.getWidth()/GameWare.WIDTH*1.75));
}
// Override methods
@Override
protected void handleInput(){}
@Override
public void update(float dt){
// Post score and exit if the game is over and the loading text is rendering
if(this.loadingText.textIsRendering()){
......@@ -79,6 +93,7 @@ public abstract class PlayStateComposite extends State {
}
}
@Override
public void render(SpriteBatch sb){
this.loadingText.draw(sb);
......@@ -89,6 +104,17 @@ public abstract class PlayStateComposite extends State {
this.renderText(sb);
}
@Override
public void dispose(){
this.loadingText.dispose();
this.pauseButton.dispose();
this.font.dispose();
}
@Override
public void reset(){}
// Renders the text - This is used by this class' render method as well as by the PauseState
public void renderText(SpriteBatch sb){
// Shows the current duration if it's supposed to be visible and the game is time-based
......@@ -107,19 +133,13 @@ public abstract class PlayStateComposite extends State {
}
}
public void setGameFinished(){
this.loadingText.setLoading();
}
public void setRound(Round r){
this.round = r;
}
public void setTournament(Tournament t){
this.tournament = t;
}
// Game done methods:
public void gameDone(){
// When game is done, this method is called and posts the score, ands new instance of the game,
// uptades rounds and tournaments if in a tournament and sends back to the correct state.
// Remember to use setScore before using this method.
protected void gameDone(){
String id = "";
//Find the id to the game just played
for (String key : GameWare.getInstance().getGameIdToPlayState().keySet()){
......@@ -136,6 +156,22 @@ public abstract class PlayStateComposite extends State {
e.printStackTrace();
}
goToNextState();
}
// Posts the score
private void postScore(String gameId )throws IOException {
if(round==null) {
QueryIntermediate.postHighscore(GameWare.getInstance().getPlayer().getId(), gameId, this.score);
} else{
List<ModelInterface> updatedRoundModels = QueryIntermediate.putRoundScore(round.get_id(),tournament.get_id(), this.score);
updatedRound = (Round) updatedRoundModels.get(0);
roundCheck = (RoundCheck) updatedRoundModels.get(1);
}
}
// Go to the correct menu-state to go back to
private void goToNextState(){
if(round == null) {
//Singleplayer
gsm.set(new ScoreState(gsm, this.score, new SinglePlayerSelectGameState(gsm)));
......@@ -153,23 +189,27 @@ public abstract class PlayStateComposite extends State {
e.printStackTrace();
}
} else {
// No new round ready
gsm.set(new ScoreState(gsm, this.score, new ViewTournamentState(gsm, tournament, updatedRound)));
}
}
}
// Setters
public void setGameFinished(){
this.loadingText.setLoading();
}
public void setScore(int score){
this.score = score;
}
public void postScore(String gameId )throws IOException {
if(round==null) {
QueryIntermediate.postHighscore(GameWare.getInstance().getPlayer().getId(), gameId, this.score);
} else{
List<ModelInterface> updatedRoundModels = QueryIntermediate.putRoundScore(round.get_id(),tournament.get_id(), this.score);
updatedRound = (Round) updatedRoundModels.get(0);
roundCheck = (RoundCheck) updatedRoundModels.get(1);
}
public void setRound(Round r){
this.round = r;
}
public void setTournament(Tournament t){
this.tournament = t;
}
// Changes the color of the pause button to white
......@@ -194,10 +234,6 @@ public abstract class PlayStateComposite extends State {
this.font.setColor(color);
}
public Color getFontColor(){
return this.font.getColor();
}
public void setTotalGameTime(float totalGameTime){
this.totalGameTime = totalGameTime;
}
......@@ -206,6 +242,11 @@ public abstract class PlayStateComposite extends State {
this.currentDuration = currentDuration;
}
// Getters