Android Open Source - Ready-Set-Rogue Control Processor






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.processors;
/* w  w w . j a v  a 2  s .  co  m*/
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.warsheep.scamp.adt.Pair;
import com.warsheep.scamp.components.*;
import com.warsheep.scamp.components.StateComponent.State;
import com.warsheep.scamp.components.StateComponent.Directionality;
import com.warsheep.scamp.screens.MainGameScreen;

import java.awt.*;
import java.util.*;

public class ControlProcessor extends EntitySystem implements InputProcessor, StateProcessor.StateListener {

    private ImmutableArray<Entity> entities;
    private Point touchStartPosition = new Point();
    private Queue<Pair<State, Directionality>> actions;
    private CollisionProcessor collisions;
    private int simulatedX = 0;
    private int simulatedY = 0;
    private boolean hasAttacked = false; // If player can attack more than once, change this to an int-variable inside AttackerComp

    public ControlProcessor() {
        actions = new ArrayDeque<>();
    }

    public void addedToEngine(Engine engine) {
        entities = engine.getEntitiesFor(Family.all(ControllableComponent.class).get());
        collisions = engine.getSystem(CollisionProcessor.class);
    }

    private void addAction(Entity entity, Pair<State, Directionality> pair) {
        TileComponent tilePos = ECSMapper.tile.get(entity);
        if (pair.getLeft() == State.MOVING) {
            if (ECSMapper.control.get(entity).movesConsumed <= ECSMapper.control.get(entity).movementBonus) {
                if (collisions.checkMove(tilePos.x + simulatedX,
                        tilePos.y + simulatedY,
                        entity, pair.getRight(), false)) {
                    System.out.println("Blocked");
                    // Some visual feedback
                } else {
                    actions.add(pair);
                    switch (pair.getRight()) {
                        case UP:
                            simulatedY++;
                            break;
                        case DOWN:
                            simulatedY--;
                            break;
                        case LEFT:
                            simulatedX--;
                            break;
                        case RIGHT:
                            simulatedX++;
                            break;
                    }
                    ECSMapper.control.get(entity).movesConsumed++;
                }
            }
        }
        if (pair.getLeft() == State.ATTACKING) {
            if (!hasAttacked) {
                actions.add(pair);
                hasAttacked = true;
            }
        }
    }

    @Override
    public Queue<Pair<Entity, Pair<State, Directionality>>> turnEnd() {
        Queue<Pair<Entity, Pair<State, Directionality>>> actionQueue = new ArrayDeque<>();
        for (int i = 0; i < entities.size(); i++) {
            for (Pair<State, Directionality> action : this.actions) {
                actionQueue.add(new Pair(entities.get(i), action));
            }
            ECSMapper.control.get(entities.get(i)).movesConsumed = 0;
        }

        while (!this.actions.isEmpty()) {
            this.actions.poll();
        }

        simulatedX = 0;
        simulatedY = 0;
        hasAttacked = false;

        return actionQueue;
    }

    public void update(float deltaTime) {
        for (int i = 0; i < entities.size(); i++) {
            Entity entity = entities.get(i);
            LevelComponent lvlCmp = ECSMapper.level.get(entity);
            DamageableComponent dmgCmp = ECSMapper.damage.get(entity);
            AttackerComponent atkCmp = ECSMapper.attack.get(entity);

            if (lvlCmp != null) {
                MainGameScreen.level = lvlCmp.level;
                MainGameScreen.currentExp = lvlCmp.experiencePoints;
                MainGameScreen.nextLevelExp = lvlCmp.nextLevelExp;
            }

            if (dmgCmp != null) {
                MainGameScreen.maxHealth = dmgCmp.maxHealth;
                MainGameScreen.currentHealth = dmgCmp.currentHealth;
            }

            if (atkCmp != null) {
                MainGameScreen.damage = atkCmp.baseDamage;
            }
        }
    }

    @Override
    public boolean keyDown(int keycode) {
        switch (keycode) {
            case Input.Keys.UP:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.UP));
                }
                return true;
            case Input.Keys.DOWN:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.DOWN));
                }
                return true;
            case Input.Keys.RIGHT:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.RIGHT));
                }
                return true;
            case Input.Keys.LEFT:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.LEFT));
                }
                return true;
            case Input.Keys.W:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.UP));
                }
                return true;
            case Input.Keys.S:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.DOWN));
                }
                return true;
            case Input.Keys.D:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.RIGHT));
                }
                return true;
            case Input.Keys.A:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.LEFT));
                }
                return true;

            // Attacking scheme --> To be changed
            case Input.Keys.I:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.UP));
                }
                return true;
            case Input.Keys.K:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.DOWN));
                }
                return true;
            case Input.Keys.L:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.RIGHT));
                }
                return true;
            case Input.Keys.J:
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.LEFT));
                }
                return true;
            case Input.Keys.R:
                MainGameScreen.gameState = MainGameScreen.GameState.GAME_OVER;
                return true;
        }

        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

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

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        touchStartPosition.x = screenX;
        touchStartPosition.y = screenY;

        int clickPosX = screenX - Gdx.graphics.getWidth() / 2;
        int clickPosY = screenY - Gdx.graphics.getHeight() / 2;

        if (Math.abs(clickPosX) > Math.abs(clickPosY)) {
            // Move Left or Right
            if (clickPosX > 0) {
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.RIGHT));
                }
            } else {
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.LEFT));
                }
            }
        } else {
            // Move Up or Down
            if (clickPosY > 0) {
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.DOWN));
                }
            } else {
                for (int i = 0; i < entities.size(); i++) {
                    this.addAction(entities.get(i), new Pair<>(State.MOVING, Directionality.UP));
                }
            }
        }

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        int xDiff = screenX - touchStartPosition.x;
        int yDiff = screenY - touchStartPosition.y;

        if (Math.abs(xDiff) > 50 || Math.abs(yDiff) > 50) {
            if (Math.abs(xDiff) > Math.abs(yDiff)) {
                // Attack sideways
                if (xDiff > 0) {
                    for (int i = 0; i < entities.size(); i++) {
                        this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.RIGHT));
                    }
                } else {
                    for (int i = 0; i < entities.size(); i++) {
                        this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.LEFT));
                    }
                }
            } else {
                // Attack up/down
                if (yDiff < 0) {
                    for (int i = 0; i < entities.size(); i++) {
                        this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.UP));
                    }
                } else {
                    for (int i = 0; i < entities.size(); i++) {
                        this.addAction(entities.get(i), new Pair<>(State.ATTACKING, Directionality.DOWN));
                    }
                }
            }
        }

        return false;
    }

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

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

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

}




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