Android Open Source - mobius Terrain Collision 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 ww  .  ja  v  a 2s . c  om*/
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.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Pool.Poolable;
import com.sgtcodfish.mobiusListing.TerrainCollisionMap;
import com.sgtcodfish.mobiusListing.WorldConstants;
import com.sgtcodfish.mobiusListing.components.PlayerState;
import com.sgtcodfish.mobiusListing.components.Position;
import com.sgtcodfish.mobiusListing.components.Solid;
import com.sgtcodfish.mobiusListing.components.Velocity;
import com.sgtcodfish.mobiusListing.player.HumanoidAnimationState;

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

  private ComponentMapper<PlayerState>  stateMapper    = null;

  private TerrainCollisionMap        collisionMap  = null;
  private Vector2              collisionVector  = null;

  private Pool<LayerAndDegree>      ladPool      = null;
  private ArrayList<LayerAndDegree>    interactedList  = null;

  @SuppressWarnings("unchecked")
  public TerrainCollisionSystem(TerrainCollisionMap collisionMap) {
    this(Filter.allComponents(Position.class, Velocity.class, Solid.class), collisionMap);
  }

  protected TerrainCollisionSystem(Filter filter, TerrainCollisionMap collisionMap) {
    super(filter);

    this.interactedList = new ArrayList<LayerAndDegree>();
    this.collisionMap = collisionMap;
    this.ladPool = new Pool<TerrainCollisionSystem.LayerAndDegree>() {
      @Override
      protected LayerAndDegree newObject() {
        return new LayerAndDegree();
      }
    };
  }

  public void setCollisionMap(TerrainCollisionMap collisionMap) {
    this.collisionMap = collisionMap;
  }

  public TerrainCollisionMap getCollisionMap() {
    return collisionMap;
  }

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

    collisionVector = new Vector2();
  }

  @Override
  public void begin() {
    for (LayerAndDegree lad : interactedList) {
      collisionMap.layerInteracted(lad.layer, lad.degree);
      ladPool.free(lad);
    }

    interactedList.clear();
  }

  @Override
  protected void process(Entity e) {
    Position position = positionMapper.get(e);
    Velocity v = velocityMapper.get(e);
    Solid s = solidMapper.get(e);

    PlayerState ps = stateMapper.get(e);

    float playerOffset = 0.0f; // TODO: Fix dirty hack for player collision
                  // box.

    if (ps != null) {
      playerOffset = 16.0f;
    }

    collisionVector.set(position.position);

    if (v.velocity.x > 0.0f) {
      collisionVector.x += s.boundingBox.width - playerOffset;
    } else {
      collisionVector.x += playerOffset;
    }

    if (v.velocity.y > 0.0f) {
      collisionVector.y += s.boundingBox.height;
    }

    v.velocity.y -= WorldConstants.GRAVITY;

    if (collisionMap.willCollideX(collisionVector, v.velocity)) {
      v.velocity.x = 0.0f;
      if (ps != null && ps.state != HumanoidAnimationState.JUMPING) {
        ps.state = HumanoidAnimationState.STANDING;
      }
    }

    if (collisionMap.willCollideY(collisionVector, v.velocity)) {
      v.velocity.y = 0.0f;
      if (ps != null && ps.state == HumanoidAnimationState.JUMPING) {
        ps.state = (v.velocity.x != 0.0f ? HumanoidAnimationState.RUNNING : HumanoidAnimationState.STANDING);
      }
    }
  }

  @Override
  protected boolean checkProcessing() {
    return (collisionMap != null);
  }

  public void platformInteracted(TiledMapTileLayer layer, int degree) {
    interactedList.add(ladPool.obtain().set(degree, layer));
  }

  private class LayerAndDegree implements Poolable {
    public int          degree  = 0;
    public TiledMapTileLayer  layer  = null;

    public LayerAndDegree() {
      this(0, null);
    }

    public LayerAndDegree(int degree, TiledMapTileLayer layer) {
      set(degree, layer);
    }

    public LayerAndDegree set(int degree, TiledMapTileLayer layer) {
      this.degree = degree;
      this.layer = layer;

      return this;
    }

    @Override
    public void reset() {
      degree = 0;
      layer = null;
    }
  }
}




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