Android Open Source - mobius Mobius Listing Game






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;
/*from  w  w w . ja  v  a2 s  .  co  m*/
import com.artemis.Component;
import com.artemis.Entity;
import com.artemis.World;
import com.artemis.managers.GroupManager;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.sgtcodfish.mobiusListing.components.Inventory;
import com.sgtcodfish.mobiusListing.components.Linked;
import com.sgtcodfish.mobiusListing.components.Position;
import com.sgtcodfish.mobiusListing.levels.LevelEntityFactory;
import com.sgtcodfish.mobiusListing.player.PlayerConstants;
import com.sgtcodfish.mobiusListing.player.PlayerEntityFactory;
import com.sgtcodfish.mobiusListing.systems.AudioSystem;
import com.sgtcodfish.mobiusListing.systems.CollisionBoxRenderingDebugSystem;
import com.sgtcodfish.mobiusListing.systems.FocusTakerSystem;
import com.sgtcodfish.mobiusListing.systems.LevelAdvanceSystem;
import com.sgtcodfish.mobiusListing.systems.LinkingSystem;
import com.sgtcodfish.mobiusListing.systems.MovementSystem;
import com.sgtcodfish.mobiusListing.systems.PlatformInputSystem;
import com.sgtcodfish.mobiusListing.systems.PlayerInputSystem;
import com.sgtcodfish.mobiusListing.systems.SolidProcessingSystem;
import com.sgtcodfish.mobiusListing.systems.SpriteRenderingSystem;
import com.sgtcodfish.mobiusListing.systems.TerrainCollisionBoxRenderingDebugSystem;
import com.sgtcodfish.mobiusListing.systems.TerrainCollisionSystem;
import com.sgtcodfish.mobiusListing.systems.TiledRenderingSystem;

/**
 * Contains all the main logic; most should be in classes that extend
 * EntityProcessingSystem.
 * 
 * @author Ashley Davis (SgtCoDFish)
 */
public class MobiusListingGame extends ApplicationAdapter {
  public enum MobiusState {
    TITLE_SCREEN, PLAYING;
  }

  public static boolean              DEBUG          = false;

  private SpriteBatch                batch          = null;
  private Camera                  camera          = null;

  public World                  world          = null;

  private MovementSystem              movementSystem      = null;
  private TerrainCollisionSystem          terrainCollisionSystem  = null;
  private LinkingSystem              linkingSystem      = null;
  private AudioSystem                audioSystem        = null;

  private TerrainCollisionBoxRenderingDebugSystem  terrainDebugSystem    = null;

  public PlayerEntityFactory            playerEntityFactory    = null;
  private Entity                  playerEntity      = null;

  public LevelEntityFactory            levelEntityFactory    = null;

  private final float                DEBUG_COOLDOWN      = 1.0f;
  private float                  timeSinceLastDebug    = DEBUG_COOLDOWN;

  public MobiusListingGame() {
    this(false);
  }

  public MobiusListingGame(boolean debug) {
    MobiusListingGame.DEBUG = debug;
  }

  @Override
  public void create() {
    if (DEBUG) {
      Gdx.app.setLogLevel(Application.LOG_DEBUG);
      Gdx.app.debug("DEBUG", "Debug logging enabled.");
    } else {
      Gdx.app.setLogLevel(Application.LOG_ERROR);
      LevelEntityFactory.VERBOSE_LOAD = false;
    }

    world = new World();

    batch = new SpriteBatch();
    camera = new OrthographicCamera(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);

    movementSystem = new MovementSystem(0.0f);
    terrainCollisionSystem = new TerrainCollisionSystem(null);
    linkingSystem = new LinkingSystem();
    audioSystem = new AudioSystem();

    world.setSystem(new PlayerInputSystem(this));
    world.setSystem(new PlatformInputSystem(camera, terrainCollisionSystem));

    world.setSystem(new SolidProcessingSystem(linkingSystem));
    world.setSystem(terrainCollisionSystem);

    world.setSystem(movementSystem);

    world.setSystem(linkingSystem);

    world.setSystem(new FocusTakerSystem(camera));
    world.setSystem(new TiledRenderingSystem(batch, camera));
    world.setSystem(new SpriteRenderingSystem(batch, camera));
    world.setSystem(audioSystem);

    if (DEBUG) {
      terrainDebugSystem = new TerrainCollisionBoxRenderingDebugSystem(camera, terrainCollisionSystem);
      world.setSystem(new CollisionBoxRenderingDebugSystem(camera));
      world.setSystem(terrainDebugSystem);
    }

    world.setSystem(new LevelAdvanceSystem(this), true);

    world.setManager(new GroupManager());

    world.initialize();

    playerEntityFactory = new PlayerEntityFactory(world);
    playerEntity = playerEntityFactory.createEntity(5.0f, 5.0f, true);

    levelEntityFactory = new LevelEntityFactory(world, batch);
    nextLevel();
    audioSystem.start();
  }

  @Override
  public void render() {
    float deltaTime = Gdx.graphics.getDeltaTime();
    world.setDelta(deltaTime);

    camera.update();

    Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // TODO: Fix dirty hack with global state in this line.
    PlayerConstants.interacting = Gdx.input.isKeyPressed(Keys.W);

    world.process();

    if (Gdx.app.getLogLevel() == Application.LOG_DEBUG) {
      timeSinceLastDebug += deltaTime;

      if (timeSinceLastDebug > DEBUG_COOLDOWN) {
        if (Gdx.input.isKeyPressed(Keys.F10)) {
          nextLevel();
          timeSinceLastDebug = 0.0f;
        }

        if (Gdx.input.isKeyPressed(Keys.F8)) {
          timeSinceLastDebug = 0.0f;
          Position p = playerEntity.getComponent(Position.class);
          Gdx.app.debug("PLAYER_POS", "Player is located at (" + p.position.x + ", " + p.position.y
              + "), int=(" + (int) p.position.x + ", " + (int) p.position.y + ").");
        } else if (Gdx.input.isKeyPressed(Keys.F9)) {
          timeSinceLastDebug = 0.0f;
          debugEntities();
        } else if (Gdx.input.isKeyPressed(Keys.F11)) {
          String inventString = "";

          for (Item i : playerEntity.getComponent(Inventory.class).inventoryList) {
            inventString += i.name + "\n";
          }

          Gdx.app.debug("PLAYER_INVENTORY", "Player inventory contains:\n" + inventString);
          timeSinceLastDebug = 0.0f;
        }
      }
    }
  }

  @Override
  public void resize(int width, int height) {
    camera = new OrthographicCamera(width, height);
  }

  public void nextLevel() {
    if (levelEntityFactory.loadNextLevel() == false) {
      Gdx.app.exit();
    }

    terrainCollisionSystem.setCollisionMap(levelEntityFactory.getCurrentLevelCollisionMap());
    movementSystem.doNextLevel(levelEntityFactory.getCurrentLevelCollisionMap().actualWidthInWorld());
    resetPlayer();
  }

  public void resetPlayer() {
    playerEntity.getComponent(Position.class).position.set(levelEntityFactory.getCurrentLevelSpawn());
    playerEntity.getComponent(Inventory.class).inventoryList.clear();
  }

  @Override
  public void resume() {

  }

  @Override
  public void dispose() {
    if (audioSystem != null) {
      audioSystem.dispose();
    }

    if (playerEntityFactory != null) {
      playerEntityFactory.dispose();
    }

    if (levelEntityFactory != null) {
      levelEntityFactory.dispose();
    }

    if (batch != null) {
      batch.dispose();
    }

    if (camera != null) {
      camera = null;
    }

    playerEntity = null;
    if (world != null) {
      world.dispose();
    }
  }

  public void debugEntities() {
    final String delim = ", ";

    Gdx.app.debug("DEBUG_ENTITIES", "There are " + world.getEntityManager().getActiveEntityCount()
        + " active entities. Enabled entities:");

    for (Entity e : world.getEntityManager().entities) {
      if (e == null || !e.isEnabled())
        continue;
      Gdx.app.debug("DEBUG_ENTITIES", "----- Entity ID: " + e.id + " -----");

      String components = "";

      for (Component c : e.getComponents()) {
        components += c.getClass().getSimpleName();

        if (c.getClass() == Linked.class) {
          components += "->" + e.getComponent(Linked.class).child.id;
        }

        components += delim;
      }

      components = components.substring(0, components.length() - delim.length());

      Gdx.app.debug("DEBUG_ENTITIES", components);
    }
  }
}




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