Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.08 KB | None | 0 0
  1. import javafx.geometry.Point2D;
  2. import javafx.scene.image.Image;
  3.  
  4. import java.awt.*;
  5. import java.io.FileInputStream;
  6.  
  7. /**
  8.  * Class used for basic wall following walking enemy.
  9.  * @author Peter Hawkins
  10.  * @version 1.0
  11.  * @since 1.0
  12.  */
  13. public class WallFollowingEnemy extends Enemy {
  14.     private String direction; //Direction enemy faces e.g "U" = Up
  15.     private Point2D position; //Position of enemy.
  16.     private Cell[][] currentMapState = Map.getInstance().getCellArray(); // MapState
  17.     private static Image im;
  18.  
  19.     /**
  20.      * Construct a new WallFollowingEnemy().
  21.      * @param point2D value to set a default point2D object.
  22.      */
  23.     public WallFollowingEnemy (Point2D point2D, String direction) {
  24.         this.position = point2D;
  25.         this.direction = direction;
  26.  
  27.         try{
  28.             System.out.println("loaded i guess");
  29.  
  30.             im = new Image(new FileInputStream("assets/images/slEnemy.png"));
  31.         } catch(Exception e) {
  32.             System.out.println("Could not load straight line enemy image!");
  33.         }
  34.     }
  35.  
  36.  
  37.     /**
  38.      * Method to calculate the enemies next move relative to the board state.
  39.      *
  40.      * @return enemies new position on the board.
  41.      */
  42.     @Override
  43.     public Point2D getNextMove() {
  44.  
  45.         int currentX = (int) position.getX();
  46.         int currentY = (int) position.getY();
  47.         Point2D nextMove = null;
  48.  
  49.         switch(direction) {
  50.             case "U":
  51.                 if(currentMapState[currentY - 1][currentX].getClass() == Wall.class) {
  52.                     System.out.println("Going up but there is a wall");
  53.                     if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  54.                         System.out.println("I can go left??");
  55.                         nextMove = new Point2D(-1,0);
  56.                     } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  57.                         nextMove = new Point2D(1,0);
  58.                     }
  59.                 }
  60.  
  61.                 if(nextMove == null) {
  62.  
  63.                     nextMove = checkUpLeft(currentX, currentY);
  64.                 }
  65.  
  66.             break;
  67.  
  68.             case "D":
  69.                 if(currentMapState[currentY + 1][currentX].getClass() == Wall.class) {
  70.                     System.out.println("Going DOWN but there is a wall");
  71.                     if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  72.                         nextMove = new Point2D(+1,0);
  73.                     } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  74.                         nextMove = new Point2D(-1,0);
  75.                     }
  76.                 }
  77.  
  78.                 if(nextMove == null) {
  79.                     nextMove = checkDownRight(currentX, currentY);
  80.                 }
  81.             break;
  82.  
  83.  
  84.             case "R":
  85.                 if(currentMapState[currentY][currentX + 1].getClass() == Wall.class) {
  86.                     if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  87.                         nextMove = new Point2D(0,1);
  88.                     } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  89.                         nextMove = new Point2D(0,-1);
  90.                     }
  91.                 }
  92.  
  93.                 if(nextMove == null) {
  94.                     nextMove = checkUpRight(currentX, currentY);
  95.                 }
  96.  
  97.             break;
  98.  
  99.             case "L":
  100.                 if(currentMapState[currentY][currentX + 1].getClass() == Wall.class) {
  101.                     if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  102.                         nextMove = new Point2D(0,-1);
  103.                     } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  104.                         nextMove = new Point2D(0,1);
  105.                     }
  106.                 }
  107.  
  108.                 if(nextMove == null) {
  109.                     nextMove = checkDownLeft(currentX, currentY);
  110.                 }
  111.             break;
  112.         }
  113.  
  114.         if(nextMove != null) {
  115.             return nextMove;
  116.         } else {
  117.  
  118.             switch(direction) {
  119.                 case "U": return new Point2D(0, -1);
  120.  
  121.                 case "D": return new Point2D(0, 1);
  122.  
  123.                 case "R": return new Point2D(1, 0);
  124.  
  125.                 case "L": return new Point2D(-1, 0);
  126.  
  127.             }
  128.             return new Point2D(0,0);
  129.         }
  130.     }
  131.  
  132.     private Point2D checkUpLeft(int currentX, int currentY) {
  133.  
  134.         if(currentMapState[currentY - 1][currentX - 1].getClass() == Wall.class) {
  135.  
  136.             if(direction.equals("R")) {
  137.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  138.                     return new Point2D(0,-1);
  139.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  140.                     return new Point2D(-1,0);
  141.                 }
  142.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  143.                 return new Point2D(-1,0);
  144.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  145.                 return new Point2D(0,-1);
  146.             }
  147.         }
  148.  
  149.         if ("R".equals(direction)) {
  150.             return null;
  151.         }
  152.         return checkUpRight(currentX, currentY);
  153.     }
  154.  
  155.     private Point2D checkDownLeft(int currentX, int currentY) {
  156.  
  157.         if(currentMapState[currentY + 1][currentX - 1].getClass() == Wall.class) {
  158.  
  159.             if(direction.equals("R")) {
  160.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  161.                     return new Point2D(0,1);
  162.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  163.                     return new Point2D(-1,0);
  164.                 }
  165.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  166.                 return new Point2D(-1,0);
  167.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  168.                 return new Point2D(0,1);
  169.             }
  170.         }
  171.  
  172.         if ("U".equals(direction)) {
  173.             return null;
  174.         }
  175.         return checkUpLeft(currentX, currentY);
  176.     }
  177.  
  178.     private Point2D checkUpRight(int currentX, int currentY) {
  179.  
  180.         if(currentMapState[currentY - 1][currentX + 1].getClass() == Wall.class) {
  181.  
  182.             if(direction.equals("L")) {
  183.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  184.                     return new Point2D(0, -1);
  185.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  186.                     return new Point2D(1,0);
  187.                 }
  188.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  189.                 return new Point2D(1,0);
  190.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  191.                 return new Point2D(0,-1);
  192.             }
  193.         }
  194.  
  195.         if ("D".equals(direction)) {
  196.             return null;
  197.         }
  198.         return checkDownRight(currentX, currentY);
  199.     }
  200.  
  201.     private Point2D checkDownRight(int currentX, int currentY) {
  202.  
  203.         if(currentMapState[currentY + 1][currentX + 1].getClass() == Wall.class) {
  204.  
  205.             if(direction.equals("L")) {
  206.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  207.                     return new Point2D(0,1);
  208.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  209.                     return new Point2D(1,0);
  210.                 }
  211.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  212.                 return new Point2D(1,0);
  213.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  214.                 return new Point2D(0,1);
  215.             }
  216.         }
  217.  
  218.         if ("L".equals(direction)) {
  219.             return null;
  220.         }
  221.         return checkDownLeft(currentX, currentY);
  222.     }
  223.  
  224.     public String getDirection() {
  225.         return direction;
  226.     }
  227.  
  228.     public void setDirection(String direction) {
  229.         this.direction = direction;
  230.     }
  231.  
  232.     @Override
  233.     public Point2D getPosition() {
  234.         return position;
  235.     }
  236.  
  237.     @Override
  238.     public void setPosition(Point2D position) {
  239.         this.position = position;
  240.     }
  241.  
  242.     @Override
  243.     public void updatePosition(Point2D pos) {
  244.         String newDirection;
  245.         if(pos.getX() > 0) {
  246.             setDirection("R");
  247.         } else if(pos.getX() < 0) {
  248.             setDirection("L");
  249.         } else if(pos.getY() > 0) {
  250.             setDirection("D");
  251.         } else if(pos.getY() < 0) {
  252.             setDirection("U");
  253.         }
  254.         this.position = new Point2D(position.getX() + pos.getX(), position.getY() + pos.getY());
  255.     }
  256.  
  257.     @Override
  258.     public Image getImage() {
  259.         return im;
  260.     }
  261.  
  262.     @Override
  263.     public String toString() {
  264.         return "Enemy " + (int) position.getX() + " " + (int) position.getY() + " WF " + direction;
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement