th.skyousuke.libgdx.xomaisad.GameOverScreen.java Source code

Java tutorial

Introduction

Here is the source code for th.skyousuke.libgdx.xomaisad.GameOverScreen.java

Source

/*
 * Copyright 2016 Surasek Nusati <surasek@gmail.com>
 *
 * 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 th.skyousuke.libgdx.xomaisad;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.viewport.FitViewport;

public class GameOverScreen implements Screen {

    private static final String WIN_TEXT = " ";
    private static final String DRAW_TEXT = "";
    private static final String LOSE_TEXT = "?? ??";

    private final String resultText;
    private final float resultTextWidth;
    private final float resultTextHeight;
    private final XOMaiSad game;

    private int nextPlayerMark;
    private String countText;

    private OrthographicCamera camera;
    private FitViewport viewport;

    public GameOverScreen(final XOMaiSad game, int playerMarkType, int result) {
        this.game = game;
        nextPlayerMark = (playerMarkType == Mark.MarkType.X) ? Mark.MarkType.O : Mark.MarkType.X;

        switch (result) {
        case GameResult.PLAYER_WIN:
            resultText = WIN_TEXT;
            break;
        case GameResult.DRAW:
            resultText = DRAW_TEXT;
            break;
        case GameResult.PLAYER_LOSE:
            resultText = LOSE_TEXT;
            break;
        default:
            throw new IllegalArgumentException("Wrong game result");
        }
        countText = "";

        game.glyphLayout.setText(Assets.instance.font, resultText);
        resultTextWidth = game.glyphLayout.width;
        resultTextHeight = game.glyphLayout.height;

        camera = new OrthographicCamera();
        viewport = new FitViewport(XOMaiSad.SCREEN_WIDTH, XOMaiSad.SCREEN_HEIGHT, camera);
    }

    @Override
    public void show() {
        Timer.schedule(new Timer.Task() {
            @Override
            public void run() {
                game.setScreen(new GameScreen(game, nextPlayerMark));
            }
        }, 3);
        Timer.schedule(new Timer.Task() {
            int count = 3;

            @Override
            public void run() {
                countText = "? " + count + " ";
                count--;
            }
        }, 0, 1, 2);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.8f, 0.8f, 0.8f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        game.batch.setProjectionMatrix(camera.combined);

        game.batch.begin();
        Assets.instance.font.draw(game.batch, resultText, -resultTextWidth / 2, resultTextHeight / 2);
        Assets.instance.font.draw(game.batch, countText, -220, -150);
        game.batch.end();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void dispose() {
    }
}