mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 04:38:00 +00:00
Support laying out and drawing tile map
This commit is contained in:
@@ -3,6 +3,7 @@ package forge.planarconquest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import forge.assets.FSkinProp;
|
||||
import forge.util.Aggregates;
|
||||
|
||||
public class ConquestPlaneMap {
|
||||
@@ -10,6 +11,7 @@ public class ConquestPlaneMap {
|
||||
private final int gridSize;
|
||||
private final HexagonTile[][] grid;
|
||||
private int tileCount;
|
||||
private float tileWidth, tileHeight;
|
||||
|
||||
public ConquestPlaneMap(ConquestPlane plane) {
|
||||
int size = (int)Math.round(Math.sqrt(plane.getCardPool().size() / 3)); //use card pool size to determine grid size
|
||||
@@ -21,6 +23,75 @@ public class ConquestPlaneMap {
|
||||
generateRandomGrid();
|
||||
}
|
||||
|
||||
public void setTileWidth(float tileWidth0) {
|
||||
if (tileWidth == tileWidth0) { return; }
|
||||
tileWidth = tileWidth0;
|
||||
tileHeight = tileWidth * (float)FSkinProp.IMG_HEXAGON_TILE.getHeight() / (float)FSkinProp.IMG_HEXAGON_TILE.getWidth();
|
||||
}
|
||||
|
||||
public float getWidth() {
|
||||
return tileWidth * 0.75f * (gridSize - 1) + tileWidth;
|
||||
}
|
||||
public float getHeight() {
|
||||
return tileHeight * gridSize + tileHeight / 2;
|
||||
}
|
||||
|
||||
public HexagonTile getTileAtPoint(float x, float y) {
|
||||
//convert pixel to axial coordinates
|
||||
x = (x - tileWidth / 2) / tileWidth;
|
||||
double t1 = y / (tileWidth / 2), t2 = Math.floor(x + t1);
|
||||
int r = (int)Math.floor((Math.floor(t1 - x) + t2) / 3);
|
||||
int q = (int)Math.floor((Math.floor(2 * x + 1) + t2) / 3) - r;
|
||||
|
||||
//convert axial to offset coordinates
|
||||
r += (q - (q & 1)) / 2;
|
||||
|
||||
if (r < 0 || q < 0 || q >= gridSize || r >= gridSize) {
|
||||
return null;
|
||||
}
|
||||
return grid[q][r];
|
||||
}
|
||||
|
||||
public void draw(IPlaneMapRenderer renderer, float scrollLeft, float scrollTop, float visibleWidth, float visibleHeight) {
|
||||
float dx = tileWidth * 0.75f;
|
||||
int startQ = getIndexInRange((int)Math.floor((scrollLeft - tileWidth) / dx) + 1);
|
||||
int startR = getIndexInRange((int)Math.floor((scrollTop - tileHeight * 1.5f) / tileHeight) + 1);
|
||||
int endQ = getIndexInRange((int)Math.floor((scrollLeft + visibleWidth - tileWidth) / dx) + 2);
|
||||
int endR = getIndexInRange((int)Math.floor((scrollTop + visibleHeight - tileHeight * 1.5f) / tileHeight) + 2);
|
||||
|
||||
float y;
|
||||
float x = startQ * dx - scrollLeft;
|
||||
float startY = startR * tileHeight - scrollTop;
|
||||
for (int q = startQ; q <= endQ; q++) {
|
||||
y = startY;
|
||||
if (q % 2 == 1) {
|
||||
y += tileHeight / 2; //odd columns start lower
|
||||
}
|
||||
for (int r = startR; r <= endR; r++) {
|
||||
HexagonTile tile = grid[q][r];
|
||||
if (tile != null) {
|
||||
renderer.draw(tile, x, y, tileWidth, tileHeight);
|
||||
}
|
||||
y += tileHeight;
|
||||
}
|
||||
x += dx;
|
||||
}
|
||||
}
|
||||
|
||||
private int getIndexInRange(int index) {
|
||||
if (index < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (index >= gridSize) {
|
||||
return gridSize - 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public static interface IPlaneMapRenderer {
|
||||
void draw(HexagonTile tile, float x, float y, float w, float h);
|
||||
}
|
||||
|
||||
private void generateRandomGrid() {
|
||||
int startQ = gridSize / 2; //player will start at center of grid always
|
||||
int startR = startQ;
|
||||
@@ -96,7 +167,7 @@ public class ConquestPlaneMap {
|
||||
}
|
||||
|
||||
private void addToList(int q, int r, List<HexagonTile> list) {
|
||||
if (r < 0 || q < 0 || q >= grid.length || r >= grid[0].length) {
|
||||
if (r < 0 || q < 0 || q >= gridSize || r >= gridSize) {
|
||||
return;
|
||||
}
|
||||
HexagonTile tile = grid[q][r];
|
||||
|
||||
Reference in New Issue
Block a user