I'm making a simple tile-map system in LWJGL, and I'm trying to figure out how to detect if the mouse is clicking inside of a tile. So far no matter where I click it's always the same tile that is shown.
Code:
int blockWidth = 100;
int blockHeight = 100;
List<Tile> tiles = new ArrayList<Tile>();
private void addTiles(){
for(int i = 0; i < ((Display.getHeight() / blockHeight) * (Display.getWidth() / blockWidth)); i++)
tiles.add(new Tile(10,10,blockWidth,blockHeight, i));
}
//...
if(Mouse.isButtonDown(0)){
System.out.println("Mouse clicked at (" + Mouse.getX() + ", " + Mouse.getY() + ")");
for(Tile tile : tiles){
if(tile.inBounds(Mouse.getX(), Mouse.getY())){
tile.highlight();
System.out.println("Box clicked at (" + Mouse.getX() + ", " + Mouse.getY() + ").");
break;
}
}
}
the inBounds method:
public boolean inBounds(int x, int y){
Rectangle bounds = new Rectangle();
bounds.setBounds(x, y, width, height);
if(bounds.intersects(x,y,1,1)){
System.out.println("Tile " + num + " clicked. X/Y val is (" + this.x + "," + this.y + ")");
return true;
}else{
return false;
}
}