Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.33 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.                     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.     /**
  133.      * Checks the upper left corner to consider its next position.
  134.      * @param currentX The enemie's current x coordinate.
  135.      * @param currentY The enemie's current y coordinate.
  136.      * @return The direction of the movement if it's executable, null otherwise.
  137.      */
  138.     private Point2D checkUpLeft(int currentX, int currentY) {
  139.  
  140.         if(currentMapState[currentY - 1][currentX - 1].getClass() == Wall.class) {
  141.  
  142.             if(direction.equals("R")) {
  143.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  144.                     return new Point2D(0,-1);
  145.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  146.                     return new Point2D(-1,0);
  147.                 }
  148.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  149.                 return new Point2D(-1,0);
  150.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  151.                 return new Point2D(0,-1);
  152.             }
  153.         }
  154.  
  155.         if ("R".equals(direction)) {
  156.             return null;
  157.         }
  158.         return checkUpRight(currentX, currentY);
  159.     }
  160.  
  161.     /**
  162.      * Checks the lower left corner to consider its next position.
  163.      * @param currentX The enemie's current x coordinate.
  164.      * @param currentY The enemie's current y coordinate.
  165.      * @return The direction of the movement if it's executable, null otherwise.
  166.      */
  167.     private Point2D checkDownLeft(int currentX, int currentY) {
  168.  
  169.         if(currentMapState[currentY + 1][currentX - 1].getClass() == Wall.class) {
  170.  
  171.             if(direction.equals("R")) {
  172.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  173.                     return new Point2D(0,1);
  174.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  175.                     return new Point2D(-1,0);
  176.                 }
  177.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  178.                 return new Point2D(-1,0);
  179.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  180.                 return new Point2D(0,1);
  181.             }
  182.         }
  183.  
  184.         if ("U".equals(direction)) {
  185.             return null;
  186.         }
  187.         return checkUpLeft(currentX, currentY);
  188.     }
  189.  
  190.     /**
  191.      * Checks the upper right corner to consider its next position.
  192.      * @param currentX The enemie's current x coordinate.
  193.      * @param currentY The enemie's current y coordinate.
  194.      * @return The direction of the movement if it's executable, null otherwise.
  195.      */
  196.     private Point2D checkUpRight(int currentX, int currentY) {
  197.  
  198.         if(currentMapState[currentY - 1][currentX + 1].getClass() == Wall.class) {
  199.  
  200.             if(direction.equals("L")) {
  201.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  202.                     return new Point2D(0, -1);
  203.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  204.                     return new Point2D(1,0);
  205.                 }
  206.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  207.                 return new Point2D(1,0);
  208.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  209.                 return new Point2D(0,-1);
  210.             }
  211.         }
  212.  
  213.         if ("D".equals(direction)) {
  214.             return null;
  215.         }
  216.         return checkDownRight(currentX, currentY);
  217.     }
  218.  
  219.     /**
  220.      * Checks the lower right corner to consider its next position.
  221.      * @param currentX The enemie's current x coordinate.
  222.      * @param currentY The enemie's current y coordinate.
  223.      * @return The direction of the movement if it's executable, null otherwise.
  224.      */
  225.     private Point2D checkDownRight(int currentX, int currentY) {
  226.  
  227.         if(currentMapState[currentY + 1][currentX + 1].getClass() == Wall.class) {
  228.  
  229.             if(direction.equals("L")) {
  230.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  231.                     return new Point2D(0,1);
  232.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  233.                     return new Point2D(1,0);
  234.                 }
  235.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  236.                 return new Point2D(1,0);
  237.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  238.                 return new Point2D(0,1);
  239.             }
  240.         }
  241.  
  242.         if ("L".equals(direction)) {
  243.             return null;
  244.         }
  245.         return checkDownLeft(currentX, currentY);
  246.     }
  247.  
  248.     public String getDirection() {
  249.         return direction;
  250.     }
  251.  
  252.     public void setDirection(String direction) {
  253.         this.direction = direction;
  254.     }
  255.  
  256.     @Override
  257.     public Point2D getPosition() {
  258.         return position;
  259.     }
  260.  
  261.     @Override
  262.     public void setPosition(Point2D position) {
  263.         this.position = position;
  264.     }
  265.  
  266.     /**
  267.      * Updates the enemies position and direction.
  268.      * @param pos The direction to which the enemy is moving.
  269.      */
  270.     @Override
  271.     public void updatePosition(Point2D pos) {
  272.         String newDirection;
  273.         if(pos.getX() > 0) {
  274.             setDirection("R");
  275.         } else if(pos.getX() < 0) {
  276.             setDirection("L");
  277.         } else if(pos.getY() > 0) {
  278.             setDirection("D");
  279.         } else if(pos.getY() < 0) {
  280.             setDirection("U");
  281.         }
  282.         this.position = new Point2D(position.getX() + pos.getX(), position.getY() + pos.getY());
  283.     }
  284.  
  285.     @Override
  286.     public Image getImage() {
  287.         return im;
  288.     }
  289.  
  290.     @Override
  291.     public String toString() {
  292.         return "Enemy " + (int) position.getX() + " " + (int) position.getY() + " WF " + direction;
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement