Android Open Source - mobius Sprite Rendering 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.  ja v a 2s. c  om*/
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.Filter;
import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.sgtcodfish.mobiusListing.components.Opacity;
import com.sgtcodfish.mobiusListing.components.PlatformSprite;
import com.sgtcodfish.mobiusListing.components.PlayerSprite;
import com.sgtcodfish.mobiusListing.components.PlayerState;
import com.sgtcodfish.mobiusListing.components.Position;
import com.sgtcodfish.mobiusListing.components.StaticSprite;
import com.sgtcodfish.mobiusListing.components.Velocity;

/**
 * Handles drawing Entities with a Position and Renderable component
 * 
 * @author Ashley Davis (SgtCoDFish)
 */
public class SpriteRenderingSystem extends EntityProcessingSystem {
  private ComponentMapper<Position>    positionMapper      = null;

  private ComponentMapper<PlayerSprite>  playerSpriteMapper    = null;
  private ComponentMapper<PlayerState>  playerStateMapper    = null;

  private ComponentMapper<StaticSprite>  staticSpriteMapper    = null;

  private ComponentMapper<PlatformSprite>  platformSpriteMapper  = null;
  private ComponentMapper<Opacity>    opacityMapper      = null;

  private Batch              batch          = null;
  private Camera              camera          = null;

  @SuppressWarnings("unchecked")
  public SpriteRenderingSystem(Batch batch, Camera camera) {
    this(Filter.allComponents(Position.class).any(PlayerSprite.class, StaticSprite.class, PlatformSprite.class),
        batch, camera);
  }

  protected SpriteRenderingSystem(Filter filter, Batch batch, Camera camera) {
    super(filter);

    this.batch = batch;
    this.camera = camera;
  }

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

    playerSpriteMapper = world.getMapper(PlayerSprite.class);
    playerStateMapper = world.getMapper(PlayerState.class);

    staticSpriteMapper = world.getMapper(StaticSprite.class);

    platformSpriteMapper = world.getMapper(PlatformSprite.class);
    opacityMapper = world.getMapper(Opacity.class);
  }

  @Override
  protected void process(Entity e) {
    Position p = positionMapper.get(e);
    PlayerSprite playerSprite = playerSpriteMapper.get(e);
    StaticSprite staticSprite = staticSpriteMapper.get(e);
    PlatformSprite platformSprite = platformSpriteMapper.get(e);

    batch.begin();

    if (playerSprite != null) {
      Velocity v = world.getMapper(Velocity.class).get(e);
      int mirror = 1;

      if (v != null) {
        if (v.velocity.x > 0.0f) {
          mirror = 1;
        } else if (v.velocity.x < 0.0f) {
          mirror = -1;
        }
      }

      batch.draw(playerSprite.getFrame(playerStateMapper.get(e).state, world.getDelta()), p.position.x
          + (mirror < 0 ? playerSprite.spriteWidth : 0), p.position.y, playerSprite.spriteWidth * mirror,
          playerSprite.spriteHeight);

    } else if (staticSprite != null) {
      int mirror = (staticSprite.mirrored ? -1 : 1);
      batch.draw(staticSprite.textureRegion, p.position.x, p.position.y, staticSprite.spriteWidth, mirror
          * staticSprite.spriteHeight);

    } else if (platformSprite != null) {
      int mirror = (platformSprite.mirrored ? -1 : 1);
      Opacity opacity = opacityMapper.get(e);

      if (opacity != null) {
        batch.setColor(1.0f, 1.0f, 1.0f, opacity.opacity);
      }

      batch.draw(platformSprite.textureRegion, p.position.x, p.position.y, platformSprite.rectangle.width, mirror
          * platformSprite.rectangle.height);

      if (opacity != null) {
        batch.setColor(1.0f, 1.0f, 1.0f, 1.0f);
      }
    }

    batch.end();
  }

  @Override
  protected void begin() {
    batch.setProjectionMatrix(camera.combined);
  }
}




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