Android Open Source - mobius Solid Processing System






From Project

Back to project page mobius.

License

The source code is released under:

MIT License

If you think the Android project mobius 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.sgtcodfish.mobiusListing.systems;
/*from  w  w  w.jav  a2  s  . co  m*/
import java.util.ArrayList;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.Filter;
import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.Gdx;
import com.sgtcodfish.mobiusListing.WorldConstants;
import com.sgtcodfish.mobiusListing.components.ChildLinked;
import com.sgtcodfish.mobiusListing.components.Collectable;
import com.sgtcodfish.mobiusListing.components.Interactable;
import com.sgtcodfish.mobiusListing.components.Inventory;
import com.sgtcodfish.mobiusListing.components.Opacity;
import com.sgtcodfish.mobiusListing.components.Position;
import com.sgtcodfish.mobiusListing.components.Solid;
import com.sgtcodfish.mobiusListing.components.Velocity;
import com.sgtcodfish.mobiusListing.player.PlayerConstants;
import com.sgtcodfish.mobiusListing.systems.AudioSystem.MobiusSounds;

/**
 * @author Ashley Davis (SgtCoDFish)
 */
public class SolidProcessingSystem extends EntityProcessingSystem {
  private ComponentMapper<Position>    positionMapper    = null;
  private ComponentMapper<Velocity>    velocityMapper    = null;

  private ComponentMapper<Solid>      solidMapper      = null;

  private ComponentMapper<Inventory>    inventoryMapper    = null;
  private ComponentMapper<Collectable>  collectableMapper  = null;

  private ComponentMapper<Interactable>  interactableMapper  = null;

  private ComponentMapper<Opacity>    opacityMapper    = null;

  private ComponentMapper<ChildLinked>  childLinkedMapper  = null;

  private ArrayList<Entity>        movingSolids    = null;
  private ArrayList<Entity>        staticSolids    = null;

  private LinkingSystem          linkingSystem    = null;

  @SuppressWarnings("unchecked")
  public SolidProcessingSystem(LinkingSystem linkingSystem) {
    this(Filter.allComponents(Position.class, Solid.class), linkingSystem);
  }

  protected SolidProcessingSystem(Filter filter, LinkingSystem linkingSystem) {
    super(filter);
    this.linkingSystem = linkingSystem;
  }

  @Override
  public void initialize() {
    positionMapper = world.getMapper(Position.class);
    velocityMapper = world.getMapper(Velocity.class);

    solidMapper = world.getMapper(Solid.class);

    inventoryMapper = world.getMapper(Inventory.class);
    collectableMapper = world.getMapper(Collectable.class);

    interactableMapper = world.getMapper(Interactable.class);

    opacityMapper = world.getMapper(Opacity.class);

    childLinkedMapper = world.getMapper(ChildLinked.class);

    movingSolids = new ArrayList<Entity>(world.getEntityManager().getActiveEntityCount());
    staticSolids = new ArrayList<Entity>(world.getEntityManager().getActiveEntityCount());
  }

  @Override
  protected void begin() {
    movingSolids.clear();
    staticSolids.clear();
  }

  @Override
  protected void process(Entity e) {
    Position position = positionMapper.get(e);
    Solid solid = solidMapper.get(e);

    boolean isChild = (childLinkedMapper.get(e) != null);

    solid.boundingBox.x = position.position.x;
    solid.boundingBox.y = position.position.y - (isChild ? solid.boundingBox.height : 0);

    if (velocityMapper.get(e) != null) {
      movingSolids.add(e);
    } else {
      Opacity opacity = opacityMapper.get(e);
      if (opacity != null) {
        if (opacity.opacity < WorldConstants.GLOBAL_SOLID_OPACITY_THRESHOLD) {
          return;
        }
      }

      staticSolids.add(e);
    }
  }

  @Override
  protected void end() {
    // compare moving against one another, compare each moving against all
    // static.
    for (int i = 0; i < movingSolids.size(); i++) {
      Entity e = movingSolids.get(i);
      Solid s = solidMapper.get(e);

      for (int j = i + 1; j < movingSolids.size(); j++) {
        Entity otherEntity = movingSolids.get(j);

        Solid other = solidMapper.get(otherEntity);

        if (s.boundingBox.overlaps(other.boundingBox)) {
          handleCollision(e, movingSolids.get(j));
        }
      }

      for (Entity staticSolid : staticSolids) {
        Solid other = solidMapper.get(staticSolid);

        if (s.boundingBox.overlaps(other.boundingBox)) {
          handleCollision(e, staticSolid);
        }
      }
    }
  }

