Android Open Source - Ready-Set-Rogue Main Game Screen






From Project

Back to project page Ready-Set-Rogue.

License

The source code is released under:

GNU General Public License

If you think the Android project Ready-Set-Rogue listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.warsheep.scamp.screens;
/* w  w w  . j  a va2 s .  c o  m*/
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.warsheep.scamp.AssetDepot;
import com.warsheep.scamp.MapImporter;
import com.warsheep.scamp.PrefabFactory;
import com.warsheep.scamp.Scamp;
import com.warsheep.scamp.algorithms.BSPMapGenerator;
import com.warsheep.scamp.components.*;
import com.warsheep.scamp.processors.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class MainGameScreen extends ScreenAdapter {

    public static Engine ecs; // Ashley Entity-Component System
    public static final float TURN_DURATION = 0.9f;
    public static final int MAP_WIDTH = 40;
    public static final int MAP_HEIGHT = 40;
    VisibilityProcessor visibilityProcessor;
    MovementProcessor movementProcessor;
    CollisionProcessor collisionProcessor;
    ControlProcessor controlProcessor;
    DeathProcessor deathProcessor;
    StateProcessor stateProcessor;
    CombatProcessor combatProcessor;
    CameraProcessor cameraProcessor;
    TileProcessor tileProcessor;
    AIProcessor aiProcessor;
    LevelingProcessor levelProcessor;

    Scamp game;
    public static GameState gameState;
    private float accumulator = 0;

    // Temp UI Values
    public static int damage = 0;
    public static int currentHealth = 0;
    public static int maxHealth = 0;
    public static int level = 0;
    public static int currentExp = 0;
    public static int nextLevelExp = 0;

    public enum GameState {
        GAME_RUNNING, GAME_PAUSED, GAME_OVER, GAME_READY
    }

    private long startTime;

    public MainGameScreen(Scamp game) {
        this.game = game;
        gameState = GameState.GAME_RUNNING;
        create();
    }

    public void create() {
        ecs = new Engine();

        ArrayList<CollisionProcessor.CollisionListener> collisionListeners = new ArrayList();
        // Initialize processors and associate them with ecs engine
        visibilityProcessor = new VisibilityProcessor();

        tileProcessor = new TileProcessor(MAP_WIDTH, MAP_HEIGHT);
        ArrayList<MovementProcessor.MovementListener> movementListeners = new ArrayList();
        movementListeners.add(tileProcessor);
        movementProcessor = new MovementProcessor(movementListeners);
        combatProcessor = new CombatProcessor();
        aiProcessor = new AIProcessor();
        controlProcessor = new ControlProcessor();
        ArrayList<StateProcessor.StateListener> stateListeners = new ArrayList();
        stateListeners.add(movementProcessor);
        stateListeners.add(combatProcessor);
        stateListeners.add(aiProcessor);
        stateListeners.add(controlProcessor);
        stateProcessor = new StateProcessor(stateListeners, TURN_DURATION);

        collisionProcessor = new CollisionProcessor(collisionListeners);
        cameraProcessor = new CameraProcessor();
        deathProcessor = new DeathProcessor();
        levelProcessor = new LevelingProcessor();


        ecs.addSystem(tileProcessor);
        ecs.addSystem(visibilityProcessor);
        ecs.addSystem(collisionProcessor);
        ecs.addSystem(movementProcessor);
        ecs.addSystem(cameraProcessor);
        ecs.addSystem(deathProcessor);
        ecs.addSystem(combatProcessor);
        ecs.addSystem(stateProcessor);
        ecs.addSystem(aiProcessor);
        ecs.addSystem(controlProcessor);
        ecs.addSystem(levelProcessor);
        Gdx.input.setInputProcessor(controlProcessor);

        PrefabFactory fab = new PrefabFactory();
        fab.buildEntity("creatures/skeleton");

        // Skeleton blocker of doom
        Random rand = new Random();
        for (int i = 1; i < 4; i++) {
            Entity skeleton = fab.buildEntity("creatures/skeleton");
            ECSMapper.tile.get(skeleton).x = rand.nextInt(12 - 6) + 1;
            ECSMapper.tile.get(skeleton).y = rand.nextInt(12 - 6) + 1;
            ecs.addEntity(skeleton);
        }

        for (int i = 1; i < 2; i++) {
            Entity skeleton = fab.buildEntity("creatures/ghost");
            ECSMapper.tile.get(skeleton).x = rand.nextInt(12 - 6) + 1;
            ECSMapper.tile.get(skeleton).y = rand.nextInt(12 - 6) + 1;
            ecs.addEntity(skeleton);
        }

        // Crappy Debug Wizard mans
        Entity wizard = fab.buildEntity("creatures/debugwizard");
        ecs.addEntity(wizard);

        ECSMapper.tile.get(wizard).x = 8;
        ECSMapper.tile.get(wizard).y = 8;
        ECSMapper.transform.get(wizard).position.x = 8 * 24;
        ECSMapper.transform.get(wizard).position.y = 8 * 24;

        createCamera(wizard);


        // genMap();
        MapImporter mapImporter = new MapImporter();
        mapImporter.loadTiledMapJson(AssetDepot.MAP_PATH);

        for (Entity e : mapImporter.getEntities()) {
            ecs.addEntity(e);
        }

        //Start calculating game time
        startTime = System.currentTimeMillis();
    }

    @Override
    public void render(float delta) {
        switch (gameState) {
            case GAME_RUNNING:
                visibilityProcessor.startBatch();
                delta = ((System.currentTimeMillis() - startTime) / 1000);
                ecs.update(delta);
                visibilityProcessor.endBatch();
                addPlayerStats();
                addTimeCircle(delta);
                break;
            case GAME_OVER:
                game.setScreen(new MainMenuScreen(game));
                break;
            default:
                break;
        }
    }

    private void addTimeCircle(float delta) {
        ShapeRenderer shapeRenderer = new ShapeRenderer();

        if (delta - accumulator > TURN_DURATION) {
            accumulator = delta;
        }
        int size = (int) (10 - (delta % 3 * 3)) - 1;

        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(1, 1, 1, 0.5f);
        shapeRenderer.circle(Gdx.graphics.getWidth() - 15, Gdx.graphics.getHeight() - 15, size);
        shapeRenderer.end();
        Gdx.gl.glDisable(GL20.GL_BLEND);

    }

    private void addPlayerStats() {
        CharSequence str = String.format("Lvl %d | Dmg %d | HP %d/%d | Exp %d/%d",
                level, damage, currentHealth, maxHealth, currentExp, nextLevelExp);
        SpriteBatch spriteBatch = new SpriteBatch();
        BitmapFont font = new BitmapFont();

        spriteBatch.enableBlending();
        spriteBatch.begin();
        font.draw(spriteBatch, str, 10, Gdx.graphics.getHeight() - 10);
        spriteBatch.end();
    }

    private void createCamera(Entity target) {
        Entity entity = new Entity();

        CameraComponent camera = new CameraComponent();
        camera.camera = ecs.getSystem(VisibilityProcessor.class).getCamera();
        camera.target = target;

        entity.add(camera);

        ecs.addEntity(entity);
    }

    private void genMap() {
        BSPMapGenerator gen = new BSPMapGenerator(MAP_WIDTH, MAP_HEIGHT, 1, 3, 4);
        byte[][] data = gen.to2DArray();
        for (int x = 0; x < data.length; x++) {
            for (int y = data[0].length - 1; y >= 0; y--) {
                if (data[x][y] != ' ') {
                    buildTile(x, data.length - y - 1, 1, (char) data[x][y]);
                }
            }
        }
    }

    private void buildTile(int x, int y, int z, char type) {
        Entity e = new Entity();

        TileComponent tc = new TileComponent();
        tc.x = x;
        tc.y = y;
        tc.z = z;

        VisibleComponent vc = new VisibleComponent();

        AssetDepot assets = AssetDepot.getInstance();

        vc.originX = 12;
        vc.originY = 12;

        if (type == '#') {
            CollidableComponent cc = new CollidableComponent();
            e.add(cc);
            vc.image = assets.fetchImage("world_24x24", "oryx_wall_island_stone", 1);
        } else {
            vc.image = assets.fetchImage("world_24x24", "oryx_floor_darkgrey_stone", 1);
        }

        e.add(tc);
        e.add(vc);
        e.add(new TransformComponent());
        ecs.addEntity(e);
    }
}




Java Source Code List

com.warsheep.scamp.AssetDepot.java
com.warsheep.scamp.IOSLauncher.java
com.warsheep.scamp.MapImporter.java
com.warsheep.scamp.PrefabFactory.java
com.warsheep.scamp.Scamp.java
com.warsheep.scamp.adt.BSPRectangle.java
com.warsheep.scamp.adt.Container.java
com.warsheep.scamp.adt.Pair.java
com.warsheep.scamp.adt.Room.java
com.warsheep.scamp.algorithms.BSPMapGenerator.java
com.warsheep.scamp.algorithms.Compositor.java
com.warsheep.scamp.android.AndroidLauncher.java
com.warsheep.scamp.client.HtmlLauncher.java
com.warsheep.scamp.components.AIControllableComponent.java
com.warsheep.scamp.components.AttackerComponent.java
com.warsheep.scamp.components.CameraComponent.java
com.warsheep.scamp.components.CollidableComponent.java
com.warsheep.scamp.components.ControllableComponent.java
com.warsheep.scamp.components.DamageableComponent.java
com.warsheep.scamp.components.DropComponent.java
com.warsheep.scamp.components.ECSMapper.java
com.warsheep.scamp.components.FactionComponent.java
com.warsheep.scamp.components.InventoryComponent.java
com.warsheep.scamp.components.LevelComponent.java
com.warsheep.scamp.components.MovementComponent.java
com.warsheep.scamp.components.StateComponent.java
com.warsheep.scamp.components.TileComponent.java
com.warsheep.scamp.components.TransformComponent.java
com.warsheep.scamp.components.VisibleComponent.java
com.warsheep.scamp.desktop.AssetPacker.java
com.warsheep.scamp.desktop.DesktopLauncher.java
com.warsheep.scamp.processors.AIProcessor.java
com.warsheep.scamp.processors.CameraProcessor.java
com.warsheep.scamp.processors.CollisionProcessor.java
com.warsheep.scamp.processors.CombatProcessor.java
com.warsheep.scamp.processors.ControlProcessor.java
com.warsheep.scamp.processors.DeathProcessor.java
com.warsheep.scamp.processors.LevelingProcessor.java
com.warsheep.scamp.processors.MovementProcessor.java
com.warsheep.scamp.processors.StateProcessor.java
com.warsheep.scamp.processors.TileProcessor.java
com.warsheep.scamp.processors.VisibilityProcessor.java
com.warsheep.scamp.screens.MainGameScreen.java
com.warsheep.scamp.screens.MainMenuScreen.java