Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public NetworkGameObject getFirstGameObjectFromLine(Line line) throws Exception {
- int start_x = (int) line.getStartX();
- int start_y = (int) line.getStartY();
- int end_x = (int) line.getEndX();
- int end_y = (int) line.getEndY();
- if (start_x != end_x && start_y != end_y) {
- throw new Exception("The line must be straight");
- }
- // Slightly messy logic, I should look into improving this
- boolean x_straight = start_x == end_x;
- NetworkGameObject object_found = null;
- if (x_straight) {
- // Line is going down or up
- boolean up = start_y > end_y;
- if (up) {
- for (int i = start_y; i != end_y; i--) {
- if (object_found != null) {
- break;
- }
- object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
- }
- } else {
- for (int i = start_y; i != end_y; i++) {
- if (object_found != null) {
- break;
- }
- object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
- }
- }
- } else {
- // Line is going left or right
- boolean left = start_x > end_x;
- if (left) {
- for (int i = start_x; i != end_x; i--) {
- if (object_found != null) {
- break;
- }
- object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
- }
- } else {
- for (int i = start_x; i != end_x; i++) {
- if (object_found != null) {
- break;
- }
- object_found = getGameObjectIfExistsAtCoordinates(start_x, i);
- }
- }
- }
- return object_found;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement