Example #1
0
        private void CreatePassage(LandBlock blockA, LandBlock blockB, Location tileA, Location tileB)
        {
            LandBlock.ConnectBlocks(blockA, blockB);

            //Debug.DrawLine (CoordToWorldPoint (tileA), CoordToWorldPoint (tileB), Color.green, 3);

            List <Location> line = GetLine(tileA, tileB);

            foreach (Location c in line)
            {
                DrawCircle(c, passageWidth);
            }
        }
Example #2
0
        private void ConnectClosestBlocks(List <LandBlock> allBlocks, bool forceAccessibilityFromMainBlock = false)
        {
            List <LandBlock> blockListA = new List <LandBlock>();
            List <LandBlock> blockListB = new List <LandBlock>();

            if (forceAccessibilityFromMainBlock)
            {
                foreach (LandBlock block in allBlocks)
                {
                    if (block.isAccessibleFromMainBlock)
                    {
                        blockListB.Add(block);
                    }
                    else
                    {
                        blockListA.Add(block);
                    }
                }
            }
            else
            {
                blockListA = allBlocks;
                blockListB = allBlocks;
            }

            int       bestDistance            = 0;
            Location  bestTileA               = new Location();
            Location  bestTileB               = new Location();
            LandBlock bestBlockA              = new LandBlock();
            LandBlock bestBlockB              = new LandBlock();
            bool      possibleConnectionFound = false;

            foreach (LandBlock blockA in blockListA)
            {
                if (!forceAccessibilityFromMainBlock)
                {
                    possibleConnectionFound = false;
                    if (blockA.connectedBlocks.Count > 0)
                    {
                        continue;
                    }
                }

                foreach (LandBlock blockB in blockListB)
                {
                    if (blockA == blockB || blockA.IsConnected(blockB))
                    {
                        continue;
                    }

                    for (int tileIndexA = 0; tileIndexA < blockA.edgeTiles.Count; tileIndexA++)
                    {
                        for (int tileIndexB = 0; tileIndexB < blockB.edgeTiles.Count; tileIndexB++)
                        {
                            Location tileA = blockA.edgeTiles[tileIndexA];
                            Location tileB = blockB.edgeTiles[tileIndexB];
                            int      distanceBetweenBlocks = (int)(Mathf.Pow(tileA.x - tileB.x, 2) + Mathf.Pow(tileA.y - tileB.y, 2));

                            if (distanceBetweenBlocks < bestDistance || !possibleConnectionFound)
                            {
                                bestDistance            = distanceBetweenBlocks;
                                possibleConnectionFound = true;
                                bestTileA  = tileA;
                                bestTileB  = tileB;
                                bestBlockA = blockA;
                                bestBlockB = blockB;
                            }
                        }
                    }
                }
                if (possibleConnectionFound && !forceAccessibilityFromMainBlock)
                {
                    CreatePassage(bestBlockA, bestBlockB, bestTileA, bestTileB);
                }
            }

            if (possibleConnectionFound && forceAccessibilityFromMainBlock)
            {
                CreatePassage(bestBlockA, bestBlockB, bestTileA, bestTileB);
                ConnectClosestBlocks(allBlocks, true);
            }

            if (!forceAccessibilityFromMainBlock)
            {
                ConnectClosestBlocks(allBlocks, true);
            }
        }