Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1.  public NetworkGameObject getFirstGameObjectFromLine(Line line) throws Exception {
  2.         int start_x = (int) line.getStartX();
  3.         int start_y = (int) line.getStartY();
  4.         int end_x = (int) line.getEndX();
  5.         int end_y = (int) line.getEndY();
  6.         if (start_x != end_x && start_y != end_y) {
  7.             throw new Exception("The line must be straight");
  8.         }
  9.  
  10.         // Slightly messy logic, I should look into improving this
  11.         boolean x_straight = start_x == end_x;
  12.         NetworkGameObject object_found = null;
  13.         if (x_straight) {
  14.             // Line is going down or up
  15.             boolean up = start_y > end_y;
  16.             if (up) {
  17.                 for (int i = start_y; i != end_y; i--) {
  18.                     if (object_found != null) {
  19.                         break;
  20.                     }
  21.                     object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
  22.                 }
  23.             } else {
  24.                 for (int i = start_y; i != end_y; i++) {
  25.                     if (object_found != null) {
  26.                         break;
  27.                     }
  28.  
  29.                     object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
  30.                 }
  31.             }
  32.         } else {
  33.             // Line is going left or right
  34.             boolean left = start_x > end_x;
  35.             if (left) {
  36.                 for (int i = start_x; i != end_x; i--) {
  37.                     if (object_found != null) {
  38.                         break;
  39.                     }
  40.                     object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
  41.                 }
  42.             } else {
  43.                 for (int i = start_x; i != end_x; i++) {
  44.                     if (object_found != null) {
  45.                         break;
  46.                     }
  47.                     object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
  48.                 }
  49.             }
  50.         }
  51.  
  52.         return object_found;
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement