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

Java tutorial

Introduction

Here is the source code for com.github.unluckyninja.defenseofhuman.DefenseOfHuman.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.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.github.unluckyninja.defenseofhuman.controller.input.listeners.PlayerInputController;
import com.github.unluckyninja.defenseofhuman.controller.input.listeners.SceneInputController;

/**
 * @author UnluckyNinja
 */
public class DefenseOfHuman extends Game {

    public static final DefenseOfHuman game = new DefenseOfHuman();
    public static final float PIXELS_PERMETER = 32.0f;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
        cfg.title = "Mouse Killer";
        cfg.width = 860;
        cfg.height = 480;
        cfg.useGL20 = true;
        new LwjglApplication(game, cfg);
    }

    int width = 860;
    int height = 480;

    SpriteBatch batch;
    OrthographicCamera camera;

    MenuScreen menu;
    GameScreen playing;
    private State state = State.MENU;

    InputMultiplexer inputilexer;
    SceneInputController sceneInputController;
    PlayerInputController playerInputController;

    @Override
    public void create() {
        batch = new SpriteBatch();
        camera = new OrthographicCamera();

        batch.setProjectionMatrix(camera.combined);

        menu = new MenuScreen(batch);
        playing = new GameScreen(batch);

        playerInputController = new PlayerInputController();
        sceneInputController = new SceneInputController(menu);
        inputilexer = new InputMultiplexer();

        changeScreen(State.MENU);

        inputilexer.addProcessor(sceneInputController);
        Gdx.input.setInputProcessor(inputilexer);
        //        Gdx.input.setCursorCatched(true);
    }

    @Override
    public void render() {
        Gdx.gl20.glClearColor(1.0f, 1.0f, 1.0f, 1);
        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);
        super.render();
    }

    @Override
    public void resize(int width, int height) {
        this.width = width;
        this.height = height;
        super.resize(width, height);
        camera.setToOrtho(false, width / PIXELS_PERMETER, height / PIXELS_PERMETER);
    }

    @Override
    public void dispose() {
        super.dispose();
        menu.dispose();
        batch.dispose();
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void changeScreen(State state) {
        switch (state) {
        case MENU:
            this.setScreen(menu);
            sceneInputController.setScreen(menu);
            this.state = State.MENU;
            break;
        case PLAYING:
            this.setScreen(playing);
            sceneInputController.setScreen(playing);
            this.state = State.PLAYING;
            break;
        }
    }

    public void unproject(Vector3 vector) {
        camera.unproject(vector);
    }

    public State getState() {
        return state;
    }

    public static enum State {
        PLAYING, MENU;
    }
}