Node2D.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package pathgame.algorithm;
  2. import pathgame.tilemap.TileType;
  3. import java.util.ArrayList;
  4. /** Class for storing node data for the Dijkstra algorithm
  5. *
  6. */
  7. public class Node2D {
  8. private int weight;
  9. private int costSoFar = Integer.MAX_VALUE;
  10. private char prevOfPath = '\0';
  11. private int prevBoatPath = -1;
  12. private TileType type = TileType.LAND;
  13. private boolean isQAdded = false;
  14. private boolean hasExtraPaths = false;
  15. private boolean isBlocked = false;
  16. private boolean isTown = false;
  17. private ArrayList<ExtraPath> extraPaths = new ArrayList<>();
  18. /** Create new Node2D with given weight
  19. *
  20. * @param weight cost it takes to travel to this node
  21. */
  22. public Node2D(int weight) {
  23. this.weight = weight;
  24. }
  25. /** Returns the weight of the node
  26. *
  27. * @return the weight of the node
  28. */
  29. public int getWeight() {
  30. return weight;
  31. }
  32. /** Returns the lowest total cost of travelling to this node from where the algorithm started
  33. *
  34. * @return lowest cost to get to this node
  35. */
  36. public int getCostSoFar() {
  37. return costSoFar;
  38. }
  39. /** Sets the currently lowest total cost of travelling to this node from where the algorithm started
  40. *
  41. * @param costSoFar currently lowest cost to get to this node
  42. */
  43. public void setCostSoFar(int costSoFar) {
  44. this.costSoFar = costSoFar;
  45. }
  46. /** Returns a character indicating the previous node of the shortest path from the algorithm's starting point to this node
  47. *
  48. * @return direction of previous node
  49. */
  50. public char getPrevOfPath() {
  51. return prevOfPath;
  52. }
  53. /** Sets the character indicating the previous node of the shortest path from the algorithm's starting point to this node
  54. *
  55. * @param prevOfPath direction of previous node
  56. */
  57. public void setPrevOfPath(char prevOfPath) {
  58. this.prevOfPath = prevOfPath;
  59. }
  60. /** Returns the list of all ExtraPaths of this node
  61. *
  62. * @return list of ExtraPaths
  63. */
  64. public ArrayList<ExtraPath> getExtraPaths() {
  65. return extraPaths;
  66. }
  67. /** Creates an ExtraPath using the given parameters and adds it to the node
  68. *
  69. * @param dest coordinate of the destination of the path
  70. * @param pathWeight total weight to travel this path
  71. * @param pathCoords list of coordinates of the path
  72. */
  73. public void addExtraPath(Coord dest, int pathWeight, ArrayList<Coord> pathCoords) {
  74. hasExtraPaths = true;
  75. extraPaths.add(new ExtraPath(dest.getX(), dest.getY(), pathWeight, pathCoords));
  76. }
  77. /** Returns hasExtraPaths, which is true, if this node has ExtraPaths
  78. *
  79. * @return whether node has ExtraPaths
  80. */
  81. public boolean hasExtraPaths() {
  82. return hasExtraPaths;
  83. }
  84. /** Returns isQAdded, which is true, if the node has been added to the queue
  85. *
  86. * @return whether node has been added to the queue
  87. */
  88. public boolean isQAdded() {
  89. return isQAdded;
  90. }
  91. /** Sets isQAdded, which is true, if the node has been added to the queue
  92. *
  93. * @param QAdded whether node has been added to the queue
  94. */
  95. public void setQAdded(boolean QAdded) {
  96. isQAdded = QAdded;
  97. }
  98. /** Returns isBlocked, which is true, if this node can't be travelled to from adjacent nodes
  99. *
  100. * @return whether node is blocked from travel
  101. */
  102. public boolean isBlocked() {
  103. return isBlocked;
  104. }
  105. /** Sets isBlocked, which is true, if this node can't be travelled to from adjacent nodes
  106. *
  107. * @param blocked whether node is blocked from travel
  108. */
  109. public void setBlocked(boolean blocked) {
  110. isBlocked = blocked;
  111. }
  112. /** Returns the type of the Tile that is represented by this node
  113. *
  114. * @return the type of the tile
  115. */
  116. public TileType getType() {
  117. return type;
  118. }
  119. /** Sets the type of the Tile that is represented by this node
  120. *
  121. * @param type the type of the tile
  122. */
  123. public void setType(TileType type) {
  124. this.type = type;
  125. }
  126. /** Returns the index of the the previous ExtraPath
  127. *
  128. * @return the index of the the previous ExtraPath
  129. */
  130. public int getPrevBoatPath() {
  131. return prevBoatPath;
  132. }
  133. /** Sets the index of the the previous ExtraPath
  134. *
  135. * @param prevBoatPath the index of the the previous ExtraPath
  136. */
  137. public void setPrevBoatPath(int prevBoatPath) {
  138. this.prevBoatPath = prevBoatPath;
  139. }
  140. /** Returns isTown, which is true if the node is a town
  141. *
  142. * @return whether node is a town
  143. */
  144. public boolean isTown() {
  145. return isTown;
  146. }
  147. /** Sets isTown, which is true if the node is a town
  148. *
  149. * @param town whether node is a town
  150. */
  151. public void setTown(boolean town) {
  152. isTown = town;
  153. }
  154. }