Android Open Source - TheFirstMyth02 A Star Node






From Project

Back to project page TheFirstMyth02.

License

The source code is released under:

MIT License

If you think the Android project TheFirstMyth02 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.game.commen;
//from   w w w  . java 2  s  .c o  m
 
public class AStarNode  implements Comparable<AStarNode>{

  /**
   *     G8  G1  G2
   *     G7    G3
   *     G6  G5  G4
   */
  private final static int G_1  = 10;
  private final static int G_2  = 14;
  private final static int G_3  = 10;
  private final static int G_4  = 14;
  private final static int G_5  = 10;
  private final static int G_6  = 14;
  private final static int G_7  = 10;
  private final static int G_8  = 14;
  private static final int G_0   = 10;
  
  private int g;  //    ???source??????????
  private int h;  //    ?????????????
  private int f;    //    f = g + h
  
  private int x;
  private int y;
  
  private AStarNode father;  //    ???

  public AStarNode(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public int getX() {
    return x;
  }

  public void setX(int x) {
    this.x = x;
  }

  public int getY() {
    return y;
  }

  public void setY(int y) {
    this.y = y;
  }
  
  public AStarNode getFather() {
    return father;
  }

  public void setFather(AStarNode father) {
    this.father = father;
  }

  public void init(AStarNode target) {
    this.g = 0;
    this.h = heuristicCostEstimate(this, target);
    this.f = g+h;
  }

  /**
   * ??H
   * @param source
   * @param target
   * @return
   */
  public int heuristicCostEstimate(AStarNode source, AStarNode target) {
//    return (int) Math.sqrt((source.x - target.x)*(source.x - target.x)+(source.y - target.y)*(source.y - target.y))*G_0;
    return (Math.abs(source.x - target.x) + Math.abs(source.y - target.y))*G_0;
  }

  @Override
  public int compareTo(AStarNode o) {
    return this.f < o.f ? -1 : 1;
  }
  
  @Override
  public boolean equals(Object obj) {
    if(obj==null || !(obj instanceof AStarNode) ) return false;
    AStarNode node = (AStarNode)obj;
    return node.x == this.x && node.y == this.y;
  }
  
//  
//  @Override
//  public int hashCode() {
//    return toString().hashCode();
//  }

  @Override
  public String toString(){
    return x+", "+y;
  }

  public void reCalculatorGAndH(AStarNode father, AStarNode target) {
    this.g = distinctG(father);
    this.h = heuristicCostEstimate(this, target);
    this.f = g+h;
    this.father = father;
  }

  public int distinctG(AStarNode father) {
    int offsetX = x-father.x;
    int offsety = y-father.y;
    int distinct = 0;
    if(offsetX==0 && offsety==-1)
      distinct = G_1;
    else if(offsetX==1 && offsety==-1)
      distinct = G_2;
    else if(offsetX==1 && offsety==0)
      distinct = G_3;
    else if(offsetX==1 && offsety==1)
      distinct = G_4;
    else if(offsetX==0 && offsety==1)
      distinct = G_5;
    else if(offsetX==-1 && offsety==1)
      distinct = G_6;
    else if(offsetX==-1 && offsety==0)
      distinct = G_7;
    else if(offsetX==-1 && offsety==-1)
      distinct = G_8;
    else
      try {
        throw new Exception("Unvalid relation between current node("+this+") and fater node("+father+")");
      } catch (Exception e) {
         e.printStackTrace();
      }
    return distinct+father.g;
  }
  
  public int getG(){
    return g;
  }

  /**
   * ???????????
   * @param node
   * @return
   */
  public boolean isBetter(AStarNode node) {
    return isGBetter(node);
  }
  
  public boolean isGBetter(AStarNode node){
    return g+distinctG(node) < node.g;
  }

  public boolean isFBetter(AStarNode node) {
    return f<node.f;
  }

  public int getF() {
    return f;
  }
}




Java Source Code List

.Test.java
com.game.base.BaseInfo.java
com.game.base.GameBackGround.java
com.game.base.GameChapterBaseSet_Shentan.java
com.game.base.GameMap.java
com.game.base.PubSet.java
com.game.commen.AStarMap.java
com.game.commen.AStarNode.java
com.game.commen.AStar.java
com.game.commen.ActionToDo.java
com.game.commen.BitmapTouchChecker.java
com.game.commen.BitmapUtil.java
com.game.commen.Direction.java
com.game.commen.EffectName.java
com.game.commen.GameXmlcommen.java
com.game.commen.GetImgCommen.java
com.game.commen.IrregularButton.java
com.game.commen.MapName.java
com.game.commen.Paintforziti.java
com.game.commen.ToDo.java
com.game.data.BaseInfo.java
com.game.data.RoleData_Main.java
com.game.data.RoleData.java
com.game.data.StroyTipData.java
com.game.effect.SpecialEffect.java
com.game.fengshen.GameActivity.java
com.game.fengshen.GameMainActivity.java
com.game.fengshen.GameView.java
com.game.fengshen.MoveTest.java
com.game.renwu.Objs.java
com.game.renwu.SpiritMain.java
com.game.renwu.Spirit_Main.java
com.game.renwu.Spirit_NPC.java
com.game.renwu.Spirit.java
com.game.renwu.Spiritgirl.java
com.game.stroy.MainStroy.java
com.game.tip.PublicTips.java