Android Open Source - DivisionByZero Level Map






From Project

Back to project page DivisionByZero.

License

The source code is released under:

Apache License

If you think the Android project DivisionByZero 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.ggstudios.divisionbyzero;
/*w  ww  . j  a  va2 s .com*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.util.SparseArray;

public class LevelMap {

  private Context context;

  private InputStream inputStream;
  private InputStreamReader inputStreamReader;
  private BufferedReader bufferedReader;

  private SparseArray<LevelNode> arr = new SparseArray<LevelNode>();

  public LevelMap(Context context) {
    this.context = context;
  }

  public void loadFrom(int resId) throws IOException {
    inputStream = context.getResources().openRawResource(resId);
    inputStreamReader = new InputStreamReader(inputStream);
    bufferedReader = new BufferedReader(inputStreamReader);

    String line;
    while((line = getNextMeaningfulLine()) != null) {
      LevelNode n = new LevelNode();
      n.id = Integer.valueOf(line);
      line = getNextMeaningfulLine();
      n.hintX = Integer.valueOf(line);
      line = getNextMeaningfulLine();
      n.hintY = Integer.valueOf(line);
      line = getNextMeaningfulLine();
      n.childCount = Integer.valueOf(line);

      for(int i = 0; i < n.childCount; i++) {
        line = getNextMeaningfulLine();
        n.childIds[i] = Integer.valueOf(line);
      }

      arr.put(n.id, n);
    }

    // once we are all done with putting in the ids
    // we can then link our tree together
    // beginning with the first node...

    LinkedList<LevelNode> toLink = new LinkedList<LevelNode>();
    LevelNode top = arr.get(0);
    top.depth = 0;
    toLink.add(top);

    while(toLink.size() != 0) {
      LevelNode n = toLink.get(0);
      n.status = LevelNode.STATUS_CLOSED;
      for(int i = 0; i < n.childCount; i++) {
        final int childId = n.childIds[i];
        final LevelNode node = arr.get(childId);
        node.depth = n.depth + 1;
        n.children.add(node);
        toLink.add(node);
      }
      toLink.removeFirst();
    }

    arr.get(0).status = LevelNode.STATUS_OPEN;
  }

  public void setCompleted(int levelId) {
    LevelNode ln = arr.get(levelId);
    ln.status = LevelNode.STATUS_COMPLETED;
    for(LevelNode n : ln.children) {
      if(n.status == LevelNode.STATUS_CLOSED) {
        n.status = LevelNode.STATUS_OPEN;
      }
    }
  }

  private String getNextMeaningfulLine() throws IOException {
    String line;

    while((line = bufferedReader.readLine()) != null) {

      line = line.trim();
      if(line.length() == 0 || line.charAt(0) == '/') {
        // skip all empty/commented lines
        continue;
      }

      return line;
    }

    return null;
  }

  public SparseArray<LevelNode> getRaw() {
    return arr;
  }

  public LevelNode getNode(int id) {
    return arr.get(id);
  }

  public LevelNode getTopNode() {
    return arr.get(0);
  }

  public static class ExtraLevelInfo {
    String levelName;
    int waveCount;
    int difficulty;
    int mapAcross, mapDown;

    private static final int MAX_TILES_SMALL = 122;
    private static final int MAX_TILES_MEDIUM = 200;    

    private static final int 
    DIFFICULTY_UNRATED = 0,
    DIFFICULTY_EASY = 1,
    DIFFICULTY_MEDIUM = 2,
    DIFFICULTY_HARD = 3,
    DIFFICULTY_BRUTAL = 4,
    DIFFICULTY_EX = 5;

    String getMapSize() {
      int totalTiles = mapAcross * mapDown;
      if(totalTiles < MAX_TILES_SMALL) {
        return "Small";
      } else if(totalTiles > MAX_TILES_MEDIUM) {
        return "Large";
      } else {
        return "Medium";
      }
    }

    public String getDifficulty() {
      switch(difficulty) {
      case DIFFICULTY_UNRATED:
        return "Unrated";
      case DIFFICULTY_EASY:
        return "Easy";
      case DIFFICULTY_MEDIUM:
        return "Medium";
      case DIFFICULTY_HARD:
        return "Hard";
      case DIFFICULTY_BRUTAL:
        return "Brutal";
      case DIFFICULTY_EX:
        return "EX";
      default:
        return "UNDEFINED";
      }
    }
  }

  public static class LevelNode {
    public static final int 
    STATUS_CLOSED = 0, 
    STATUS_OPEN = 1,
    STATUS_COMPLETED = 2;

    public int depth;

    public int status;

    public int id;
    public int childCount;
    public int[] childIds = new int[3];

    public int hintX, hintY;

    public Object view;

    private boolean loaded = false;

    private ExtraLevelInfo levelInfo;

    public List<LevelNode> children = new LinkedList<LevelNode>();

    public int getHeight() {
      if(childCount == 0)
        return 0;
      else {
        int max = 0;
        for(LevelNode n : children) {
          max = Math.max(max, n.getHeight());
        }

        return max + 1;
      }
    }

    public boolean isLoaded() {
      return loaded;
    }

    public void loadLevelData(Context context) {
      LevelManager lm = new LevelManager();
      lm.setMockMode(true);
      int id = getLevelResId(context);
      if(id != 0) {
        lm.loadLevelFromFile(context, id);

        levelInfo = new ExtraLevelInfo();
        levelInfo.difficulty = lm.getLevelDifficulty();
        levelInfo.levelName = lm.getLevelName();
        if(lm.isCustomMap()) {
          levelInfo.mapAcross = lm.getCustomMapW();
          levelInfo.mapDown = lm.getCustomMapH();
        } else {
          Map.Builder builder = new Map.Builder();
          builder.setType(lm.getMapType());
          levelInfo.mapAcross = builder.getTilesAcross();
          levelInfo.mapDown = builder.getTilesDown();
        }
        levelInfo.waveCount = lm.getWaveCount();
      }
    }

    public int getLevelResId(Context context) {
      return context.getResources().getIdentifier( "lv_" + id , "raw" , context.getPackageName());
    }

    public ExtraLevelInfo getExtraInfo() {
      return levelInfo;
    }
  }
}




Java Source Code List

com.ggstudios.divisionbyzero.ActivityAbout.java
com.ggstudios.divisionbyzero.ActivityLevelSelection.java
com.ggstudios.divisionbyzero.ActivityMainMenu.java
com.ggstudios.divisionbyzero.ActivitySettings.java
com.ggstudios.divisionbyzero.ActivitySplashScreen.java
com.ggstudios.divisionbyzero.ApplicationMain.java
com.ggstudios.divisionbyzero.BaseActivity.java
com.ggstudios.divisionbyzero.BaseDialog.java
com.ggstudios.divisionbyzero.BulletManager.java
com.ggstudios.divisionbyzero.Bullet.java
com.ggstudios.divisionbyzero.Button.java
com.ggstudios.divisionbyzero.Circle.java
com.ggstudios.divisionbyzero.ClickableCollection.java
com.ggstudios.divisionbyzero.Clickable.java
com.ggstudios.divisionbyzero.ConfirmDialog.java
com.ggstudios.divisionbyzero.Core.java
com.ggstudios.divisionbyzero.CustomGLSurfaceView.java
com.ggstudios.divisionbyzero.DialogManager.java
com.ggstudios.divisionbyzero.DrawableCollection.java
com.ggstudios.divisionbyzero.DrawableString.java
com.ggstudios.divisionbyzero.Drawable.java
com.ggstudios.divisionbyzero.EndDialog.java
com.ggstudios.divisionbyzero.Event.java
com.ggstudios.divisionbyzero.ExplosionGenerator.java
com.ggstudios.divisionbyzero.ExtrasManager.java
com.ggstudios.divisionbyzero.FontManager.java
com.ggstudios.divisionbyzero.GameRenderer.java
com.ggstudios.divisionbyzero.GameUpdater.java
com.ggstudios.divisionbyzero.Game.java
com.ggstudios.divisionbyzero.Grid.java
com.ggstudios.divisionbyzero.Hud.java
com.ggstudios.divisionbyzero.InGameMenu.java
com.ggstudios.divisionbyzero.InfoDialog.java
com.ggstudios.divisionbyzero.Label.java
com.ggstudios.divisionbyzero.LevelManager.java
com.ggstudios.divisionbyzero.LevelMap.java
com.ggstudios.divisionbyzero.LineGuide.java
com.ggstudios.divisionbyzero.MainActivity.java
com.ggstudios.divisionbyzero.Map.java
com.ggstudios.divisionbyzero.MessageDialog.java
com.ggstudios.divisionbyzero.ParticleEngine.java
com.ggstudios.divisionbyzero.PathFinder.java
com.ggstudios.divisionbyzero.PauseMenu.java
com.ggstudios.divisionbyzero.PictureBox.java
com.ggstudios.divisionbyzero.Player.java
com.ggstudios.divisionbyzero.PopupMenu.java
com.ggstudios.divisionbyzero.Rectangle.java
com.ggstudios.divisionbyzero.Shader.java
com.ggstudios.divisionbyzero.SpawnManager.java
com.ggstudios.divisionbyzero.SpriteAnimation.java
com.ggstudios.divisionbyzero.SpriteManager.java
com.ggstudios.divisionbyzero.Sprite.java
com.ggstudios.divisionbyzero.Statbar.java
com.ggstudios.divisionbyzero.StateManager.java
com.ggstudios.divisionbyzero.TargetRectangle.java
com.ggstudios.divisionbyzero.TextureManager.java
com.ggstudios.divisionbyzero.TowerInfoDialog.java
com.ggstudios.divisionbyzero.TowerLibrary.java
com.ggstudios.divisionbyzero.TowerManager.java
com.ggstudios.divisionbyzero.TowerMenu.java
com.ggstudios.divisionbyzero.Tower.java
com.ggstudios.divisionbyzero.UpdatableCollection.java
com.ggstudios.divisionbyzero.Updatable.java
com.ggstudios.divisionbyzero.UpgradeDialog.java
com.ggstudios.divisionbyzero.Utils.java
com.ggstudios.divisionbyzero.VBO.java
com.ggstudios.divisionbyzero.WaveControlDialog.java
com.ggstudios.divisionbyzero.WaveControlHud.java
com.ggstudios.divisionbyzero.ZoomControl.java
com.ggstudios.utils.BitmapUtils.java
com.ggstudios.utils.BufferUtils.java
com.ggstudios.utils.DebugLog.java
com.ggstudios.utils.ShaderUtils.java
com.ggstudios.widget.LevelMapBackground.java
fix.android.opengl.GLES20.java