Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.12 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 Radu Bucurescu
  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.                     if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  53.                         nextMove = new Point2D(-1,0);
  54.                     } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  55.                         nextMove = new Point2D(1,0);
  56.                     }
  57.                 }
  58.  
  59.                 if(nextMove == null) {
  60.  
  61.                     nextMove = checkUpLeft(currentX, currentY);
  62.                 }
  63.  
  64.             break;
  65.  
  66.             case "D":
  67.                 if(currentMapState[currentY + 1][currentX].getClass() == Wall.class) {
  68.                     if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  69.                         nextMove = new Point2D(+1,0);
  70.                     } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  71.                         nextMove = new Point2D(-1,0);
  72.                     }
  73.                 }
  74.  
  75.                 if(nextMove == null) {
  76.                     nextMove = checkDownRight(currentX, currentY);
  77.                 }
  78.             break;
  79.  
  80.  
  81.             case "R":
  82.                 if(currentMapState[currentY][currentX + 1].getClass() == Wall.class) {
  83.                     if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  84.                         nextMove = new Point2D(0,1);
  85.                     } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  86.                         nextMove = new Point2D(0,-1);
  87.                     }
  88.                 }
  89.  
  90.                 if(nextMove == null) {
  91.                     nextMove = checkUpRight(currentX, currentY);
  92.                 }
  93.  
  94.             break;
  95.  
  96.             case "L":
  97.                 if(currentMapState[currentY][currentX + 1].getClass() == Wall.class) {
  98.                     if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  99.                         nextMove = new Point2D(0,-1);
  100.                     } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  101.                         nextMove = new Point2D(0,1);
  102.                     }
  103.                 }
  104.  
  105.                 if(nextMove == null) {
  106.                     nextMove = checkDownLeft(currentX, currentY);
  107.                 }
  108.             break;
  109.         }
  110.  
  111.         if(nextMove != null) {
  112.             return nextMove;
  113.         } else {
  114.  
  115.             switch(direction) {
  116.                 case "U": return new Point2D(0, -1);
  117.  
  118.                 case "D": return new Point2D(0, 1);
  119.  
  120.                 case "R": return new Point2D(1, 0);
  121.  
  122.                 case "L": return new Point2D(-1, 0);
  123.  
  124.             }
  125.             return new Point2D(0,0);
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * Checks the upper left corner to consider its next position.
  131.      * @param currentX The enemie's current x coordinate.
  132.      * @param currentY The enemie's current y coordinate.
  133.      * @return The direction of the movement if it's executable, null otherwise.
  134.      */
  135.     private Point2D checkUpLeft(int currentX, int currentY) {
  136.  
  137.         if(currentMapState[currentY - 1][currentX - 1].getClass() == Wall.class) {
  138.  
  139.             if(direction.equals("R")) {
  140.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  141.                     return new Point2D(0,-1);
  142.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  143.                     return new Point2D(-1,0);
  144.                 }
  145.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  146.                 return new Point2D(-1,0);
  147.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  148.                 return new Point2D(0,-1);
  149.             }
  150.         }
  151.  
  152.         if ("R".equals(direction)) {
  153.             return null;
  154.         }
  155.         return checkUpRight(currentX, currentY);
  156.     }
  157.  
  158.     /**
  159.      * Checks the lower left corner to consider its next position.
  160.      * @param currentX The enemie's current x coordinate.
  161.      * @param currentY The enemie's current y coordinate.
  162.      * @return The direction of the movement if it's executable, null otherwise.
  163.      */
  164.     private Point2D checkDownLeft(int currentX, int currentY) {
  165.  
  166.         if(currentMapState[currentY + 1][currentX - 1].getClass() == Wall.class) {
  167.  
  168.             if(direction.equals("R")) {
  169.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  170.                     return new Point2D(0,1);
  171.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  172.                     return new Point2D(-1,0);
  173.                 }
  174.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  175.                 return new Point2D(-1,0);
  176.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  177.                 return new Point2D(0,1);
  178.             }
  179.         }
  180.  
  181.         if ("U".equals(direction)) {
  182.             return null;
  183.         }
  184.         return checkUpLeft(currentX, currentY);
  185.     }
  186.  
  187.     /**
  188.      * Checks the upper right corner to consider its next position.
  189.      * @param currentX The enemie's current x coordinate.
  190.      * @param currentY The enemie's current y coordinate.
  191.      * @return The direction of the movement if it's executable, null otherwise.
  192.      */
  193.     private Point2D checkUpRight(int currentX, int currentY) {
  194.  
  195.         if(currentMapState[currentY - 1][currentX + 1].getClass() == Wall.class) {
  196.  
  197.             if(direction.equals("L")) {
  198.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  199.                     return new Point2D(0, -1);
  200.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  201.                     return new Point2D(1,0);
  202.                 }
  203.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  204.                 return new Point2D(1,0);
  205.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  206.                 return new Point2D(0,-1);
  207.             }
  208.         }
  209.  
  210.         if ("D".equals(direction)) {
  211.             return null;
  212.         }
  213.         return checkDownRight(currentX, currentY);
  214.     }
  215.  
  216.     /**
  217.      * Checks the lower right corner to consider its next position.
  218.      * @param currentX The enemie's current x coordinate.
  219.      * @param currentY The enemie's current y coordinate.
  220.      * @return The direction of the movement if it's executable, null otherwise.
  221.      */
  222.     private Point2D checkDownRight(int currentX, int currentY) {
  223.  
  224.         if(currentMapState[currentY + 1][currentX + 1].getClass() == Wall.class) {
  225.  
  226.             if(direction.equals("L")) {
  227.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  228.                     return new Point2D(0,1);
  229.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  230.                     return new Point2D(1,0);
  231.                 }
  232.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  233.                 return new Point2D(1,0);
  234.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  235.                 return new Point2D(0,1);
  236.             }
  237.         }
  238.  
  239.         if ("L".equals(direction)) {
  240.             return null;
  241.         }
  242.         return checkDownLeft(currentX, currentY);
  243.     }
  244.  
  245.     public String getDirection() {
  246.         return direction;
  247.     }
  248.  
  249.     public void setDirection(String direction) {
  250.         this.direction = direction;
  251.     }
  252.  
  253.     @Override
  254.     public Point2D getPosition() {
  255.         return position;
  256.     }
  257.  
  258.     @Override
  259.     public void setPosition(Point2D position) {
  260.         this.position = position;
  261.     }
  262.  
  263.     /**
  264.      * Updates the enemies position and direction.
  265.      * @param pos The direction to which the enemy is moving.
  266.      */
  267.     @Override
  268.     public void updatePosition(Point2D pos) {
  269.         String newDirection;
  270.         if(pos.getX() > 0) {
  271.             setDirection("R");
  272.         } else if(pos.getX() < 0) {
  273.             setDirection("L");
  274.         } else if(pos.getY() > 0) {
  275.             setDirection("D");
  276.         } else if(pos.getY() < 0) {
  277.             setDirection("U");
  278.         }
  279.         this.position = new Point2D(position.getX() + pos.getX(), position.getY() + pos.getY());
  280.     }
  281.  
  282.     @Override
  283.     public Image getImage() {
  284.         return im;
  285.     }
  286.  
  287.     @Override
  288.     public String toString() {
  289.         return "Enemy " + (int) position.getX() + " " + (int) position.getY() + " WF " + direction;
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement