Android Open Source - mobius Platform Input 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  ww w  .  j  a v a2 s  .  c  o m
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.Filter;
import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.math.Vector3;
import com.sgtcodfish.mobiusListing.components.ChildLinked;
import com.sgtcodfish.mobiusListing.components.DxLayer;
import com.sgtcodfish.mobiusListing.components.DyLayer;
import com.sgtcodfish.mobiusListing.components.FadableLayer;
import com.sgtcodfish.mobiusListing.components.MovingLayer;
import com.sgtcodfish.mobiusListing.components.Opacity;
import com.sgtcodfish.mobiusListing.components.PlatformInputListener;
import com.sgtcodfish.mobiusListing.components.PlatformSprite;
import com.sgtcodfish.mobiusListing.components.Position;

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

  private ComponentMapper<PlatformSprite>  platformSpriteMapper  = null;

  private ComponentMapper<DxLayer>    dxLayerMapper      = null;
  private ComponentMapper<DyLayer>    dyLayerMapper      = null;
  private ComponentMapper<FadableLayer>  fadableLayerMapper    = null;

  private ComponentMapper<Opacity>    opacityMapper      = null;

  private ComponentMapper<ChildLinked>  childLinkedMapper    = null;

  private TerrainCollisionSystem      collisionSystem      = null;

  private static final float        CLICK_WAIT_TIME      = 0.5f;
  private float              timeSinceLastClick    = CLICK_WAIT_TIME;
  private boolean              clickFlag        = false;

  private Camera              camera          = null;
  private Vector3              mouse          = null;

  private boolean              hasReleased        = false;

  @SuppressWarnings("unchecked")
  public PlatformInputSystem(Camera camera, TerrainCollisionSystem collisionSystem) {
    this(Filter.allComponents(PlatformInputListener.class, PlatformSprite.class).any(DxLayer.class, DyLayer.class,
        FadableLayer.class), collisionSystem, camera);
  }

  protected PlatformInputSystem(Filter filter, TerrainCollisionSystem collisionSystem, Camera camera) {
    super(filter);

    this.camera = camera;
    this.collisionSystem = collisionSystem;
    this.mouse = new Vector3();
  }

  @Override
  public void initialize() {
    positionMapper = world.getMapper(Position.class);
    platformSpriteMapper = world.getMapper(PlatformSprite.class);
    dxLayerMapper = world.getMapper(DxLayer.class);
    dyLayerMapper = world.getMapper(DyLayer.class);
    fadableLayerMapper = world.getMapper(FadableLayer.class);
    opacityMapper = world.getMapper(Opacity.class);
    childLinkedMapper = world.getMapper(ChildLinked.class);
  }

  @Override
  public void begin() {
    timeSinceLastClick += world.getDelta();
  }

  @Override
  protected void process(Entity e) {
    if (timeSinceLastClick > CLICK_WAIT_TIME && hasReleased) {
      Position p = positionMapper.get(e);
      PlatformSprite platformSprite = platformSpriteMapper.get(e);

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

      platformSprite.rectangle.x = p.position.x;
      platformSprite.rectangle.y = p.position.y;

      if (isChild) {
        platformSprite.rectangle.y -= platformSprite.spriteHeight;
      }

      if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
        clickFlag = true;

        mouse.set(Gdx.input.getX(), Gdx.input.getY(), 0.0f);
        mouse = camera.unproject(mouse, 0.0f, 0.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        if (platformSprite.rectangle.contains((float) mouse.x, (float) mouse.y)) {
          // if this entity is a child, move its parent.
          Entity actualEntity = (isChild ? childLinkedMapper.get(e).parentEntity : e);

          DxLayer dxLayer = dxLayerMapper.get(actualEntity);
          MovingLayer movingLayer = (dxLayer != null ? dxLayer : dyLayerMapper.get(actualEntity));
          FadableLayer fadableLayer = fadableLayerMapper.get(actualEntity);

          final int degree = 1; // could be different in future

          if (movingLayer != null) {
            collisionSystem.platformInteracted(movingLayer.layer, degree * movingLayer.tempDirection);
            movingLayer.interact(positionMapper.get(actualEntity), degree);
          } else if (fadableLayer != null) {
            collisionSystem.platformInteracted(fadableLayer.layer, degree);
            fadableLayer.interact(opacityMapper.get(actualEntity), degree);
          }

          if (movingLayer == null && fadableLayer == null) {
            Gdx.app.error("PLATFORM_INPUT",
                "Invalid interactable platform (neither movable or fadable) detected.");
          }
        }
      }

      if (Gdx.app.getLogLevel() == Application.LOG_DEBUG) {
        if (Gdx.input.isButtonPressed(Buttons.RIGHT)) {
          Gdx.app.debug("PLATFORM_INPUT", "Detected platform rectangle:\nx: " + platformSprite.rectangle.x
              + "\ny: " + platformSprite.rectangle.y + "\nw: " + platformSprite.rectangle.width + "\nh: "
              + platformSprite.rectangle.height);
        }
      }
    } else if (!Gdx.input.isButtonPressed(Buttons.LEFT)) {
      hasReleased = true;
    }
  }

  @Override
  public void end() {
    if (clickFlag) {
      timeSinceLastClick = 0.0f;
      clickFlag = false;
      hasReleased = false;
    }
  }
}




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