Coverage Summary for Class: WorldInputProcessor (io.github.unisim.world)

Class Class, % Method, % Branch, % Line, %
WorldInputProcessor 0% (0/1) 0% (0/10) 0% (0/76) 0% (0/111)


 
 package io.github.unisim.world;
 
 import com.badlogic.gdx.Input.Keys;
 import com.badlogic.gdx.Input;
 import com.badlogic.gdx.InputProcessor;
 import io.github.unisim.GameState;
 import io.github.unisim.Point;
 import io.github.unisim.building.Building;
 
 /**
  * Handles input events related to the world, after they have passed through the
  * UiInputProcessor.
  */
 public class WorldInputProcessor implements InputProcessor {
     private World world;
     private int[] cursorPos = new int[2];
     private int[] cursorPosWhenClicked = new int[2];
     private boolean clickedOnWorld = false;
     private boolean draggedSinceClick = true;
     private boolean moving = false;
     private Point location = null;
     private boolean flipped;
     private boolean moveBuild = false;
     private Point initialSize;
     private boolean setSize = false;
 
     public WorldInputProcessor(World world) {
         this.world = world;
     }
 
     @Override
     public boolean keyDown(int keycode) {
         if (keycode == Keys.RIGHT || keycode == Keys.D) {
             world.addDir(4, 0);
         }
         if (keycode == Keys.LEFT || keycode == Keys.A) {
             world.addDir(-4, 0);
         }
         if (keycode == Keys.UP || keycode == Keys.W) {
             world.addDir(0, 4);
         }
         if (keycode == Keys.DOWN || keycode == Keys.S) {
             world.addDir(0, -4);
         }
 
         switch (keycode) {
             case Keys.P:
                 GameState.paused = !GameState.paused;
                 break;
             case Keys.R:
                 // Flip the selected building
                 if (world.selectedBuilding != null) {
                     Point oldSize = null;
                     if (world.getPlaced() == true && setSize == false) {
                         oldSize = new Point(world.selectedBuilding.size.x, world.selectedBuilding.size.y);
                     }
                     world.selectedBuilding.flipped = !world.selectedBuilding.flipped;
                     int temp = world.selectedBuilding.size.x;
                     world.selectedBuilding.size.x = world.selectedBuilding.size.y;
                     world.selectedBuilding.size.y = temp;
                     world.selectedBuildingUpdated = true;
                     if (world.getPlaced() == true) {
                         world.setSize(oldSize);
                     }
                 }
                 break;
             default:
                 break;
         }
         return false;
     }
 
     @Override
     public boolean keyUp(int keycode) {
         if (keycode == Keys.RIGHT || keycode == Keys.D) {
             world.addDir(-4, 0);
         }
         if (keycode == Keys.LEFT || keycode == Keys.A) {
             world.addDir(4, 0);
         }
         if (keycode == Keys.UP || keycode == Keys.W) {
             world.addDir(0, -4);
         }
         if (keycode == Keys.DOWN || keycode == Keys.S) {
             world.addDir(0, 4);
         }
         return false;
     }
 
     @Override
     public boolean keyTyped(char character) {
         return false;
     }
 
     /**
      * Detect when the mouse has been clicked and record the cursor postion.
      * Sets the clickedOnWorld flag, if the mouse has been clicked in a valid
      * start location.
      */
     @Override
     public boolean touchDown(int x, int y, int pointer, int button) {
         Point gridPos = world.getCursorGridPos();
 
         if (button == Input.Buttons.LEFT) {
             Building clickedBuilding = world.getBuildingAt(gridPos);
             setSize = false;
 
             if (world.selectedBuilding != null) {
                 moving = false;
                 return true;
             }
             if (clickedBuilding != null && GameState.paused == false) {
                 moving = true;
                 // Select building from map and reset its state
                 world.selectedBuilding = clickedBuilding;
                 location = clickedBuilding.location;
                 flipped = clickedBuilding.flipped;
                 initialSize = clickedBuilding.size;
                 world.setLocation(location);
                 world.setFlipped(flipped);
                 world.setSize(initialSize);
                 moveBuild = true;
                 world.setPlaced(true);
                 world.buildingManager.removeBuilding(clickedBuilding);
                 world.selectedBuildingUpdated = true; // Notify world of update
             } else if (world.selectedBuilding != null) {
                 if (world.buildingManager.isBuildable(
                         world.btmLeft, world.topRight, world.getMapTiles())) {
                     // Set the building's location for preview
                     world.selectedBuilding.location = world.btmLeft;
 
                     // Update the preview in the BuildingManager
                     world.buildingManager.setPreviewBuilding(world.selectedBuilding);
 
                     // Mark the building as updated
                     world.selectedBuildingUpdated = true;
                 } else {
                     // Handle case where the position is not buildable
                     System.out.println("Cannot place the building here.");
                 }
             }
         }
 
         if (button == Input.Buttons.RIGHT) {
             // Deselect the building
             world.selectedBuilding = null;
             world.selectedBuildingUpdated = true;
             moveBuild = false;
             world.setPlaced(false);
         }
 
         if (world.selectedBuilding != null && button == Input.Buttons.RIGHT) {
             world.selectedBuilding = null;
             moveBuild = false;
             world.setPlaced(false);
         }
         clickedOnWorld = true;
         cursorPos[0] = cursorPosWhenClicked[0] = x;
         cursorPos[1] = cursorPosWhenClicked[1] = y;
 
         return true;
     }
 
     /**
      * When the mouse is released, stop tracking the dragging events.
      */
     @Override
     public boolean touchUp(int x, int y, int pointer, int button) {
         clickedOnWorld = false;
         if (moving == true) {
             draggedSinceClick = true;
         } else {
             draggedSinceClick = false;
         }
         if (!draggedSinceClick && world.selectedBuilding != null) {
             if (moveBuild == true) {
                 world.placeBuildingOld();
                 moveBuild = false;
                 world.setPlaced(false);
                 return false;
             }
             if (world.placeBuilding()) {
                 draggedSinceClick = true;
                 location = null;
             } else {
                 if (location != null) {
                     world.setBuildingNull(location, flipped, initialSize);
                 }
             }
         }
         return false;
     }
 
     /**
      * If the mouse has been clicked in a valid location, allow the map to be panned
      * by clicking and holding the mouse button.
      */
     @Override
     public boolean touchDragged(int x, int y, int pointer) {
         if (clickedOnWorld) {
             if (Math.max(Math.abs(cursorPos[0] - cursorPosWhenClicked[0]),
                     Math.abs(cursorPos[1] - cursorPosWhenClicked[1])) > 5) {
                 draggedSinceClick = true;
             }
             world.pan(cursorPos[0] - x, y - cursorPos[1]);
             cursorPos[0] = x;
             cursorPos[1] = y;
             return true;
         }
         return false;
     }
 
     @Override
     public boolean touchCancelled(int screenX, int screenY, int pointer, int button) {
         return false;
     }
 
     @Override
     public boolean mouseMoved(int x, int y) {
         return false;
     }
 
     /**
      * Zoom in on the map when the mouse wheel is scrolled.
      */
     @Override
     public boolean scrolled(float amountX, float amountY) {
         world.zoom(amountY);
         return true;
     }
 }