com.sixteencolorgames.sandbox.GameScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.sixteencolorgames.sandbox.GameScreen.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sixteencolorgames.sandbox;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.sixteencolorgames.sandbox.generation.World;

/**
 *
 * @author Allen
 */
class GameScreen extends MenuScreen {

    GameLauncher game;
    String STARTTEXT = "Start Game";
    Rectangle startGame;
    OrthographicCamera camera;
    World world;
    float elapsedTime = 0;
    private final int WIDTH = 800, HEIGHT = 480;

    public GameScreen(GameLauncher game, World generating) {
        this.game = game;
        world = generating;
        camera = new OrthographicCamera();
        camera.setToOrtho(false, WIDTH, HEIGHT);
    }

    /**
     * @return the camera
     */
    public OrthographicCamera getCamera() {
        return camera;
    }

    public void setCameraPosition(Vector2 center) {
        camera.position.set(center, 0);
    }

    /**
     * @return the elapsedTime
     */
    public float getElapsedTime() {
        return elapsedTime;
    }

    public void moveCameraPosition(Vector2 unit) {
        camera.translate(unit.cpy().scl(camera.zoom));
    }

    @Override
    public void render(float delta) {
        super.render(delta);
        camera.update();

        game.shape.setProjectionMatrix(camera.combined);
        game.batch.setProjectionMatrix(camera.combined);
        game.batch.begin();
        elapsedTime += Gdx.graphics.getDeltaTime();
        world.render();
        //        game.batch.draw(game.textures.get("startgame"), startGame.x, startGame.y,
        //                startGame.width, startGame.height);

        game.batch.end();
        //        game.shape.begin(ShapeRenderer.ShapeType.Line);
        //        game.shape.setColor(Color.CYAN);
        //        for (Waypoint point : dun.graph.getWaypoints()) {
        //            try {
        //                game.shape.line(point.getGridPosition().cpy().scl(SCALE).add(
        //                        SCALE / 2, SCALE / 2),
        //                        point.getPrevious().getGridPosition().cpy().scl(SCALE).add(
        //                                SCALE / 2, SCALE / 2));
        //            } catch (Exception ex) {
        //            }
        //        }
        //        game.shape.end();
        Matrix4 uiMatrix = camera.combined.cpy();
        uiMatrix.setToOrtho2D(0, 0, WIDTH, HEIGHT);
        game.batch.setProjectionMatrix(uiMatrix);
        game.batch.begin();
        game.batch.setColor(1, 1, 1, .5f);
        renderHUD();
        game.batch.setColor(1, 1, 1, 1);
        game.batch.end();

        if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
            this.moveCameraPosition(new Vector2(2, 0));
            if (camera.position.x / 16 > world.getWidth()) {
                this.setCameraPosition(new Vector2(0, camera.position.y));
            }
        } else if (Gdx.input.isKeyPressed(Keys.LEFT)) {
            this.moveCameraPosition(new Vector2(-2, 0));
            if (camera.position.x / 16 < 0) {
                this.setCameraPosition(new Vector2(world.getWidth() * 16, camera.position.y));
            }
        }

    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }

    private void renderHUD() {
    }

}