halive.shootinoutside.game.ShootinOutsideGame.java Source code

Java tutorial

Introduction

Here is the source code for halive.shootinoutside.game.ShootinOutsideGame.java

Source

/*******************************************************************************
 * Copyright (c) HALive, 2015.
 * For Licence information see LICENSE.md
 ******************************************************************************/

package halive.shootinoutside.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import halive.shootinoutside.common.core.game.entities.player.Player;
import halive.shootinoutside.common.core.game.map.GameMap;
import halive.shootinoutside.common.core.game.map.MapContainer;
import halive.shootinoutside.game.entities.PlayerActive;
import halive.shootinoutside.game.entities.PlayerInactive;
import halive.shootinoutside.game.hud.GameHUD;
import halive.shootinoutside.game.input.GameControls;
import halive.shootinoutside.game.map.GameMapRenderer;
import halive.shootinoutside.game.menu.GameOptionMenu;
import halive.shootinoutside.game.menu.TestGameOptionMenu;
import halive.shootinoutside.menu.MainMenu;
import halive.shootinoutside.util.VectorUtils;
import halive.shootinoutside.util.ui.MultipleInputAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ShootinOutsideGame implements InputProcessor, Screen, MapContainer {

    private PlayerActive player;
    private List<PlayerInactive> otherPlayers;

    private GameMap gameMap;
    private GameMapRenderer mapRenderer;

    private MultipleInputAdapter multipleInputAdapter = new MultipleInputAdapter();

    private MainMenu mainMenu;

    private GameHUD hud;
    private boolean renderOptionMenu = false;
    private GameOptionMenu menu;

    public ShootinOutsideGame(MainMenu menu) {
        this.mainMenu = menu;
    }

    public MainMenu getMainMenu() {
        return mainMenu;
    }

    public void setMainMenu(MainMenu mainMenu) {
        this.mainMenu = mainMenu;
    }

    public void init() {
        player = new PlayerActive(this);
        player.init();

        gameMap = GameMap.createEmptyMap(50, 50);
        mapRenderer = new GameMapRenderer(gameMap);
        player.respawn(VectorUtils.toVector2(gameMap.getSpawnPos(null)));
        multipleInputAdapter.addProcessor(this);

        hud = new GameHUD(new ScreenViewport(), this);
        multipleInputAdapter.addProcessor(hud);
        Gdx.input.setInputProcessor(multipleInputAdapter);
        Gdx.graphics.setContinuousRendering(true);
        hud.init();
        menu = new TestGameOptionMenu(new ScreenViewport(), this);
        menu.init();

        otherPlayers = Collections.synchronizedList(new ArrayList<PlayerInactive>());
        int x = 0, y = 0;
        do {
            do {
                PlayerInactive inactive = new PlayerInactive(this);
                inactive.setName("Test Player");
                inactive.setxPos(x);
                inactive.setyPos(y);
                otherPlayers.add(inactive);
                y += 64;
            } while (y < gameMap.getHeightPixels());
            x += 64;
        } while (x < gameMap.getWidthPixels());
    }

    public void addPlayer(PlayerInactive in) {
        in.init();
        otherPlayers.add(in);
    }

    @Override
    public boolean keyDown(int keycode) {
        player.handleKeyInput(keycode);
        if (GameControls.OPTION_MENU.isValidKeycode(keycode)) {
            menu.showMenu();
        }
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        if (GameControls.RIGHT.isValidKeycode(keycode) || GameControls.LEFT.isValidKeycode(keycode))
            player.clearXMovement();
        if (GameControls.UP.isValidKeycode(keycode) || GameControls.DOWN.isValidKeycode(keycode))
            player.clearYMovement();
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        player.mouseMoved(screenX, screenY);
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        player.mouseMoved(screenX, screenY);
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        player.mouseMoved(screenX, screenY);
        return false;
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        if (menu.isMenuActive()) {
            menu.render(delta);
        } else {
            mapRenderer.setView(player.getPlayerCamaera());
            mapRenderer.render();
            player.render();
            for (int i = 0; i < otherPlayers.size(); i++) {
                PlayerInactive p = otherPlayers.get(i);
                if (p.isVisible(this.player.getPlayerCamaera())) {
                    p.render();
                }
                p.update(delta);
            }
            hud.draw();
        }
    }

    public void update(float delta) {
        player.update(delta);
        player.updateCamera();
        hud.act(delta);
    }

    @Override
    public void resize(int width, int height) {
        player.resize(width, height);
        hud.resize(width, height);
    }

    public GameMap getGameMap() {
        return gameMap;
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }

    @Override
    public int getMapHeight() {
        return gameMap.getHeightPixels();
    }

    @Override
    public int getMapWidth() {
        return gameMap.getWidthPixels();
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }

    public MultipleInputAdapter getMultipleInputAdapter() {
        return multipleInputAdapter;
    }
}