So my issue is, usually when I generate my maps I’d use the following:
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
tiles.add(new Tile(x, y));
}
}
You can get the just of that.
Anyway I need to pretty much have pragmatic way of doing this in an isometric / 45 degree rotation.
Say the first loop you’d add new Tile(width / 2, 0).
Next loop you’d add new Tile((width / 2) – 1, 0), and new Tile((width / 2) + 1, 0).
I’ve experimented with a few differing ways to do this, but haven’t had much success.
If I understand correctly, it seems like you’re trying to represent the tiles for an isometric game, e.g.:
For this, your
Tiles store the (x,y) information of their location with the following layout:and so on.
However, I would suggest to decouple the internal representation of your tile grid from the way it is displayed. Just because you want isometric graphics doesn’t necessarily mean that you have to store your
Tiles’ (x,y)-positions in a diamond shape as well.Instead, try the following layout for each of your tiles:
That is, tile
Ais created asnew Tile(0,0), tileBasnew Tile(0,1), etc.With this representation you’ll get the advantage that adjacent tiles in your isometric display will only differ by
1in either the x- or the y-direction. This should make the initialization step of yourtileslist much easier.To map these coordinates to the isometric display positions, remember that tile
Aat location(0,0)is at the top of the diamond. You can then compute the display positions of all other tiles relative to that one:So let’s assume your display renders
Aat the pixel coordinates(px, py), and every tile is rendered with a width ofpWidthand a height ofpHeight.The horizontal offset for
tilecan then be computed by walking righttile.xtimes and walking lefttile.ytimes. Because of the isometric view, you only offset by half of the width in every step.The vertical offset is computed similarly. While
tile.xandtile.ycancel each other out in the horizontal directorion (since left and right are opposite of each other), they both contribute to moving down in the vertical direction.Unless your game does scrolling/zooming, you can compute the pixel coordinates of every
Tileright in the constructor, because they only depend on the values of (x,y) and the constantswidth,px,py,pWidth, andpHeight.