Beispiel #1
0
        protected void AddDoors(ArrayGrid <MapElement> map, ArrayGrid <int> valueMap, float doorProbability = 1f)
        {
            IEnumerator <Vector2Int> mapIter = IterateOverMap(map);

            while (mapIter.MoveNext())
            {
                Vector2Int current          = mapIter.Current;
                var        adjacentElements = map.GetAdjacentElements(current, true);

                int roomCells     = adjacentElements.Where(x => x == defaultRoomElement).Count();
                int corridorCells = adjacentElements.Where(x => x == defaultCorridorElement).Count();
                int doorCells     = adjacentElements.Where(x => x == defaultDoorElement).Count();

                if (map[current] == defaultCorridorElement)
                {
                    if ((corridorCells == 1 && doorCells == 0 && roomCells > 0 && roomCells < 4) ||
                        (corridorCells == 0 && doorCells == 0))
                    {
                        float exist = GameRNG.Randf();
                        if (exist < doorProbability)
                        {
                            map[current] = defaultDoorElement;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        bool GenerateMap(ArrayGrid <MapElement> map, ArrayGrid <int> valueMap)
        {
            List <Vector2Int> drillers = new List <Vector2Int>();

            drillers.Add(new Vector2Int(map.w / 2, map.h / 2));
            while (drillers.Count > 0)
            {
                int i = 0;
                while (i < drillers.Count)
                {
                    Vector2Int di = drillers[i];

                    bool removeDriller = false;
                    int  behavior      = GameRNG.Rand(4);
                    if (behavior == 0)
                    {
                        di.y -= 2;
                        if (di.y < 0 || map[di] == defaultCorridorElement)
                        {
                            removeDriller = (!allowLoops || (allowLoops && GameRNG.Randf() < loopProb));
                        }
                        if (!removeDriller)
                        {
                            map[di.x, di.y + 1] = defaultCorridorElement;
                        }
                    }
                    else if (behavior == 1)
                    {
                        di.y += 2;
                        if (di.y >= map.h || map[di] == defaultCorridorElement)
                        {
                            removeDriller = true;
                        }
                        else
                        {
                            map[di.x, di.y - 1] = defaultCorridorElement;
                        }
                    }
                    else if (behavior == 2)
                    {
                        di.x -= 2;
                        if (di.x < 0 || map[di] == defaultCorridorElement)
                        {
                            removeDriller = true;
                        }
                        else
                        {
                            map[di.x + 1, di.y] = defaultCorridorElement;
                        }
                    }
                    else if (behavior == 3)
                    {
                        di.x += 2;
                        if (di.x >= map.w || map[di] == defaultCorridorElement)
                        {
                            removeDriller = true;
                        }
                        else
                        {
                            map[di.x - 1, di.y] = defaultCorridorElement;
                        }
                    }

                    if (removeDriller)
                    {
                        drillers.RemoveAt(i);
                    }
                    else
                    {
                        drillers[i] = di;
                        drillers.Add(di);
                        drillers.Add(di);
                        map[di] = defaultCorridorElement;
                        ++i;
                    }
                }
            }

            return(true);
        }