Пример #1
0
        private static void CreatePassage(Region regionA, Region regionB, Vector3Int tileA, Vector3Int tileB)
        {
            List <Vector3Int> line       = HexTools.GetLine(tileA, tileB);
            Passageway        passageway = new Passageway(regionA, regionB, tileA, tileB, line);

            Region.ConnectRegions(regionA, regionB, passageway);
            //Debug.DrawLine(tilemap.CellToWorld(tileA), tilemap.CellToWorld(tileB), Color.green, 10);

            foreach (Vector3Int tileCoord in line)
            {
                mapGen.ChangeAllTilesAround(tileCoord, rules.passageSize, rules.openTile);
            }
        }
        private void ConnectClosestRooms(List <Room> allRooms, bool ensureConnectionToMainRoom = false)
        {
            var roomListA = new List <Room>();
            var roomListB = new List <Room>();

            if (ensureConnectionToMainRoom)
            {
                foreach (var room in allRooms)
                {
                    if (room.IsAccessibleFromMainRoom)
                    {
                        roomListB.Add(room);
                    }
                    else
                    {
                        roomListA.Add(room);
                    }
                }
            }
            else
            {
                roomListA = allRooms;
                roomListB = allRooms;
            }

            var passage = new Passageway(_passageRadius, _map)
            {
                Distance  = 0,
                StartTile = new Coord(),
                EndTile   = new Coord(),
                StartRoom = new Room(),
                EndRoom   = new Room()
            };

            var possibleConnectionFound = false;

            foreach (var roomA in roomListA)
            {
                if (!ensureConnectionToMainRoom)
                {
                    possibleConnectionFound = false;
                    if (roomA.HasAConnection)
                    {
                        continue;
                    }
                }


                foreach (var roomB in roomListB)
                {
                    if (roomA == roomB || roomA.IsConnected(roomB))
                    {
                        continue;
                    }

                    foreach (var tileA in roomA.EdgeTiles)
                    {
                        foreach (var tileB in roomB.EdgeTiles)
                        {
                            var distanceBetweenRooms = (int)(Mathf.Pow(tileA.TileX - tileB.TileX, 2)
                                                             + Mathf.Pow(tileA.TileY - tileB.TileY, 2));

                            if (distanceBetweenRooms < passage.Distance || !possibleConnectionFound)
                            {
                                passage = new Passageway(_passageRadius, _map)
                                {
                                    Distance  = distanceBetweenRooms,
                                    StartTile = tileA,
                                    EndTile   = tileB,
                                    StartRoom = roomA,
                                    EndRoom   = roomB
                                };
                                possibleConnectionFound = true;
                            }
                        }
                    }
                }

                if (possibleConnectionFound && !ensureConnectionToMainRoom)
                {
                    passage.Tunnel();
                }
            }


            if (possibleConnectionFound && ensureConnectionToMainRoom)
            {
                passage.Tunnel();
                ConnectClosestRooms(allRooms, true);
            }
            if (!ensureConnectionToMainRoom)
            {
                ConnectClosestRooms(allRooms, true);
            }
        }