scenes.GameModeSelect.java Source code

Java tutorial

Introduction

Here is the source code for scenes.GameModeSelect.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 scenes;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.circle.game.Config;
import map.GameMaps;
import map.Map;
import misc.CircleColors;
import object.CombustibleBarrel;
import object.MapObject;
import object.Obstacle;

/**
 *
 * @author <a href="mailto:kevin.vanderburg@student.fontys.nl">Kevin van der
 * Burg</a>
 */
public class GameModeSelect extends ApplicationAdapter {

    private Stage stage; //done
    private TextureAtlas atlas; //done
    private Skin skin; //done
    private Table table; // Align all buttons
    private TextButton buttonTut, buttonPlay, buttonExit;
    private BitmapFont font60, font30; //done
    private Label heading;
    private Map[] availableMaps;
    private int selectedMap;
    private ShapeRenderer shapeRenderer;
    private int xOffset;
    private int yOffset;
    private SpriteBatch batch;
    private Texture barrelTexture = new Texture(Gdx.files.internal("sprites/Barrel.png"));
    private int playerLimitIndex;
    private int[] playerLimit = new int[] { 1, 2, 3, 4 };

    public GameModeSelect() {
        create();
    }

    @Override
    public void create() {
        playerLimitIndex = 0;
        shapeRenderer = new ShapeRenderer();
        shapeRenderer.setAutoShapeType(true);

        batch = new SpriteBatch();
        availableMaps = GameMaps.getMaps();
        stage = new Stage();

        Gdx.input.setInputProcessor(stage);

        atlas = new TextureAtlas("ui/buttonPack.pack");
        skin = new Skin(atlas);

        table = new Table(skin);
        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        //initialize fonts
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/MunroSmall.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        parameter.size = 60;
        font60 = generator.generateFont(parameter); // font size 12 pixels
        font60.setColor(Color.BLACK);
        parameter.size = 40;
        font30 = generator.generateFont(parameter); // font size 12 pixels
        font30.setColor(0.5f, 0.5f, 0.5f, 255);
        generator.dispose(); // don't forget to dispose to avoid memory leaks!

        final TextButtonStyle textButtonStyle = new TextButtonStyle();
        textButtonStyle.downFontColor = new Color(0f, 0.57f, 0.92f, 1f);
        textButtonStyle.overFontColor = new Color(0f, 0.57f, 1f, 1f);
        textButtonStyle.font = font30;
        textButtonStyle.fontColor = Color.BLACK;

        buttonTut = new TextButton("Tutorial", textButtonStyle);
        buttonTut.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Config.page = new SearchGame(availableMaps[selectedMap].getName(), "Tutorial",
                        playerLimit[playerLimitIndex]);
            }

        });

        buttonTut.pad(20);

        buttonPlay = new TextButton("Normal Game", textButtonStyle);
        buttonPlay.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Config.page = new SearchGame(availableMaps[selectedMap].getName(), "Normal Game",
                        playerLimit[playerLimitIndex]);
            }

        });

        buttonPlay.pad(20);

        buttonExit = new TextButton("Back", textButtonStyle);
        buttonExit.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Config.page = new MainMenu();
            }

        });
        buttonExit.pad(20);

        LabelStyle headingStyle = new LabelStyle(font60, Color.BLACK);

        final TextButton toggleLevel = new TextButton("Map: " + availableMaps[selectedMap].getName(),
                textButtonStyle);
        toggleLevel.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                selectedMap++;
                if (selectedMap >= availableMaps.length)
                    selectedMap = 0;

                toggleLevel.setText("Map: " + availableMaps[selectedMap].getName());
            }
        });

        final TextButton togglePlayeLimit = new TextButton("players: " + playerLimit[playerLimitIndex],
                textButtonStyle);
        togglePlayeLimit.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                playerLimitIndex++;
                if (playerLimitIndex >= playerLimit.length)
                    playerLimitIndex = 0;

                togglePlayeLimit.setText("players: " + playerLimit[playerLimitIndex]);
            }
        });
        heading = new Label("Game modes", headingStyle);

        table.add(heading);
        table.row();
        table.padBottom(50);
        table.add(buttonTut);
        table.row();
        table.padBottom(50);
        table.add(buttonPlay);
        table.row();
        table.padBottom(50);
        table.add(toggleLevel);
        table.row();
        table.padBottom(50);
        table.add(togglePlayeLimit);
        table.row();
        table.padBottom(50);
        table.add(buttonExit);
        //table.debug();
        stage.addActor(table);
        //xOffset = (Gdx.graphics.getWidth() / 100 * 65);
        xOffset = (int) table.getX() + (int) toggleLevel.getRight() + (Gdx.graphics.getWidth() / 2);
        yOffset = Gdx.graphics.getHeight() / 2 - (availableMaps[selectedMap].getHeight() / 5);

    }

    @Override
    public void render() {
        shapeRenderer.begin();
        Gdx.gl.glLineWidth(2);
        shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(CircleColors.white);
        shapeRenderer.rect(xOffset, yOffset, (availableMaps[selectedMap].getWidth() / 5),
                (availableMaps[selectedMap].getHeight() / 5));
        shapeRenderer.set(ShapeRenderer.ShapeType.Line);
        shapeRenderer.setColor(CircleColors.black);
        shapeRenderer.rect(xOffset, yOffset, (availableMaps[selectedMap].getWidth() / 5),
                (availableMaps[selectedMap].getHeight() / 5));

        if (availableMaps[selectedMap] != null) {
            for (MapObject mapObject : availableMaps[selectedMap].getObjects()) {
                if (mapObject.getClass() == CombustibleBarrel.class) {
                    CombustibleBarrel combustibleBarrel = (CombustibleBarrel) mapObject;
                    shapeRenderer.setColor(CircleColors.red);
                    shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
                    batch.begin();
                    //shapeRenderer.circle(xOffset + combustibleBarrel.getPosition().x, yOffset + combustibleBarrel.getPosition().y, 18);
                    batch.draw(barrelTexture, xOffset + (combustibleBarrel.getPosition().x / 5) - (18 / 5),
                            yOffset + (combustibleBarrel.getPosition().y / 5) - (18 / 5), (32 / 5), (32 / 5));
                    batch.end();
                }
                if (mapObject.getClass() == Obstacle.class) {
                    Obstacle obstacle = (Obstacle) mapObject;
                    shapeRenderer.setColor(CircleColors.dark_gray);
                    shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
                    shapeRenderer.rect(xOffset + (obstacle.getPosition().x / 5),
                            yOffset + (obstacle.getPosition().y / 5), (obstacle.getWidth() / 5),
                            (obstacle.getHeight() / 5));
                }
            }
        }

        shapeRenderer.end();
        stage.act();
        stage.draw();
    }
}