Android Open Source - nahwc-g Level






From Project

Back to project page nahwc-g.

License

The source code is released under:

Apache License

If you think the Android project nahwc-g 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

/*
 *   Copyright 2013 oddlydrawn//w w w. j a  v a  2  s .  com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.tumblr.oddlydrawn.stupidworm;

import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.math.Vector2;

/** @author oddlydrawn */
public class Level {
  public static final int TILES_WIDTH = 60;
  public static final int TILES_HEIGHT = 40;
  public static final int WALL = 1;
  public final static int SIZE = 8;
  private final String COLLIDES = "collides";
  private final String START = "start";
  private final String LEVEL_PREFIX = "data/maps/level";
  private final String LEVEL_POSTFIX = ".tmx";
  private String level;
  private int[][] levelArray;
  private Vector2 startCoords;
  private int tmpX;
  private int tmpY;

  public Level (int levelNum) {
    startCoords = new Vector2();

    // Loads the level the user selected at the MainMenuScreen, obtained from God
    String lvl = Integer.toString(levelNum);
    level = LEVEL_PREFIX + lvl + LEVEL_POSTFIX;
    levelArray = new int[TILES_WIDTH][TILES_HEIGHT];
  }

  public void loadLevel () {
    TiledMap tiledMap;
    TiledMapTileLayer layer;
    Cell cell;

    // Creates the map objects and loads the appropriate level.
    tiledMap = new TiledMap();
    cell = new Cell();
    tiledMap = new TmxMapLoader().load(level);

    // Gets the collision layer from the map.
    layer = (TiledMapTileLayer)tiledMap.getLayers().get(0);
    cell = layer.getCell(0, 0);
    int width = layer.getWidth();
    int height = layer.getHeight();

    // Goes through all the tiles in the layer, looking for tiles with walls
    // and a single tile with the start position for the worm.
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        cell = layer.getCell(x, y);
        if (cell != null) {
          if (hasCollides(cell)) {
            levelArray[x][y] = 1;
          } else if (hasStart(cell)) {
            startCoords.x = x * SIZE;
            startCoords.y = y * SIZE;
            levelArray[x][y] = 0;
          } else {
            levelArray[x][y] = 0;
          }
        }
      }
    }
    tiledMap.dispose();
  }

  private boolean hasCollides (Cell cell) {
    if (cell.getTile().getProperties().containsKey(COLLIDES)) {
      return true;
    }
    return false;
  }

  private boolean hasStart (Cell cell) {
    if (cell.getTile().getProperties().containsKey(START)) {
      return true;
    }
    return false;
  }

  public Vector2 getStartCoords () {
    return startCoords;
  }

  public boolean isWallAt (float x, float y) {
    tmpX = (int)x;
    tmpY = (int)y;
    tmpX /= SIZE;
    tmpY /= SIZE;
    if (levelArray[tmpX][tmpY] == WALL) return true;

    return false;
  }

  public int[][] getLevelArray () {
    return levelArray;
  }
}




Java Source Code List

com.tumblr.oddlydrawn.nahwc.IOSLauncher.java
com.tumblr.oddlydrawn.nahwc.client.GwtLauncher.java
com.tumblr.oddlydrawn.nahwc.client.HtmlLauncher.java
com.tumblr.oddlydrawn.nahwc.desktop.DesktopLauncher.java
com.tumblr.oddlydrawn.stupidworm.AndroidLauncher.java
com.tumblr.oddlydrawn.stupidworm.Assets.java
com.tumblr.oddlydrawn.stupidworm.CheckCollision.java
com.tumblr.oddlydrawn.stupidworm.Controller.java
com.tumblr.oddlydrawn.stupidworm.Food.java
com.tumblr.oddlydrawn.stupidworm.Level.java
com.tumblr.oddlydrawn.stupidworm.MainMenuInterface.java
com.tumblr.oddlydrawn.stupidworm.MyGdxGame.java
com.tumblr.oddlydrawn.stupidworm.MyMusic.java
com.tumblr.oddlydrawn.stupidworm.NahwcGame.java
com.tumblr.oddlydrawn.stupidworm.Renderer.java
com.tumblr.oddlydrawn.stupidworm.SavedStuff.java
com.tumblr.oddlydrawn.stupidworm.Vector2Marked.java
com.tumblr.oddlydrawn.stupidworm.Worm.java
com.tumblr.oddlydrawn.stupidworm.screens.GameScreen.java
com.tumblr.oddlydrawn.stupidworm.screens.LicenseScreen.java
com.tumblr.oddlydrawn.stupidworm.screens.LoadingScreen.java
com.tumblr.oddlydrawn.stupidworm.screens.MainMenuScreen.java