Android Open Source - feup-lpoo-android-tower-defense Path Cluster






From Project

Back to project page feup-lpoo-android-tower-defense.

License

The source code is released under:

MIT License

If you think the Android project feup-lpoo-android-tower-defense 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 pt.up.fe.lpoo.towerdefense;
/*  w w w.  j  a  v  a 2 s  .c om*/
import java.util.ArrayList;

/**
 * This class contains the information of every path used collectively by enemies in the game
 *  as well as methods to create new paths
 *  
 * @author Joao Marinheiro
 * @author Luis Cleto
 */
public class PathCluster {
  private ArrayList<PathInfo> paths;

  /**
   * Default constructor for the PathCluster class. Initializes the paths array
   */
  public PathCluster(){
    paths = new ArrayList<PathInfo>();
  }

  /**
   * Adds a new path to the path bank
   * @param path path to be added to the path bank
   * @param xOrig starting x position of the path
   * @param yOrig starting y position of the path
   * @param xDest final x position of the path
   * @param yDest final y position of the path
   * @param enemyType enemy type for which the path was calculated (different enemies have different optimal paths)
   */
  public void addPath(ArrayList<Tile> path, int xOrig, int yOrig, int xDest, int yDest, int enemyType){
    paths.add(new PathInfo(path,xOrig,yOrig,xDest,yDest, enemyType));
  }

  /**
   * Searches for a path between the two given points for the given enemy type
   * @param xOrig starting x position for the path
   * @param yOrig starting y position for the path
   * @param xDest final x position for the path
   * @param yDest final y position for the path
   * @param enemyType enemy type for which the path was calculated
   * @return returns the path if it exists, null otherwise
   */
  public ArrayList<Tile> getPath(int xOrig, int yOrig, int xDest, int yDest, int enemyType){
    for(int i=0; i < paths.size(); i++){
      PathInfo cur = paths.get(i);
      if(xDest == cur.xDest && yDest == cur.yDest && xOrig == cur.xOrig && yOrig == cur.yOrig
          && enemyType == cur.enemyType)
        return cur.path;
    }
    return null;
  }
  /**
   * Updates all the paths that may have been affected by a recent alteration to a Tile's status
   *  (like building/remove a defense object)
   * @param changedTile the tile that suffered the change
   * @param tileGrid the map of all the tiles
   */
  public void recalculatePathsWithTile(Tile changedTile, ArrayList<ArrayList<Tile>> tileGrid){
    for(int i=0; i < paths.size(); i++){
      if(pathContains(paths.get(i).path, changedTile) || !changedTile.hasDefenseObject()){
        Float[] weights;
        switch(paths.get(i).enemyType){
        case Slime.enemyType:
          weights = Slime.tileWeights;
          break;
        case Golem.enemyType:
          weights = Golem.tileWeights;
          break;
        case Skull.enemyType:
          weights = Skull.tileWeights;
          break;
        default:
          weights = Slime.tileWeights;
          break;
        }
        paths.get(i).recalculate(weights, tileGrid);
      }
    }
  }

  private boolean pathContains(ArrayList<Tile> path, Tile changedTile) {
    for(int i=0; i < path.size(); i++){
      if(path.get(i).equals(changedTile))
        return true;
    }
    return false;
  }

  /**
   * Class for storing the information for a certain path
   * @author Joao Marinheiro
   * @author Luis Cleto
   */
  private class PathInfo{
    private ArrayList<Tile> path;
    private int xOrig, yOrig, xDest, yDest;
    private int enemyType;
    /**
     * Constructor for the PathInfo object
     * @param path array of tiles that make up the path
     * @param xOrig starting x position of the path
     * @param yOrig starting y position of the path
     * @param xDest final x position of the path
     * @param yDest final y position of the path
     * @param enemyType type of enemy the path was calculated for
     */
    PathInfo(ArrayList<Tile> path, int xOrig, int yOrig, int xDest, int yDest, int enemyType){
      this.path = path;
      this.xOrig = xOrig;
      this.yOrig = yOrig;
      this.xDest = xDest;
      this.yDest = yDest;
      this.enemyType = enemyType;
    }
    /**
     * Recalculates the path between the starting and end point of the current path
     * @param weights tile weights to be used for the new calculation
     * @param tileGrid grid with all of the map's tiles
     */
    public void recalculate(Float[] weights, ArrayList<ArrayList<Tile>> tileGrid) {
      ArrayList<Tile> newPath = Enemy.getPathToPoint(path.get(0).getGridX(), path.get(0).getGridY(), 
          path.get(path.size()-1).getGridX(), path.get(path.size()-1).getGridY(), tileGrid, weights);
      int i=0;
      for(;i < newPath.size(); i++){
        if(i < path.size())
          path.set(i, newPath.get(i));
        else
          path.add(newPath.get(i));
      }
      while(i < path.size()){
        path.remove(path.size()-1);
      }
    }
  }
}




