Ejemplo n.º 1
0
        private void ConnectRoomsToStart()
        {
            Queue <MapRoom> toConnect        = new Queue <MapRoom>();
            List <MapRoom>  connectedToStart = new List <MapRoom>();
            MapRoom         startRoom        = null;

            List <MapRegion> checks;

            foreach (MapRoom room in rooms)
            {
                if (room.region.isStartRegion)
                {
                    startRoom = room;
                    startRoom.SetAccessibleFromStartRoom();

                    connectedToStart.Add(startRoom);

                    Queue <MapRegion> neighbours = new Queue <MapRegion>();
                    foreach (MapRegion reg in mapHolder.GetNeighbourRegions(startRoom.region))
                    {
                        if (!reg.empty)
                        {
                            neighbours.Enqueue(reg);
                        }
                    }

                    checks = new List <MapRegion>();

                    // vzit vsechny sousedy startovni mistnosti
                    while (neighbours.Count > 0)
                    {
                        MapRegion reg = neighbours.Dequeue();

                        checks.Add(reg);

                        // jen ty ktere MAJI BYT pripojene
                        if (reg == null || !reg.isAccessibleFromStart)
                        {
                            continue;
                        }

                        // soused startovniho regionu je jeho potomek - jsou spojeny - pridat do seznamu connectedToStart
                        if (reg.IsInFamily(startRoom.region))
                        {
                            foreach (MapRoom r in GetRoomsInRegion(reg))
                            {
                                connectedToStart.Add(r);
                            }

                            // pridat vsechny sousedy child regionu
                            foreach (MapRegion r in mapHolder.GetNeighbourRegions(reg))
                            {
                                if (r != null && r.isAccessibleFromStart && !neighbours.Contains(r) && !checks.Contains(r))
                                {
                                    neighbours.Enqueue(r);
                                }
                            }

                            continue;
                        }

                        // pridat do seznamu k pripojeni
                        foreach (MapRoom r in GetRoomsInRegion(reg))
                        {
                            if (!r.isAccessibleFromStartRoom)
                            {
                                toConnect.Enqueue(r);
                            }
                        }
                    }
                }
            }

            while (toConnect.Count > 0)
            {
                // 1. vzit kazdou mistnost k pripojeni a zkusit ji pripojit k jedne z mistnosti v "connected"

                MapRoom toConnectRoom = toConnect.Dequeue();

                //Debug.Log("** connecting " + toConnectRoom.region.x + ", " + toConnectRoom.region.y);

                int max = connectedToStart.Count;
                for (int i = 0; i < max; i++)
                {
                    MapRoom connectedRoom = connectedToStart[i];

                    // mistnosti jsou sousedi
                    if (CanBeConnected(toConnectRoom, connectedRoom) && !AreRoomsConnected(toConnectRoom, connectedRoom))
                    {
                        if (connectedRoom.isAccessibleFromStartRoom && connectedRoom.region.onlyOnePassage && connectedRoom.connectedRooms.Count > 0)
                        {
                            continue;
                        }

                        //Debug.Log("propojuji " + toConnectRoom.region.x + ", " + toConnectRoom.region.y + " s " + connectedRoom.region.x + ", " + connectedRoom.region.y);

                        ConnectRooms(toConnectRoom, connectedRoom);
                        connectedToStart.Add(toConnectRoom);

                        Queue <MapRegion> neighbours = new Queue <MapRegion>();
                        foreach (MapRegion reg in mapHolder.GetNeighbourRegions(toConnectRoom.region))
                        {
                            // pokud to neni empty region, pridat jeho sousedy do fronty
                            if (!reg.empty)
                            {
                                neighbours.Enqueue(reg);
                            }
                        }

                        checks = new List <MapRegion>();

                        // vzit vsechny sousedy startovni mistnosti
                        while (neighbours.Count > 0)
                        {
                            MapRegion reg = neighbours.Dequeue();

                            checks.Add(reg);

                            // jen ty ktere MAJI BYT pripojene
                            if (reg == null || !reg.isAccessibleFromStart)                             // TODO check if already connected?
                            {
                                continue;
                            }

                            // soused startovniho regionu je jeho potomek - jsou spojeny - pridat do seznamu connectedToStart
                            if (reg.IsInFamily(toConnectRoom.region))
                            {
                                //Debug.Log("region " + reg.x + ", " + reg.y + " je ve family s toConnect (" + toConnectRoom.region.x + ", " + toConnectRoom.region.y + ")");
                                foreach (MapRoom r in GetRoomsInRegion(reg))
                                {
                                    if (!connectedToStart.Contains(r))
                                    {
                                        connectedToStart.Add(r);                                         // maybe add duplicity check
                                    }
                                }
                                // pridat vsechny sousedy child regionu
                                foreach (MapRegion r in mapHolder.GetNeighbourRegions(reg))
                                {
                                    if (r != null && r.isAccessibleFromStart && !neighbours.Contains(r) && !checks.Contains(r))
                                    {
                                        //Debug.Log("pridavam souseda (reg " + reg.x + ", " + reg.y + ")  : " + r.x + ", " + r.y);
                                        neighbours.Enqueue(r);
                                    }
                                }

                                continue;
                            }

                            // pridat do seznamu k pripojeni
                            foreach (MapRoom r in GetRoomsInRegion(reg.GetParentOrSelf()))
                            {
                                if (!r.isAccessibleFromStartRoom)
                                {
                                    toConnect.Enqueue(r);
                                }
                            }
                        }

                        // pripojit k prvni nalezene
                        if (toConnectRoom.region.onlyOnePassage)
                        {
                            break;
                        }
                    }
                }
            }
        }