Android Open Source - Ready-Set-Rogue State 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  ww.  j  av 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.warsheep.scamp.adt.Pair;
import com.warsheep.scamp.components.ECSMapper;
import com.warsheep.scamp.components.StateComponent;
import com.warsheep.scamp.components.StateComponent.*;

import java.util.*;

public class StateProcessor extends EntitySystem {

    private List<StateListener> listeners;
    private ImmutableArray<Entity> statefuls;
    private float interval;
    private float accumulator = 0;


    public static interface StateListener {

        // Do nothing defaults
        default public void idle(Entity entity) {
            ECSMapper.state.get(entity).inProgress = false;
        }

        default public void dead(Entity entity) {
            ECSMapper.state.get(entity).inProgress = false;
        }

        default public void hurt(Entity entity) {
            ECSMapper.state.get(entity).inProgress = false;
        }

        default public void attacking(Entity entity, Directionality direction) {
            ECSMapper.state.get(entity).inProgress = false;
        }

        default public void moving(Entity entity, Queue<Directionality> direction) {
            ECSMapper.state.get(entity).inProgress = false;
        }

        default public Queue<Pair<Entity, Pair<State, Directionality>>> turnEnd() {
            return new ArrayDeque<>();
        }
    }

    public StateProcessor(List<StateListener> listeners, float interval) {
        this.listeners = listeners;
        this.interval = interval;
    }

    @Override
    public void addedToEngine(Engine engine) {
        statefuls = engine.getEntitiesFor(Family.all(StateComponent.class).get());
    }

    @Override
    public void update(float deltaTime) {

        if (deltaTime - accumulator > this.interval) {
            this.accumulator = deltaTime;
            this.updateInterval();
        }

    }

    protected void updateInterval() {
        this.pullActions();
        this.resolveTurn();
    }

    private void pullActions() {
        Queue<Pair<Entity, Pair<State, Directionality>>> actionQueue;
        Map<Entity, Queue<Directionality>> movesMap = new HashMap<>();
        Map<Entity, Directionality> attacksMap = new HashMap<>();

        for (StateListener listener : this.listeners) {
            actionQueue = listener.turnEnd();

            if (actionQueue != null) {
                Entity e = null;

                for (Pair<Entity, Pair<State, Directionality>> action : actionQueue) {
                    if (action.getRight().getLeft() == State.MOVING) {
                        e = action.getLeft();
                        if(!movesMap.containsKey(e)) {
                            movesMap.put(e, new ArrayDeque<>());
                        }
                        movesMap.get(e).add(action.getRight().getRight());
                    }
                    if (action.getRight().getLeft() == State.ATTACKING) {
                        e = action.getLeft();
                        attacksMap.put(e, action.getRight().getRight());
                    }
                }
            }
        }
        for (StateListener movers : this.listeners) {
            for (Map.Entry<Entity, Queue<Directionality>> m : movesMap.entrySet()) {
                movers.moving(m.getKey(), m.getValue());
            }
            for (Map.Entry<Entity, Directionality> a : attacksMap.entrySet()) {
                movers.attacking(a.getKey(), a.getValue());
            }
        }

    }

    private void resolveTurn() {
        for (Entity stateful : this.statefuls) {
            State state = ECSMapper.state.get(stateful).state;
            boolean inProgress = ECSMapper.state.get(stateful).inProgress;
            if (!inProgress) {
                ECSMapper.state.get(stateful).inProgress = true;
                Directionality direction = ECSMapper.state.get(stateful).direction;
                switch (state) {
                    case ATTACKING:
                        for (StateListener listener : this.listeners) {
                            listener.attacking(stateful, direction);
                        }
                        break;
                    case DEAD:
                        for (StateListener listener : this.listeners) {
                            listener.dead(stateful);
                        }
                        break;
                    case IDLE:
                        for (StateListener listener : this.listeners) {
                            listener.idle(stateful);
                        }
                        break;
                    case HURT:
                        for (StateListener listener : this.listeners) {
                            listener.hurt(stateful);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}




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