user1650305
user1650305

Reputation:

Filling a square with tiles

I have a 2D tile game that I am trying to create brush sizes for. Currently, my code looks like this:

if (isMouseClicked0) {
    int grid_x = Math.round(mouseX / blockSize);
    int grid_y = Math.round(mouseY / blockSize);
    for (int x = 0; x < brushSize; x++) {
        for (int y = 0; y < brushSize; y++) {
            world.setAt(grid_x, grid_y, b[inventorySelect]);
            grid_x += x;
            grid_y += y;
        }
    }
}

The setAt() method looks like this:

public void setAt(int x, int y, BlockType b) {
    if (x <= Display.getWidth() / blockSize && y <= Display.getHeight() / blockSize) {
        blocks[x][y] = new BaseBlock(b, x * blockSize, y * blockSize);
    }
    render();
}

This currently produces this output:

Tiles

The tile above first tile on the top left is where I clicked my mouse, so you can see that the next tile isn't rendering. I've been at this for hours, so I'm probably missing something simple. Any help?

EDIT: The brush size is 2, so I should be creating four tiles. blockSize is 32, it's how big my blocks are.

Upvotes: 1

Views: 173

Answers (1)

wldsvc
wldsvc

Reputation: 1272

The problem is with :

grid_x += x;
grid_y += y;

You are basically moving diagonally. This might work better :

    for (int x = 0; x < brushSize; x++) {
        for (int y = 0; y < brushSize; y++) {
            world.setAt(grid_x + x, grid_y + y, b[inventorySelect]);
        }
    }

Upvotes: 1

Related Questions