Java Source Code List

pt.up.fe.lpoo.framework.Audio.java
pt.up.fe.lpoo.framework.FileIO.java
pt.up.fe.lpoo.framework.Game.java
pt.up.fe.lpoo.framework.Graphics.java
pt.up.fe.lpoo.framework.Image.java
pt.up.fe.lpoo.framework.Input.java
pt.up.fe.lpoo.framework.Music.java
pt.up.fe.lpoo.framework.Pool.java
pt.up.fe.lpoo.framework.Screen.java
pt.up.fe.lpoo.framework.Sound.java
pt.up.fe.lpoo.framework.implementation.AndroidAudio.java
pt.up.fe.lpoo.framework.implementation.AndroidFastRenderView.java
pt.up.fe.lpoo.framework.implementation.AndroidFileIO.java
pt.up.fe.lpoo.framework.implementation.AndroidGame.java
pt.up.fe.lpoo.framework.implementation.AndroidGraphics.java
pt.up.fe.lpoo.framework.implementation.AndroidImage.java
pt.up.fe.lpoo.framework.implementation.AndroidInput.java
pt.up.fe.lpoo.framework.implementation.AndroidMusic.java
pt.up.fe.lpoo.framework.implementation.AndroidSound.java
pt.up.fe.lpoo.framework.implementation.MultiTouchHandler.java
pt.up.fe.lpoo.framework.implementation.SingleTouchHandler.java
pt.up.fe.lpoo.framework.implementation.TouchHandler.java
pt.up.fe.lpoo.towerdefense.Animation.java
pt.up.fe.lpoo.towerdefense.Assets.java
pt.up.fe.lpoo.towerdefense.BarricadeDefense.java
pt.up.fe.lpoo.towerdefense.BoulderProjectile.java
pt.up.fe.lpoo.towerdefense.BoulderTower.java
pt.up.fe.lpoo.towerdefense.BuildingOption.java
pt.up.fe.lpoo.towerdefense.DefenseObject.java
pt.up.fe.lpoo.towerdefense.DescriptionObject.java
pt.up.fe.lpoo.towerdefense.Enemy.java
pt.up.fe.lpoo.towerdefense.FreezeTower.java
pt.up.fe.lpoo.towerdefense.GameElements.java
pt.up.fe.lpoo.towerdefense.GameObj.java
pt.up.fe.lpoo.towerdefense.Golem.java
pt.up.fe.lpoo.towerdefense.InstructionsScreen.java
pt.up.fe.lpoo.towerdefense.LevelScreen.java
pt.up.fe.lpoo.towerdefense.LevelSelectionScreen.java
pt.up.fe.lpoo.towerdefense.LoadingScreen.java
pt.up.fe.lpoo.towerdefense.MainMenuScreen.java
pt.up.fe.lpoo.towerdefense.Messages.java
pt.up.fe.lpoo.towerdefense.MusicPlayer.java
pt.up.fe.lpoo.towerdefense.PathCluster.java
pt.up.fe.lpoo.towerdefense.PoisonTower.java
pt.up.fe.lpoo.towerdefense.Projectile.java
pt.up.fe.lpoo.towerdefense.Skull.java
pt.up.fe.lpoo.towerdefense.Slime.java
pt.up.fe.lpoo.towerdefense.SplashLoadingScreen.java
pt.up.fe.lpoo.towerdefense.StatusEffect.java
pt.up.fe.lpoo.towerdefense.TarPoolDefense.java
pt.up.fe.lpoo.towerdefense.TextParser.java
pt.up.fe.lpoo.towerdefense.Tile.java
pt.up.fe.lpoo.towerdefense.TowerDefenseGame.java
pt.up.fe.lpoo.towerdefense.TurretProjectile.java
pt.up.fe.lpoo.towerdefense.TurretTower.java
pt.up.fe.lpoo.towerdefense.Wave.java