se.theodor.quiz.screen.PlayQuiz.java Source code

Java tutorial

Introduction

Here is the source code for se.theodor.quiz.screen.PlayQuiz.java

Source

/********************************************************************************
 * 
 *   Copyright 2014 Theodor Angergard
 *
 *   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 se.theodor.quiz.screen;

import se.theodor.quiz.GameField;
import se.theodor.quiz.OpenQuestionPanel;
import se.theodor.quiz.Question;
import se.theodor.quiz.Quiz;
import se.theodor.quiz.Team;
import se.theodor.quiz.TeamsEditingPanel;
import se.theodor.quiz.TeamsPanel;
import se.theodor.quiz.util.Objects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;

public class PlayQuiz implements Screen {

    public PlayQuiz(Quiz quiz, String gameName, String quizName, Array<Team> teams) {
        this.quiz = quiz;
        this.quizName = quizName;
        this.gameName = gameName;
        newGame = true;
        startTeams = teams;
    }

    // If you're loading from a saved file
    public PlayQuiz(Quiz quiz, String gameName) {
        this.quiz = quiz;
        this.gameName = gameName;
        newGame = false;
    }

    private Array<Team> startTeams;
    private OpenQuestionPanel openQuestionPanel;
    private GameField gameField;
    private TeamsPanel teamsPanel;
    private Quiz quiz;
    private Stage stage;
    private String quizName;
    private Question questionOpen;
    private String gameName;
    private boolean newGame = false;

    @Override
    public void render(float delta) {
        if (TeamsEditingPanel.hasClosedRecently()) {
            Team teamEdited = TeamsEditingPanel.getTeamEdited();
            if (TeamsEditingPanel.setTurn()) {
                teamsPanel.setTeamIndex(teamsPanel.getTeamIndex(teamEdited));
            }
            teamsPanel.getTeams().get(teamsPanel.getTeamIndex(teamEdited)).setPoints(TeamsEditingPanel.getPoints());

            gameField.loadActors();
        }

        if (openQuestionPanel != null) {
            openQuestionPanel.render(delta);
        } else {
            gameField.update();
            //         if (Gdx.input.isKeyPressed(Keys.UP)) {
            //            gameField.scaleFontCategory(0.005f);
            //         } else if (Gdx.input.isKeyPressed(Keys.DOWN)) {
            //            gameField.scaleFontCategory(-0.005f);
            //         } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
            //            gameField.scaleFontQuestion(0.005f);
            //         } else if (Gdx.input.isKeyPressed(Keys.LEFT)) {
            //            gameField.scaleFontQuestion(-0.005f);
            //         }
        }
        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {
        stage = Objects.STAGE;
        stage.clear();
        if (newGame) {
            gameField = new GameField(quizName, quiz, this, stage);
            teamsPanel = new TeamsPanel(quiz);
            teamsPanel.setTeams(startTeams);
            teamsPanel.setTeamIndex(0);
        } else {
            gameField = new GameField(quiz, this, stage);
            teamsPanel = new TeamsPanel(quiz);
            teamsPanel.setTeams(gameField.loadQuizAndGame(gameName));
        }
        gameField.loadActors();
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

    public void openQuestion(Question question) {
        questionOpen = question;
        openQuestionPanel = new OpenQuestionPanel(
                new Rectangle(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), question, this, quiz);
    }

    public void closeQuestion(String result) {
        openQuestionPanel = null;
        gameField.closeQuestion();
        teamsPanel.nextTurn(result, questionOpen.getPoints());
        questionOpen = null;
        gameField.saveGame(gameName, quizName, teamsPanel.getTeamIndex(), teamsPanel.getTeams());// TODO:ADD CHOOSe NAME
        loadUI();
    }

    public void setTeamIndex(int i) {
        teamsPanel.setTeamIndex(i);
    }

    public Array<Team> getTeams() {
        return teamsPanel.getTeams();
    }

    private void loadUI() {

    }

}