0
\$\begingroup\$

I am a beginner at libGdx.

I am building a board game (the Fanorama game). I would like to use a pic as the board and place the pieces on the board on top of the board sprite. Is there a nice way to pre-determine what (x,y) co-ordinate locations on the board sprite are valid for placing a piece. Later on, when the game is being played I would like to move the pieces from location to location.

Right now, the only way I can think of is to calculate each piece location pixel co-ordinates and hard-code it in an array. I am not sure if there is a better way to do this and a best practice way for me to learn. I am also concerned if this will be affected when the user resized the screen or uses a different sized device (mobile).

How to solve this problem?

\$\endgroup\$
1
\$\begingroup\$

Assuming the board view is from directly above, for simple rectangular boards, you can do pretty well by interpolating from the four corners of the active playing area.

\$\endgroup\$
0
\$\begingroup\$

Why not have a 2 dimensional array, of the size of your grid, then draw your pieces depending on how many pixels you have as the dimensions of each grid.

For example:

public static final WORLD_WIDTH = 6;
public static final WORLD_HEIGHT = 6;

public static final GRID_WIDTH = 16;
public static final GRID_HEIGHT = 16;

GridPoint[][] grid = new GridPoint[WORLD_HEIGHT][WORLD_WIDTH]; //Grid Point is an object that would store information about each gridPoint. For example, it might store the piece that is on that gridPoint, whether a piece can be moved to things gridPoint etc.

instantiate grid objects:

for(int y = 0; y < grid.length; y++){
    for(int x = 0; x < grid[y].length; x++){
        grid[y][x] = new GridPoint(...parameters about the grid point, piece type maybe);
    }
}

in your render method:

for(int y = 0; y < grid.length; y++){
    for(int x = 0; x < grid[y].length; x++){
        grid[y][x].draw(batch, x * GRID_SIZE, y * GRID_SIZE);
    }
}

You might also want to consider making each GridPoint an actor/widget then you would be able to use libGDX's DragAndDrop function.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.