public static GridNode2D[,] BuildGrid(Rect bounds, float nodeSize, LayerMask layerMask, Dictionary<string, int> tagPenalties)
        {
            float xDistance = bounds.xMax - bounds.xMin;
            float yDistance = bounds.yMax - bounds.yMin;
            int gridX = (int) Mathf.Ceil(xDistance / nodeSize);
            int gridY = (int) Mathf.Ceil(yDistance / nodeSize);

            GridNode2D[,] returnVal = new GridNode2D[gridX, gridY];
            int nodeY = 0;

            for (float y = bounds.yMax; y > bounds.yMin; y -= nodeSize) {
                // trace ray from left to right
                float distanceSet = 0;
                int nodeX = 0;
                while (distanceSet <= xDistance) {
                    float distanceLeft = xDistance - distanceSet;
                    Vector2 origin = new Vector2(bounds.xMin + distanceSet, y);
                    RaycastHit2D rayCast = Physics2D.Raycast(origin, Vector2.right, distanceLeft, layerMask);

                    // If it hits something then mark each entry up to that point as passable
                    float hitPoint = Mathf.Floor(rayCast.distance / nodeSize) * nodeSize;
                    if (rayCast.collider == null) {
                        hitPoint = distanceLeft;
                    }

                    Debug.DrawRay(origin, Vector2.right * hitPoint);

                    for (float i = 0; i < hitPoint; i += nodeSize) {
                        returnVal[nodeX, nodeY] = new GridNode2D(new Point2D(origin.x + i, y)) {
                            Passable = true,
                        };
                        nodeX++;
                    }

                    // Mark the point it hit as impassable
                    distanceSet += (hitPoint + nodeSize);
                    if (distanceSet < xDistance && rayCast.collider != null) {
                        nodeX++;
                        bool passable = false;
                        int penalty = 0;

                        if (tagPenalties.ContainsKey(rayCast.collider.tag)) {
                            penalty = tagPenalties[rayCast.collider.tag];
                            passable = true;
                        }

                        returnVal[nodeX, nodeY] = new GridNode2D(new Point2D(bounds.xMin + distanceSet, y)) {
                            Passable = passable,
                            Penalty = penalty
                        };
                    }
                    // Repeat until there is no distance to go
                }

                nodeY++;
            }

            return returnVal;
        }
Example #2
0
        private GridNode2D[,] buildGridFromChars(char[,] gridDef)
        {
            // Build a grid:
            //   * = empty
            //   x = impassable
            //   1 - 9 = penalty
            GridNode2D[,] grid = new GridNode2D[gridDef.GetLength(1), gridDef.GetLength(0)];
            for (int x = 0; x < gridDef.GetLength(1); x++) {
                for (int y = 0; y < gridDef.GetLength(0); y++) {
                    grid[x, y] = new GridNode2D(new Point2D(x, y));
                    switch (gridDef[y, x]) {
                        case 'x':
                            grid[x, y].Passable = false;
                            break;
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            grid[x, y].Penalty = int.Parse(gridDef[y, x].ToString());
                            grid[x, y].Passable = true;
                            break;
                        default:
                            grid[x, y].Passable = true;
                            break;
                    }
                }
            }

            return grid;
        }