Search code examples
javaslick2d

Detect mouseclick on object Slick2d


I've made this code that successfully creates a 16x12 grid by 50x50 squares on a 800x600px board. As you can see, the player moves to the coordinates of the players mouseclick.

Each square of the grid has an object of Felt (field) on it, which can be laast (locked). If a fields lock attribute is set to 1, the player should not be moved to that position.

How do i detect the field a player tries to move on to achieve this?

public class SimpleGame extends BasicGame{

    private Image plane;
    private float planeX;
    private float planeY;

    public SimpleGame()
    {
        super("SpilTest");
    }

    @Override
    public void init(GameContainer gc) throws SlickException {
        plane = new Image("figur.png");
    }

    @Override
    public void update(GameContainer gc, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
            this.planeX = input.getMouseX() - 30;
            this.planeY = input.getMouseY() - 50;
        }

    }

    public void render(GameContainer gc, Graphics g) throws SlickException {

        Felt board[][] = nytGrid();

        int distancex = 0;
        int distancey = 0;
        int counter = 0;

        for (int i=0; i < board.length ; i++) {
            for (int j=0; j < board[i].length ; j++) {                
                if (board[i][j].getLaast() == 1) {
                    g.setColor(Color.red);
                    g.fillRect(distancex, distancey, 50, 50);
                }

                distancex += 50;
                counter++;
                if (counter == 16) {
                    distancey += 50;
                    distancex = 0;
                    counter = 0;
                }
            }
        }

        g.drawImage(plane, planeX, planeY);

    }

    public static void main(String[] args) throws SlickException {

         AppGameContainer app = new AppGameContainer(new SimpleGame());

         app.setDisplayMode(800, 600, false);
         app.setTargetFrameRate(60);
         app.start();
    }

    public Felt[][] nytGrid() {

        Felt [][] board = new Felt[16][12];        

        for (int i=0; i < board.length ; i++) {
            for (int j=0; j < board[i].length ; j++) {
                int x = i;
                int y = j;
                board[i][j] = new Felt(x, y);

                if (i == 5 && j == 5) {
                    board[i][j].setLaast(1);
                }
            }
        }

        return board;
    }
}

Solution

  • First off, you should probably initialize the board in the init() method instead of render, so it doesn't have to do it every frame, and move the declaration for the grid next to the plane, planeX and planeY declarations in the class.

    Now to disable movement into a locked square, first add a method to check if a square at certain coordinates is locked, so something along the lines of:

    private boolean isLocked(int x, int y) {
        int square = board[x/50][y/50];
        if (square == 1) return true;
        else return false;
    }
    

    Next modify the part of your update() method where you update the plane coordinates, so vaguely something like:

    if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
            int destX = input.getMouseX() - 30;
            int destY = input.getMouseY() - 50;
            if (!isLocked(destX, destY)) {
                       this.planeX = destX;
                       this.planeY = destY;     
            }
    }