Support laying out and drawing tile map

This commit is contained in:
drdev
2014-12-16 05:20:15 +00:00
parent 02040ee026
commit 8c82551adc
2 changed files with 99 additions and 23 deletions

View File

@@ -32,6 +32,9 @@ import forge.planarconquest.ConquestPlane;
import forge.planarconquest.ConquestPlane.Region; import forge.planarconquest.ConquestPlane.Region;
import forge.planarconquest.ConquestPlaneData; import forge.planarconquest.ConquestPlaneData;
import forge.planarconquest.ConquestPlaneData.RegionData; import forge.planarconquest.ConquestPlaneData.RegionData;
import forge.planarconquest.ConquestPlaneMap;
import forge.planarconquest.ConquestPlaneMap.HexagonTile;
import forge.planarconquest.ConquestPlaneMap.IPlaneMapRenderer;
import forge.planarconquest.ConquestPreferences.CQPref; import forge.planarconquest.ConquestPreferences.CQPref;
import forge.planarconquest.IVCommandCenter; import forge.planarconquest.IVCommandCenter;
import forge.screens.FScreen; import forge.screens.FScreen;
@@ -43,7 +46,6 @@ import forge.toolbox.FDisplayObject;
import forge.toolbox.FEvent; import forge.toolbox.FEvent;
import forge.toolbox.FScrollPane; import forge.toolbox.FScrollPane;
import forge.toolbox.FEvent.FEventHandler; import forge.toolbox.FEvent.FEventHandler;
import forge.toolbox.FScrollPane.ScrollBounds;
import forge.toolbox.FLabel; import forge.toolbox.FLabel;
import forge.util.Utils; import forge.util.Utils;
@@ -85,6 +87,7 @@ public class CommandCenterScreen extends FScreen implements IVCommandCenter {
public void update() { public void update() {
model = FModel.getConquest().getModel(); model = FModel.getConquest().getModel();
setHeaderCaption(model.getName()); setHeaderCaption(model.getName());
map.revalidate();
updateCurrentDay(); updateCurrentDay();
} }
@@ -173,44 +176,46 @@ public class CommandCenterScreen extends FScreen implements IVCommandCenter {
btnEndDay.setSize(width / 2, btnEndDay.getAutoSizeBounds().height); btnEndDay.setSize(width / 2, btnEndDay.getAutoSizeBounds().height);
btnEndDay.setPosition((width - btnEndDay.getWidth()) / 2, height - btnEndDay.getHeight()); btnEndDay.setPosition((width - btnEndDay.getWidth()) / 2, height - btnEndDay.getHeight());
//btnEditDeck.setBounds(0, height - VPrompt.HEIGHT, VPrompt.BTN_WIDTH, VPrompt.HEIGHT); //float numCommanders = commanderRow.panels.length;
//btnPortal.setBounds(width - VPrompt.BTN_WIDTH, height - VPrompt.HEIGHT, VPrompt.BTN_WIDTH, VPrompt.HEIGHT); //float commanderWidth = (width - (numCommanders + 3) * COMMANDER_GAP) / numCommanders;
//float commanderRowHeight = commanderWidth * FCardPanel.ASPECT_RATIO + 2 * COMMANDER_ROW_PADDING;
//commanderRow.setBounds(0, btnEndDay.getTop() - PLANE_NAME_FONT.getLineHeight() - 2 * FLabel.DEFAULT_INSETS - commanderRowHeight, width, commanderRowHeight);
float numCommanders = commanderRow.panels.length; map.setBounds(0, startY, width, btnEndDay.getTop() - startY);
float commanderWidth = (width - (numCommanders + 3) * COMMANDER_GAP) / numCommanders;
float commanderRowHeight = commanderWidth * FCardPanel.ASPECT_RATIO + 2 * COMMANDER_ROW_PADDING;
commanderRow.setBounds(0, btnEndDay.getTop() - PLANE_NAME_FONT.getLineHeight() - 2 * FLabel.DEFAULT_INSETS - commanderRowHeight, width, commanderRowHeight);
map.setBounds(0, startY, width, commanderRow.getTop() - startY);
} }
private class PlaneMap extends FScrollPane { private class PlaneMap extends FScrollPane implements IPlaneMapRenderer {
private float zoom = 1; private float zoom = 1;
private FImage walker = (FImage)PlaneswalkerAchievements.getTrophyImage("Ajani Goldmane"); private FImage walker = (FImage)PlaneswalkerAchievements.getTrophyImage("Ajani Goldmane");
private Graphics g;
private ConquestPlaneMap mapData;
public PlaneMap() { public PlaneMap() {
} }
@Override @Override
protected ScrollBounds layoutAndGetScrollBounds(float visibleWidth, float visibleHeight) { protected ScrollBounds layoutAndGetScrollBounds(float visibleWidth, float visibleHeight) {
return new ScrollBounds(visibleWidth, visibleHeight); if (model == null) {
mapData = null;
return new ScrollBounds(visibleWidth, visibleHeight);
}
mapData = model.getCurrentPlaneData().getMap();
mapData.setTileWidth(visibleWidth / 4 * zoom);
return new ScrollBounds(mapData.getWidth(), mapData.getHeight());
} }
protected void drawBackground(Graphics g) { protected void drawBackground(Graphics g0) {
float w = getWidth(); if (mapData == null) { return; }
float h = getHeight(); g = g0;
float tileWidth = w / 4 * zoom; mapData.draw(this, getScrollLeft(), getScrollTop(), getWidth(), getHeight());
float tileHeight = tileWidth * FSkinImage.HEXAGON_TILE.getHeight() / FSkinImage.HEXAGON_TILE.getWidth(); g = null;
float x = (w - tileWidth) / 2;
float y = (h - tileHeight) / 2;
drawTile(g, x, y, tileWidth, tileHeight);
} }
private void drawTile(Graphics g, float x, float y, float w, float h) { @Override
public void draw(HexagonTile tile, float x, float y, float w, float h) {
g.drawImage(FSkinImage.HEXAGON_TILE, x, y, w, h); g.drawImage(FSkinImage.HEXAGON_TILE, x, y, w, h);
float pedestalWidth = w * 0.8f; float pedestalWidth = w * 0.2f;//0.8f;
float pedestalHeight = pedestalWidth * walker.getHeight() / walker.getWidth(); float pedestalHeight = pedestalWidth * walker.getHeight() / walker.getWidth();
g.drawImage(walker, x + (w - pedestalWidth) / 2, y + h / 2 - pedestalHeight * 0.8f, pedestalWidth, pedestalHeight); g.drawImage(walker, x + (w - pedestalWidth) / 2, y + h / 2 - pedestalHeight * 0.8f, pedestalWidth, pedestalHeight);
} }

View File

@@ -3,6 +3,7 @@ package forge.planarconquest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import forge.assets.FSkinProp;
import forge.util.Aggregates; import forge.util.Aggregates;
public class ConquestPlaneMap { public class ConquestPlaneMap {
@@ -10,6 +11,7 @@ public class ConquestPlaneMap {
private final int gridSize; private final int gridSize;
private final HexagonTile[][] grid; private final HexagonTile[][] grid;
private int tileCount; private int tileCount;
private float tileWidth, tileHeight;
public ConquestPlaneMap(ConquestPlane plane) { public ConquestPlaneMap(ConquestPlane plane) {
int size = (int)Math.round(Math.sqrt(plane.getCardPool().size() / 3)); //use card pool size to determine grid size 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(); 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() { private void generateRandomGrid() {
int startQ = gridSize / 2; //player will start at center of grid always int startQ = gridSize / 2; //player will start at center of grid always
int startR = startQ; int startR = startQ;
@@ -96,7 +167,7 @@ public class ConquestPlaneMap {
} }
private void addToList(int q, int r, List<HexagonTile> list) { 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; return;
} }
HexagonTile tile = grid[q][r]; HexagonTile tile = grid[q][r];