public NodeGrid Generate(NodeGrid nodeGrid) { // Calculate the number of walls to place int wallsLeft = (int)Math.Round(nodeGrid.CellCount * _percentFill); if (_fillTo) { wallsLeft -= (_add ? nodeGrid.Walls() : nodeGrid.Empty()).Count(); } wallsLeft = Mathf.Clamp(wallsLeft, 0, nodeGrid.CellCount); // Begin adding walls while (wallsLeft > 0) { // Grab a node, skip if already a wall Node node = nodeGrid.GetRandomNode(); if (_add) { if (!node.Walkable) { continue; } node.Walkable = false; } else { if (node.Walkable) { continue; } node.Walkable = true; } wallsLeft--; } return(nodeGrid); }