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

Java tutorial

Introduction

Here is the source code for th.skyousuke.libgdx.xomaisad.GameScreen.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.math.Vector3;
import com.badlogic.gdx.utils.viewport.FitViewport;

/**
 * Created by Bill on 16/12/2559.
 */

public class GameScreen implements Screen {

    private static final String AI_TEXT = "Ai: ?...";
    private static final String TURN_TEXT = "";

    private final float aiTextWidth;
    private final XOMaiSad game;

    private OrthographicCamera camera;
    private FitViewport viewport;
    private Vector3 touchPos;

    private Board board;
    private Ai ai;

    public GameScreen(final XOMaiSad game) {
        this(game, Mark.MarkType.X);
    }

    public GameScreen(final XOMaiSad game, int playerMark) {
        this.game = game;
        camera = new OrthographicCamera();
        viewport = new FitViewport(XOMaiSad.SCREEN_WIDTH, XOMaiSad.SCREEN_HEIGHT, camera);
        touchPos = new Vector3();

        board = new Board(playerMark);
        ai = new Ai();

        game.glyphLayout.setText(Assets.instance.font, AI_TEXT);
        aiTextWidth = game.glyphLayout.width;
    }

    @Override
    public void show() {
    }

    @Override
    public void render(float delta) {

        if (Gdx.input.justTouched()) {
            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(touchPos);
            board.handleTouch(touchPos.x, touchPos.y);
        }

        if (board.getState().isAiReadyToPlay() && !ai.isThinking()) {
            new Thread(() -> ai.minimaxMove(board.getState())).start();
        }

        if (board.getState().isGameOver()) {
            game.setScreen(new GameOverScreen(game, board.getState().getPlayerMarkType(),
                    board.getState().getGameResult()));
        }
        board.update(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();
        if (ai.isThinking()) {
            Assets.instance.font.draw(game.batch, AI_TEXT, -aiTextWidth / 2, 245);
        }

        Assets.instance.font.draw(game.batch, TURN_TEXT, -140, -210);

        if (board.getState().getActiveMarkType() == Mark.MarkType.X) {
            game.batch.draw(Assets.instance.imageAtlas.findRegion("x", 5), 70, -265);
        } else {
            game.batch.draw(Assets.instance.imageAtlas.findRegion("o", 5), 70, -265);
        }
        board.render(game);
        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() {
    }
}