public Square(ControlNode TL, ControlNode TR, ControlNode BR, ControlNode BL) { this.TL = TL; this.TR = TR; this.BL = BL; this.BR = BR; this.L = BL.above; this.T = TL.right; this.R = BR.above; this.B = BL.right; if (TL.isActive) { configuration += 8; } if (TR.isActive) { configuration += 4; } if (BL.isActive) { configuration += 1; } if (BR.isActive) { configuration += 2; } }
public SquareGrid(float[,] map, float squareSize) { //Init int nodeCountX = map.GetLength(0); int nodeCountZ = map.GetLength(1); float halfMapWidthX = nodeCountX * squareSize * 0.5f; float halfMapWidthZ = nodeCountZ * squareSize * 0.5f; float halfSquareSize = squareSize * 0.5f; //Step 1. Create the control nodes which have a position and a state if they are active or not //These can be reused between squares, so better to create them once from a memory perspective ControlNode[,] controlNodes = new ControlNode[nodeCountX, nodeCountZ]; for (int x = 0; x < nodeCountX; x++) { for (int z = 0; z < nodeCountZ; z++) { //Center the map around (0,0) float xPos = -halfMapWidthX + x * squareSize + halfSquareSize; float zPos = -halfMapWidthZ + z * squareSize + halfSquareSize; MyVector2 pos = new MyVector2(xPos, zPos); bool isActive = map[x, z] >= 1f - Mathf.Epsilon; controlNodes[x, z] = new ControlNode(pos, isActive, squareSize, map[x, z]); } } //Step 2. Create the squares which consists of 4 control nodes, so there will be 1 less square than nodes squares = new Square[nodeCountX - 1, nodeCountZ - 1]; for (int x = 0; x < nodeCountX - 1; x++) { for (int z = 0; z < nodeCountZ - 1; z++) { //The control nodes were created from BL ControlNode BL = controlNodes[x + 0, z + 0]; ControlNode BR = controlNodes[x + 1, z + 0]; ControlNode TR = controlNodes[x + 1, z + 1]; ControlNode TL = controlNodes[x + 0, z + 1]; squares[x, z] = new Square(TL, TR, BR, BL); } } }