I am making a turn-based tactical game (think something like X-Com) and I am currently trying to figure out how to handle mouse input in a clean way without a mess of if-statements.

I have a lot of variables that come into play and that changes what happens when you click a tile in the game.

For example: if the currently selected unit is a player controlled unit and the tile you click is within that units movement range, I want to queue up a move action. If the clicked tile contains another player character I want to select that one instead, but if it contains an enemy I want to select the enemy or attack it if the player is adjacent.

So basically what happens when you click depend on selectedUnit, clickedTile, clickedTile.getUnit().getType(), and if the selected units tile is a neighbour to the clicked tile.

Is there a cleaner way to handle this than a mess of if-statments? I am using Java and Libgdx if that matters, but I am guessing this is mostly a design question.


1 Answer 1


Since you are trying to avoid if statements, have you considered implementing the InputProcessor class?

public boolean keyDown(int keycode) {
    if (keycode == Input.Keys.BACK || keycode == Input.Keys.ESCAPE) {
        // Go back code
    } // other keys
    return false;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .