Package settlers.game.map

Source Code of settlers.game.map.SettlerMap

package settlers.game.map;

import java.util.Arrays;

import settlers.game.DefaultGameEnums.TileTypes;
import settlers.game.data.MapCoord;

abstract public class SettlerMap {
  public int height;
  public int width;
  protected int[][] NUM_OF_CHANCE_TOKENS;

  public int[][] chance;

  /**
   * Is the first tile (top left) below the highest tile?
   */
  public boolean isOriginDown;

  public int getInstancesOfChanceTile(int chanceTile) {
    for (int i = 0; i < NUM_OF_CHANCE_TOKENS[0].length; i++) {
      if (chanceTile == NUM_OF_CHANCE_TOKENS[0][i])
        return NUM_OF_CHANCE_TOKENS[1][i];
    }
    return 0;
  }

  abstract public TileTypes getTileAt(MapCoord coord);

  abstract public boolean isLand(int x, int y);

  abstract public TileTypes getTileAt(int x, int y);

  abstract public String tileMapAsString();

  public static boolean areTilesAdjacent(boolean isOriginDown,
      MapCoord coord1, MapCoord coord2) {
    int[][] offsetToNeighbours = { { 0, 1 }, { 0, -1 }, { 1, 0 },
        { -1, 0 }, { 1, 1 }, { -1, 1 } };

    if (isPartofDownRow(isOriginDown, coord1.getX())) {
      offsetToNeighbours[4] = new int[] { -1, -1 };
      offsetToNeighbours[5] = new int[] { 1, -1 };
    }

    for (int i = 0; i < offsetToNeighbours.length; i++) {
      if (coord1.getX() == coord2.getX() + offsetToNeighbours[i][0]
          && coord1.getY() == coord2.getY()
              + offsetToNeighbours[i][1])
        return true;
    }
    return false;
  }

  public static boolean tilesVeticallyAdjacent(boolean isOriginDown,
      MapCoord coord1, MapCoord coord2) {
    int[][] offsetToVerticalNeighbours = { { 0, 1 }, { 0, -1 } };

    for (int i = 0; i < offsetToVerticalNeighbours.length; i++) {
      if (coord1.getX() == coord2.getX()
          + offsetToVerticalNeighbours[i][0]
          && coord1.getY() == coord2.getY()
              + offsetToVerticalNeighbours[i][1])
        return true;
    }
    return false;
  }

  public static boolean tilesDiagonalLeft(boolean isOriginDown,
      MapCoord coord1, MapCoord coord2) {
    int[][] offsetToNeighbours = { { 1, 0 }, { -1, 0 } };

    if (isPartofDownRow(isOriginDown, coord1.getX())) {
      offsetToNeighbours[0] = new int[] { 1, 1 };
      offsetToNeighbours[1] = new int[] { -1, -1 };
    }

    for (int i = 0; i < offsetToNeighbours.length; i++) {
      if (coord1.getX() == coord2.getX() + offsetToNeighbours[i][0]
          && coord1.getY() == coord2.getY()
              + offsetToNeighbours[i][1])
        return true;
    }
    return false;
  }

  public static boolean tilesDiagonalRight(boolean isOriginDown,
      MapCoord coord1, MapCoord coord2) {
    int[][] offsetToNeighbours = { { 1, -1 }, { -1, 1 } };

    if (isPartofDownRow(isOriginDown, coord1.getX())) {
      offsetToNeighbours[0] = new int[] { 1, 0 };
      offsetToNeighbours[1] = new int[] { -1, 0 };
    }

    for (int i = 0; i < offsetToNeighbours.length; i++) {
      if (coord1.getX() == coord2.getX() + offsetToNeighbours[i][0]
          && coord1.getY() == coord2.getY()
              + offsetToNeighbours[i][1])
        return true;
    }
    return false;
  }

  public static boolean tilesRightFacing(boolean isOriginDown,
      MapCoord coord1, MapCoord coord2, MapCoord coord3) {
    int x1 = 0, x2 = -1, y1 = -1, y2 = 1;

    if (isPartofDownRow(isOriginDown, coord1.getX())) {
      x1 = 0;
      y1 = -1;
      x2 = -1;
      y2 = 0;
    }

    if (coord1.getX() == coord2.getX() + x1
        && coord1.getY() == coord2.getY() + y1
        && coord2.getX() == coord3.getX() + x2
        && coord2.getY() == coord3.getY() + y2)
      return true;
    else
      return false;
  }

  public static boolean isPartOfMap(SettlerMap map, int x, int y) {
    if (x > 0 && y > 0 && x < map.width && y < map.height)
      return true;
    else
      return false;
  }

  public static boolean isIntersection(boolean isOriginDown,
      MapCoord[] location) {
    switch (location.length) {
    case 2:
      return areTilesAdjacent(isOriginDown, location[0], location[1]);
    case 3:
      return areTilesAdjacent(isOriginDown, location[0], location[1]);
    default:
      return false;
    }
  }

  public static boolean isPartofDownRow(boolean isOriginDown, int x) {
    if (isOriginDown && Math.abs(x) % 2 == 0 || !isOriginDown
        && Math.abs(x) % 2 == 1)
      return true;
    return false;
  }

  public static MapCoord[] tilesActivatedByRoll(int roll, SettlerMap map) {
    int tilesLeftToBeActivated = map.getInstancesOfChanceTile(roll);
    MapCoord[] activatedTiles = new MapCoord[tilesLeftToBeActivated];

    for (int x = 0; x < map.chance.length; x++) {
      for (int y = 0; y < map.chance[0].length; y++) {
        if (map.chance[x][y] == roll) {
          activatedTiles[activatedTiles.length
              - tilesLeftToBeActivated] = new MapCoord(x, y);
          tilesLeftToBeActivated--;
          if (tilesLeftToBeActivated == 0)
            break;
        }
      }
    }
    System.out.print(Arrays.toString(activatedTiles));
    return activatedTiles;
  }

  public static MapCoord findFirstInstance(TileTypes type, SettlerMap map) {
    for (int x = 0; x < map.width; x++) {
      for (int y = 0; y < map.height; y++) {
        if (map.getTileAt(x, y) == type)
          return new MapCoord(x, y);
      }
    }
    return null;
  }
}
TOP

Related Classes of settlers.game.map.SettlerMap

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.