public SquareGrid(int[,] map, float squareSize) { int nodeCountX = map.GetLength(0); int nodeCountY = map.GetLength(1); float mapWidth = nodeCountX * squareSize; float mapHeight = nodeCountY * squareSize; ControlNode[,] controlNodes = new ControlNode[nodeCountX, nodeCountY]; for (int x = 0; x < nodeCountX; x++) { for (int y = 0; y < nodeCountY; y++) { Vector3 pos = new Vector3(-mapWidth / 2 + x * squareSize + squareSize / 2, 0, -mapHeight / 2 + y * squareSize + squareSize / 2); controlNodes[x, y] = new ControlNode(pos, map[x, y] == 1, squareSize); } } squares = new Square[nodeCountX - 1, nodeCountY - 1]; for (int x = 0; x < nodeCountX - 1; x++) { for (int y = 0; y < nodeCountY - 1; y++) { squares[x, y] = new Square(controlNodes[x, y + 1], controlNodes[x + 1, y + 1], controlNodes[x + 1, y], controlNodes[x, y]); } } }
public Square(ControlNode topLeft, ControlNode topRight, ControlNode bottomRight, ControlNode bottomLeft) { _topLeft = topLeft; _topRight = topRight; _bottomRight = bottomRight; _bottomLeft = bottomLeft; _centerTop = _topLeft._right; _centerRight = _bottomRight._above; _centerBottom = _bottomLeft._right; _centerLeft = _bottomLeft._above; if (_topLeft._active) { _Configuration += 8; } if (_topRight._active) { _Configuration += 4; } if (_bottomRight._active) { _Configuration += 2; } if (_bottomLeft._active) { _Configuration += 1; } }