com.github.unluckyninja.defenseofhuman.GameScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.github.unluckyninja.defenseofhuman.GameScreen.java

Source

/****************************************************************************************
 * Copyright (C) 2013 UnluckyNinja                                                      *
 *                                                                                      *
 * This program is free software; you can redistribute it and/or                        *
 * modify it under the terms of the GNU General Public License                          *
 * as published by the Free Software Foundation; either version 2                       *
 * of the License, or (at your option) any later version.                               *
 *                                                                                      *
 * This program is distributed in the hope that it will be useful,                      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                        *
 * GNU General Public License for more details.                                         *
 *                                                                                      *
 * You should have received a copy of the GNU General Public License                    *
 * along with this program; if not, see here:http://www.gnu.org/licenses/gpl-2.0.txt    *
 ****************************************************************************************/
package com.github.unluckyninja.defenseofhuman;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.github.unluckyninja.defenseofhuman.model.GameWorld;
import com.github.unluckyninja.defenseofhuman.view.renderer.PlayerRenderer;

/**
 * @author UnluckyNinja
 */
public class GameScreen implements Screen {

    private boolean debug = true;

    private GameWorld gameWorld;
    private SpriteBatch batch;
    private PlayerRenderer playerRenderer = new PlayerRenderer(null);

    private Stage stage;

    public GameScreen(SpriteBatch batch) {
        this.batch = batch;
        gameWorld = new GameWorld();
        setupStage();
        tempCreate();
    }

    private void tempCreate() {
    }

    private void setupStage() {
        stage = new Stage();
        stage.addListener(new InputListener() {

            @Override
            public boolean keyDown(InputEvent event, int keycode) {
                if (keycode == Input.Keys.ESCAPE) {
                    DefenseOfHuman.game.changeScreen(DefenseOfHuman.State.MENU);
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void render(float delta) {
        batch.setProjectionMatrix(DefenseOfHuman.game.camera.combined);
        logics(delta);
        graphics(delta);
        DefenseOfHuman.game.camera.position.set(gameWorld.getLocalPlayer().getX(),
                gameWorld.getLocalPlayer().getY(), delta);
        DefenseOfHuman.game.camera.update();
    }

    @Override
    public void resize(int width, int height) {
        stage.setViewport(width, height, true);
        Vector3 temp = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
        DefenseOfHuman.game.unproject(temp);
        DefenseOfHuman.game.sceneInputController.cursorPosition.set(temp.x, temp.y);
    }

    @Override
    public void show() {
        DefenseOfHuman.game.inputilexer.addProcessor(0, stage);
        DefenseOfHuman.game.inputilexer.addProcessor(0, gameWorld.getLocalPlayer().getController());
    }

    @Override
    public void hide() {
        DefenseOfHuman.game.inputilexer.removeProcessor(stage);
        DefenseOfHuman.game.inputilexer.removeProcessor(gameWorld.getLocalPlayer().getController());
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        stage.dispose();
        gameWorld.dispose();
    }

    private void logics(float delta) {
        gameWorld.update(delta);
    }

    private void graphics(float delta) {
        batch.begin();
        playerRenderer.render(batch, gameWorld.getLocalPlayer(), delta);
        batch.end();
        if (debug) {
            gameWorld.drawDebug(batch);
        }
    }

    public void toggleDebug(boolean debug) {
        this.debug = debug;
    }

    public boolean isDebugging() {
        return debug;
    }
}