  protected void handleCollision(Entity one, Entity other) {
    if (inventoryMapper.get(one) != null && collectableMapper.get(other) != null) {
      collect(one, other);
      return;
    } else if (inventoryMapper.get(other) != null && collectableMapper.get(one) != null) {
      collect(other, one);
      return;
    } else if (solidMapper.get(other).weight < solidMapper.get(one).weight && interactableMapper.get(other) != null
        && PlayerConstants.interacting) {
      // TODO: Fix dirty hack with weights
      if (inventoryMapper.get(one) != null && inventoryMapper.get(one).inventoryList.size() > 0) {
        world.getSystem(LevelAdvanceSystem.class).setPassive(false);
      }
    } else if ((solidMapper.get(one).weight < solidMapper.get(other).weight && interactableMapper.get(one) != null && PlayerConstants.interacting)) {
      // TODO: Fix dirty hack with weights
      if (inventoryMapper.get(other) != null && inventoryMapper.get(other).inventoryList.size() > 0) {
        world.getSystem(LevelAdvanceSystem.class).setPassive(false);
      }
    }
  }

  protected void collect(Entity one, Entity item) {
    Inventory inventory = inventoryMapper.get(one);
    Collectable collectable = collectableMapper.get(item);

    inventory.inventoryList.add(collectable.item);
    Gdx.app.debug("PICKUP", collectable.item.name + " was picked up!");

    linkingSystem.scheduleForRemoval(item);
    world.getSystem(AudioSystem.class).enqueue(MobiusSounds.BLOOP);
  }
}




Java Source Code List

com.sgtcodfish.mobiusListing.Item.java
com.sgtcodfish.mobiusListing.MobiusListingGame.java
com.sgtcodfish.mobiusListing.TerrainCollisionMap.java
com.sgtcodfish.mobiusListing.WorldConstants.java
com.sgtcodfish.mobiusListing.android.AndroidLauncher.java
com.sgtcodfish.mobiusListing.components.ChildLinked.java
com.sgtcodfish.mobiusListing.components.Collectable.java
com.sgtcodfish.mobiusListing.components.DxLayer.java
com.sgtcodfish.mobiusListing.components.DyLayer.java
com.sgtcodfish.mobiusListing.components.FadableLayer.java
com.sgtcodfish.mobiusListing.components.FocusTaker.java
com.sgtcodfish.mobiusListing.components.InteractableLayer.java
com.sgtcodfish.mobiusListing.components.Interactable.java
com.sgtcodfish.mobiusListing.components.Inventory.java
com.sgtcodfish.mobiusListing.components.Linked.java
com.sgtcodfish.mobiusListing.components.MobiusSprite.java
com.sgtcodfish.mobiusListing.components.MovingLayer.java
com.sgtcodfish.mobiusListing.components.Opacity.java
com.sgtcodfish.mobiusListing.components.PlatformInputListener.java
com.sgtcodfish.mobiusListing.components.PlatformSprite.java
com.sgtcodfish.mobiusListing.components.PlayerInputListener.java
com.sgtcodfish.mobiusListing.components.PlayerSprite.java
com.sgtcodfish.mobiusListing.components.PlayerState.java
com.sgtcodfish.mobiusListing.components.Position.java
com.sgtcodfish.mobiusListing.components.Solid.java
com.sgtcodfish.mobiusListing.components.StaticSprite.java
com.sgtcodfish.mobiusListing.components.TiledRenderable.java
com.sgtcodfish.mobiusListing.components.Velocity.java
com.sgtcodfish.mobiusListing.desktop.DesktopLauncher.java
com.sgtcodfish.mobiusListing.levels.LevelEntityFactory.java
com.sgtcodfish.mobiusListing.player.HumanoidAnimationState.java
com.sgtcodfish.mobiusListing.player.PlayerConstants.java
com.sgtcodfish.mobiusListing.player.PlayerEntityFactory.java
com.sgtcodfish.mobiusListing.systems.AudioSystem.java
com.sgtcodfish.mobiusListing.systems.CollisionBoxRenderingDebugSystem.java
com.sgtcodfish.mobiusListing.systems.FocusTakerSystem.java
com.sgtcodfish.mobiusListing.systems.LevelAdvanceSystem.java
com.sgtcodfish.mobiusListing.systems.LinkingSystem.java
com.sgtcodfish.mobiusListing.systems.MovementSystem.java
com.sgtcodfish.mobiusListing.systems.PlatformInputSystem.java
com.sgtcodfish.mobiusListing.systems.PlayerInputSystem.java
com.sgtcodfish.mobiusListing.systems.SolidProcessingSystem.java
com.sgtcodfish.mobiusListing.systems.SpriteRenderingSystem.java
com.sgtcodfish.mobiusListing.systems.TerrainCollisionBoxRenderingDebugSystem.java
com.sgtcodfish.mobiusListing.systems.TerrainCollisionSystem.java
com.sgtcodfish.mobiusListing.systems.TiledRenderingSystem.java