Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.76 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.     private boolean hasToRedirect = false; //if the enemy has to change his direction.
  19.     private boolean hasRedirected = true;
  20.     private int numberOfChecks; //stores the number of checks we perform on his diagonal cells.
  21.  
  22.     /**
  23.      * Construct a new WallFollowingEnemy().
  24.      * @param point2D value to set a default point2D object.
  25.      */
  26.     public WallFollowingEnemy (Point2D point2D, String direction) {
  27.         this.position = point2D;
  28.         this.direction = direction;
  29.  
  30.         try{
  31.             System.out.println("loaded i guess");
  32.  
  33.             im = new Image(new FileInputStream("assets/images/slEnemy.png"));
  34.         } catch(Exception e) {
  35.             System.out.println("Could not load straight line enemy image!");
  36.         }
  37.     }
  38.  
  39.  
  40.     /**
  41.      * Method to calculate the enemies next move relative to the board state.
  42.      *
  43.      * @return enemies new position on the board.
  44.      */
  45.     @Override
  46.     public Point2D getNextMove() {
  47.  
  48.         int currentX = (int) position.getX();
  49.         int currentY = (int) position.getY();
  50.         Point2D nextMove = null;
  51.         numberOfChecks = 0;
  52.         System.out.println("has to redirect: " + hasToRedirect);
  53.  
  54.         switch(direction) {
  55.             case "U":
  56.                 if(currentMapState[currentY - 1][currentX].getClass() != Ground.class) {
  57.                     hasToRedirect = true;
  58.                     hasRedirected = false;
  59.                     if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  60.                         nextMove = new Point2D(-1,0);
  61.                     } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  62.                         nextMove = new Point2D(1,0);
  63.                     }
  64.                 }
  65.  
  66.                 if(nextMove == null) {
  67.  
  68.                     nextMove = checkUpLeft(currentX, currentY);
  69.                 }
  70.  
  71.             break;
  72.  
  73.             case "D":
  74.                 if(currentMapState[currentY + 1][currentX].getClass() != Ground.class) {
  75.                     hasToRedirect = true;
  76.                     hasRedirected = false;
  77.                     if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  78.                         nextMove = new Point2D(+1,0);
  79.                     } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  80.                         nextMove = new Point2D(-1,0);
  81.                     }
  82.                 }
  83.  
  84.                 if(nextMove == null) {
  85.                     nextMove = checkDownRight(currentX, currentY);
  86.                 }
  87.             break;
  88.  
  89.  
  90.             case "R":
  91.                 if(currentMapState[currentY][currentX + 1].getClass() != Ground.class) {
  92.                     hasToRedirect = true;
  93.                     hasRedirected = false;
  94.                     if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  95.                         nextMove = new Point2D(0,1);
  96.                     } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  97.                         nextMove = new Point2D(0,-1);
  98.                     }
  99.                 }
  100.  
  101.                 if(nextMove == null) {
  102.                     nextMove = checkUpRight(currentX, currentY);
  103.                 }
  104.  
  105.             break;
  106.  
  107.             case "L":
  108.                 if(currentMapState[currentY][currentX - 1].getClass() != Ground.class) {
  109.                     hasToRedirect = true;
  110.                     hasRedirected = false;
  111.                     if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  112.                         nextMove = new Point2D(0,-1);
  113.                     } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  114.                         nextMove = new Point2D(0,1);
  115.                     }
  116.                 }
  117.  
  118.                 if(nextMove == null) {
  119.                     nextMove = checkDownLeft(currentX, currentY);
  120.                 }
  121.             break;
  122.         }
  123.  
  124.         System.out.println(numberOfChecks);
  125.  
  126.         if(nextMove != null) {
  127.             return nextMove;
  128.         } else {
  129.  
  130.  
  131.  
  132.             switch(direction) {
  133.                 case "U": return new Point2D(0, -1);
  134.  
  135.                 case "D": return new Point2D(0, 1);
  136.  
  137.                 case "R": return new Point2D(1, 0);
  138.  
  139.                 case "L": return new Point2D(-1, 0);
  140.  
  141.             }
  142.             return new Point2D(0,0);
  143.         }
  144.     }
  145.  
  146.     /**
  147.      * Checks the upper left corner to consider its next position.
  148.      * @param currentX The enemie's current x coordinate.
  149.      * @param currentY The enemie's current y coordinate.
  150.      * @return The direction of the movement if it's executable, null otherwise.
  151.      */
  152.     private Point2D checkUpLeft(int currentX, int currentY) {
  153.  
  154.         if(currentMapState[currentY - 1][currentX - 1].getClass() != Ground.class) {
  155.  
  156.             if(direction.equals("R")) {
  157.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  158.                     return new Point2D(0,-1);
  159.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  160.                     return new Point2D(-1,0);
  161.                 }
  162.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  163.                 return new Point2D(-1,0);
  164.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  165.                 return new Point2D(0,-1);
  166.             }
  167.         }
  168.  
  169.         numberOfChecks++;
  170.         if(numberOfChecks == 2 && !hasToRedirect) {
  171.             hasToRedirect = true;
  172.             hasRedirected = false;
  173.             return null;
  174.         }
  175.  
  176.         if ("R".equals(direction)) {
  177.             return null;
  178.         }
  179.         return checkUpRight(currentX, currentY);
  180.     }
  181.  
  182.     /**
  183.      * Checks the lower left corner to consider its next position.
  184.      * @param currentX The enemie's current x coordinate.
  185.      * @param currentY The enemie's current y coordinate.
  186.      * @return The direction of the movement if it's executable, null otherwise.
  187.      */
  188.     private Point2D checkDownLeft(int currentX, int currentY) {
  189.  
  190.         if(currentMapState[currentY + 1][currentX - 1].getClass() != Ground.class) {
  191.  
  192.             if(direction.equals("R")) {
  193.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  194.                     return new Point2D(0,1);
  195.                 } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  196.                     return new Point2D(-1,0);
  197.                 }
  198.             } else if(currentMapState[currentY][currentX - 1].getClass() == Ground.class) {
  199.                 return new Point2D(-1,0);
  200.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  201.                 return new Point2D(0,1);
  202.             }
  203.         }
  204.  
  205.         numberOfChecks++;
  206.         if(numberOfChecks == 2 && !hasToRedirect) {
  207.             hasToRedirect = true;
  208.             hasRedirected = false;
  209.             return null;
  210.         }
  211.  
  212.         if ("U".equals(direction)) {
  213.             return null;
  214.         }
  215.         return checkUpLeft(currentX, currentY);
  216.     }
  217.  
  218.     /**
  219.      * Checks the upper right corner to consider its next position.
  220.      * @param currentX The enemie's current x coordinate.
  221.      * @param currentY The enemie's current y coordinate.
  222.      * @return The direction of the movement if it's executable, null otherwise.
  223.      */
  224.     private Point2D checkUpRight(int currentX, int currentY) {
  225.  
  226.         if(currentMapState[currentY - 1][currentX + 1].getClass() != Ground.class) {
  227.  
  228.             if(direction.equals("L")) {
  229.                 if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  230.                     return new Point2D(0, -1);
  231.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  232.                     return new Point2D(1,0);
  233.                 }
  234.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  235.                 return new Point2D(1,0);
  236.             } else if(currentMapState[currentY - 1][currentX].getClass() == Ground.class) {
  237.                 return new Point2D(0,-1);
  238.             }
  239.         }
  240.  
  241.         numberOfChecks++;
  242.         if(numberOfChecks == 2 && !hasToRedirect) {
  243.             hasToRedirect = true;
  244.             hasRedirected = false;
  245.             return null;
  246.         }
  247.  
  248.         if ("D".equals(direction)) {
  249.             return null;
  250.         }
  251.         return checkDownRight(currentX, currentY);
  252.     }
  253.  
  254.     /**
  255.      * Checks the lower right corner to consider its next position.
  256.      * @param currentX The enemie's current x coordinate.
  257.      * @param currentY The enemie's current y coordinate.
  258.      * @return The direction of the movement if it's executable, null otherwise.
  259.      */
  260.     private Point2D checkDownRight(int currentX, int currentY) {
  261.  
  262.         if(currentMapState[currentY + 1][currentX + 1].getClass() != Ground.class) {
  263.  
  264.             if(direction.equals("L")) {
  265.                 if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  266.                     return new Point2D(0,1);
  267.                 } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  268.                     return new Point2D(1,0);
  269.                 }
  270.             } else if(currentMapState[currentY][currentX + 1].getClass() == Ground.class) {
  271.                 return new Point2D(1,0);
  272.             } else if(currentMapState[currentY + 1][currentX].getClass() == Ground.class) {
  273.                 return new Point2D(0,1);
  274.             }
  275.         }
  276.  
  277.         numberOfChecks++;
  278.         if(numberOfChecks == 2 && !hasToRedirect) {
  279.             hasToRedirect = true;
  280.             hasRedirected = false;
  281.             return null;
  282.         }
  283.  
  284.         if ("L".equals(direction)) {
  285.             return null;
  286.         }
  287.         return checkDownLeft(currentX, currentY);
  288.     }
  289.  
  290.     public String getDirection() {
  291.         return direction;
  292.     }
  293.  
  294.     public void setDirection(String direction) {
  295.         this.direction = direction;
  296.     }
  297.  
  298.     @Override
  299.     public Point2D getPosition() {
  300.         return position;
  301.     }
  302.  
  303.     @Override
  304.     public void setPosition(Point2D position) {
  305.         this.position = position;
  306.     }
  307.  
  308.     /**
  309.      * Updates the enemies position and direction.
  310.      * @param pos The direction to which the enemy is moving.
  311.      */
  312.     @Override
  313.     public void updatePosition(Point2D pos) {
  314.         String newDirection;
  315.  
  316.         if(pos.getX() > 0) {
  317.             setDirection("R");
  318.         } else if(pos.getX() < 0) {
  319.             setDirection("L");
  320.         } else if(pos.getY() > 0) {
  321.             setDirection("D");
  322.         } else if(pos.getY() < 0) {
  323.             setDirection("U");
  324.         }
  325.  
  326.  
  327.  
  328.         if(!hasRedirected) {
  329.             hasRedirected = true;
  330.         } else {
  331.             hasRedirected = false;
  332.             hasToRedirect = false;
  333.         }
  334.  
  335.  
  336.         this.position = new Point2D(position.getX() + pos.getX(), position.getY() + pos.getY());
  337.     }
  338.  
  339.     @Override
  340.     public Image getImage() {
  341.         return im;
  342.     }
  343.  
  344.     @Override
  345.     public String toString() {
  346.         return "Enemy " + (int) position.getX() + " " + (int) position.getY() + " WF " + direction;
  347.     }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement