Example #1
0
        private void GenerateTrees(ArrayGrid <MapElement> map)
        {
            var openGroundSpaces = map.GetPositionsOfType(defaultFillElement);
            int spacesToFill     = (int)(openGroundSpaces.Count * percentageOfGroundToFillWithTrees);
            //Debug.Log("trees = " + spacesToFill);
            int abort = 0;

            for (int i = 0; i < spacesToFill; ++i)
            {
                Vector2Int spotToFill = openGroundSpaces.GetRandomElementFromList();
                //Debug.Log("trying to put tree at " + spotToFill);
                var nearby = map.GetAdjacentElementsOfType(spotToFill, true, defaultBuildingWallElement);

                //TODO: fix this
                if (nearby.Count <= 0)
                {
                    //Debug.Log("Planting tree at " + spotToFill);
                    map[spotToFill] = defaultTreeElement;
                    openGroundSpaces.Remove(spotToFill);
                }
                else
                {
                    --i;
                    abort++;
                }

                if (abort >= 10000)
                {
                    break;
                }
            }
        }