/** * Handle the mouse being pressed. If the mouse is over a unit select it. Otherwise we move the * selected unit to the new target (assuming there was a path found) * * @param x The x coordinate of the mouse cursor on the screen * @param y The y coordinate of the mouse cursor on the screen */ private void handleMousePressed(int x, int y) { x -= 50; y -= 50; x /= 16; y /= 16; if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) { return; } if (map.getUnit(x, y) != 0) { selectedx = x; selectedy = y; lastFindX = -1; } else { if (selectedx != -1) { map.clearVisited(); path = finder.findPath( new UnitMover(map.getUnit(selectedx, selectedy)), selectedx, selectedy, x, y); if (path != null) { int unit = map.getUnit(selectedx, selectedy); map.setUnit(selectedx, selectedy, 0); map.setUnit(x, y, unit); selectedx = x; selectedy = y; lastFindX = -1; move = true; } } } repaint(0); }
/** * Handle the mouse being moved. In this case we want to find a path from the selected unit to the * position the mouse is at * * @param x The x coordinate of the mouse cursor on the screen * @param y The y coordinate of the mouse cursor on the screen */ private void handleMouseMoved(int x, int y) { x -= 50; y -= 50; x /= 16; y /= 16; if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) { return; } if (selectedx != -1) { if ((lastFindX != x) || (lastFindY != y)) { lastFindX = x; lastFindY = y; path = finder.findPath( new UnitMover(map.getUnit(selectedx, selectedy)), selectedx, selectedy, x, y); repaint(0); } } }