Example #1
0
        private Node TreeDistribution(Rectangle area, Color[] colors, BodyRenderer renderer)
        {
            var hexagons = new Node();

            var radius = 2;
            var spacing = 4;
            var mapWidth = area.Width / spacing - 3;
            var mapHeight = area.Height / spacing - 1;
            var map = new HexagonMap<bool>(mapWidth, mapHeight);

            var first = new Hexagon(Rand.Floor(mapWidth), Rand.Floor(mapHeight));
            map[first] = true;
            hexagons.Add(BuildHexagon(renderer, colors, first, radius, spacing));

            var current = first;
            var n = 5 + Rand.Floor(10);
            for (int i = 0; i < n; i++)
            {
                var freeNeighbors = current.GetNeighborsWhere(hexagon => map.IsInBound(hexagon) && !map[hexagon]).ToArray();
                if (!freeNeighbors.Any())
                    break;

                current = freeNeighbors.RandomItem();
                map[current] = true;

                hexagons.Add(BuildHexagon(renderer, colors, current, radius, spacing));
            }

            return hexagons;
        }