using System; using System.Collections.Generic; using System.Text; namespace BattleForAllenCreek { class MapGrid { int originX; int originY; int topHeight; int bottomHeight; int hexsideWidth; int centerWidth; int hexagonWidth; int hexagonHeight; int halfHexagonWidth; int halfHexagonHeight; int oneFourthHexagonHeight; int leftMapEdge; //int hexsideEdge; // pixel info from screen int mapGridX, mapGridY; int distanceFromLeftEdgeOfHexagon; int distanceFromTopEdgeOfHexagon; int column, row; // pixel info adjusted for hexside edge slope //int adjustedPixelX, adjustedPixelY; // hexagon and it's hexpart Hexagon hexagon; Hexpart hexpart; // maxHexagon Hexagon maxHexagon; int maxHexpartX, maxHexpartY; public MapGrid(int originX, int originY , int topHeight, int bottomHeight , int hexsideWidth, int centerWidth , int rightEdgeHexagonNumber, int bottomEdgeHexagonNumber ) { // mapGrid properties this.originX = originX; this.originY = originY; this.topHeight = topHeight; this.bottomHeight = bottomHeight; this.hexsideWidth = hexsideWidth; this.centerWidth = centerWidth; this.hexagonHeight = this.topHeight + this.bottomHeight; this.hexagonWidth = this.hexsideWidth + this.centerWidth; this.halfHexagonWidth = this.hexagonWidth / 2; this.halfHexagonHeight = this.hexagonHeight / 2; this.oneFourthHexagonHeight = this.hexagonHeight / 4; this.leftMapEdge = -(this.hexsideWidth + (this.centerWidth / 2)); // use maxHexagon to find when mouse goes off the map into the map edge this.maxHexagon = new Hexagon(); this.maxHexagon.setNumber(rightEdgeHexagonNumber); this.maxHexpartX = this.maxHexagon.getX(); this.maxHexagon.setNumber(bottomEdgeHexagonNumber); this.maxHexpartY = this.maxHexagon.getY(); this.hexagon = new Hexagon(); this.hexpart = new Hexpart(); } public void setPixels(int pixelX, int pixelY) { this.calculateHexpartFromPixels(pixelX, pixelY); this.calculateHexagonFromPixels(); } public void setHexagonXY(int hexpartX, int hexpartY) { this.setHexpartXY(hexpartX, hexpartY); } public void setHexpartXY(int hexpartX, int hexpartY) { this.mapGridX = (this.halfHexagonWidth * hexpartX) - originX; this.mapGridY = (this.oneFourthHexagonHeight * hexpartY) - originY; this.hexpart.setXY(hexpartX, hexpartY); } private void calculateHexpartFromPixels(int pixelX, int pixelY) { int hexpartX, hexpartY; // adjust for hexagonGrid origin this.mapGridX = pixelX + originX; this.mapGridY = pixelY + originY; this.column = (this.mapGridX - this.leftMapEdge) / this.hexagonWidth; // distanceFromLeftEdgeOfHexagon used to calc how far right the point is in the left hexside // half way in uses the hexagon to the left // further in uses the hexagon to the right this.distanceFromLeftEdgeOfHexagon = (this.mapGridX - this.leftMapEdge) - (this.column * this.hexagonWidth); if (this.distanceFromLeftEdgeOfHexagon < this.hexsideWidth) { // it's a / or \ hexside hexpartX = (2 * this.column) - 1; this.row = this.mapGridY / this.halfHexagonHeight; hexpartY = (2 * this.row) + 1; // distanceFromTopEdgeOfHexagon used to calc how far down the point is into the bottom hexside // top half of the bottom hexside uses the hexagon above // bottom halv of the bottom hexside uses the hexagon below this.distanceFromTopEdgeOfHexagon = this.mapGridY - (this.row * this.topHeight); } else { // it's a center or lower hexside hexpartX = 2 * (this.column); this.mapGridY = this.mapGridY + this.oneFourthHexagonHeight; this.row = this.mapGridY / this.halfHexagonHeight; hexpartY = (2 * this.row); // distanceFromTopEdgeOfHexagon used to calc how far down the point is into the bottom hexside // top half of the bottom hexside uses the hexagon above // bottom halv of the bottom hexside uses the hexagon below this.distanceFromTopEdgeOfHexagon = this.mapGridY - (this.row * this.topHeight); } this.hexpart.setXY(hexpartX, hexpartY); } private void calculateHexagonFromPixels() { // if the pixels are near a hexside, need to calculate which side of the hexside // to correctly find which hexagon it is in int hexpartX, hexpartY, hexpartType; hexpartX = this.hexpart.getX(); hexpartY = this.hexpart.getY(); hexpartType = this.hexpart.getType(); switch (hexpartType) { case 1: this.hexagon.setXY(hexpartX, hexpartY); break; case 2: if (this.distanceFromTopEdgeOfHexagon < this.oneFourthHexagonHeight) { this.hexagon.setXY(hexpartX, hexpartY - 2); } else { this.hexagon.setXY(hexpartX, hexpartY + 2); } break; case 3: if (this.distanceFromLeftEdgeOfHexagon * this.topHeight < this.distanceFromTopEdgeOfHexagon * this.hexsideWidth) { this.hexagon.setXY(hexpartX - 1, hexpartY + 1); } else { this.hexagon.setXY(hexpartX + 1, hexpartY - 1); } break; case 4: if (this.distanceFromLeftEdgeOfHexagon * this.topHeight < this.distanceFromTopEdgeOfHexagon * this.hexsideWidth) { this.hexagon.setXY(hexpartX - 1, hexpartY - 1); } else { this.hexagon.setXY(hexpartX + 1, hexpartY + 1); } break; } } public Hexagon getHexagon() { return this.hexagon; } public Hexpart getHexpart() { return this.hexpart; } public int getPixelX() { return this.mapGridX; } public int getPixelY() { return this.mapGridY; } } }