Guest User

Untitled

a guest
Jan 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.awt.*;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. /**
  8. * Represents the path that the monsters follow in a tower defense game. The path is a collection
  9. * of discrete Point objects
  10. *
  11. * @author Elizabeth
  12. *
  13. */
  14. public class Path {
  15. private ArrayList<Point> points;
  16. private final int STEP = 5; //distance between points
  17.  
  18. /**
  19. * Constructs a path from a text file. The first line of the text file is the starting x and y
  20. * coordinates of the path. Every line thereafter contains a direction, and a distance. All
  21. * distances should be in increments of 5
  22. *
  23. * @param pathFile the name of the file to be read from
  24. */
  25. public Path(String pathFile){
  26. points = new ArrayList<Point>();
  27. File f = new File(pathFile);
  28. try {
  29. Scanner scan = new Scanner(f);
  30. String s = scan.next();
  31. String[] coords = s.split(",");
  32. points.add(new Point(Integer.parseInt(coords[0]),Integer.parseInt(coords[1])));
  33. while(scan.hasNext()){
  34. s = scan.next();
  35. coords = s.split(",");
  36. int dist = Integer.parseInt(coords[1]);
  37. Point last = points.get(points.size()-1);
  38.  
  39. if(coords[0].equals("r")){
  40. for(int i = STEP; i<=dist;i+=STEP){
  41. points.add(new Point(last.x+i,last.y));
  42. }
  43. }
  44.  
  45. if(coords[0].equals("l")){
  46. for(int i = STEP; i<=dist;i+=STEP){
  47. points.add(new Point(last.x-i,last.y));
  48. }
  49. }
  50.  
  51. if(coords[0].equals("u")){
  52. for(int i = STEP; i<=dist;i+=STEP){
  53. points.add(new Point(last.x,last.y-i));
  54. }
  55. }
  56.  
  57. if(coords[0].equals("d")){
  58. for(int i = STEP; i<=dist;i+=STEP){
  59. points.add(new Point(last.x,last.y+i));
  60. }
  61. }
  62. }
  63. } catch (FileNotFoundException e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. }
  67.  
  68.  
  69.  
  70. }
  71.  
  72. /**
  73. * Gets the next point in the path based on a current point and a speed.
  74. * A speed of one will take you to the next point in the path - 5 pixels away.
  75. *
  76. * @param current starting Point
  77. * @param speed int determining how far down the path to travel
  78. * @return the new position
  79. */
  80. public Point nextPosition(Point current, int speed){
  81. int index = points.indexOf(current);
  82. if(index+speed < points.size())
  83. return points.get(index+speed);
  84. else
  85. return getEnd();
  86. }
  87.  
  88. /**
  89. * Draws the path
  90. *
  91. * @param g - the Graphics object on which the path is drawn
  92. */
  93. public void draw(Graphics g){
  94. g.setColor(Color.red);
  95. for(int i = 0; i<points.size()-1; i++){
  96. g.drawLine(points.get(i).x,points.get(i).y,points.get(i+1).x,points.get(i+1).y);
  97. }
  98. g.setColor(Color.black);
  99. }
  100.  
  101. /**
  102. * Gets the starting point of the path
  103. *
  104. * @return the starting Point of the path
  105. */
  106. public Point getStart(){
  107. return points.get(0);
  108. }
  109.  
  110. /**
  111. * Gets the ending point of the path
  112. *
  113. * @return the ending Point of the path
  114. */
  115. public Point getEnd(){
  116. return points.get(points.size()-1);
  117. }
  118.  
  119. }
Add Comment
Please, Sign In to add comment