public void RebuildNodesOnHighSectorEdges(IntVector3 areaWithSectors, int side)
        {
            WorldArea        area         = manager.worldData.worldAreas[areaWithSectors.x];
            MultiLevelSector sector       = area.sectorGrid[0][areaWithSectors.y];
            MultiLevelSector higherSector = manager.GetHigherSectorFromLower(1, sector, area);

            Debug.Log("Lower level  start " + areaWithSectors.y + "  end " + areaWithSectors.z);

            // clear out both sides  //area.sectorGrid[1][areaWithSectors.z]
            manager.RemoveAllAbstractNodesOnSectorEdge(higherSector, side);
            manager.RemoveAllAbstractNodesOnSectorEdge(manager.GetHigherSectorFromLower(1, area.sectorGrid[0][areaWithSectors.z], area), manager.FlipDirection(side));

            // rebuild side
            RebuildNodesOnHighSectorEdge(1, higherSector, side, manager.EdgeIndexToVector(side), area);
        }
Exemple #2
0
        private void RebuildNodesOnSectorEdge(MultiLevelSector sector, int edgeIndex, int edgeIndexNeighbourSector,
                                              Vector2 startInSector, Vector2 startInNeighbourSector, Vector2 direction, WorldArea area)
        {
            // remove connections to sector nodes on edge + remove them and those directly linked on neighbour sector
            Manager.RemoveAllAbstractNodesOnSectorEdge(sector, edgeIndex);

            var maxStep = direction == Vector2.right ? sector.TilesInWidth : sector.TilesInHeight;

            int sec = -1;

            for (int i = 0; i < maxStep; i++)
            {
                Tile neighbour =
                    area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * i][
                        (int)startInNeighbourSector.y + (int)direction.y * i];
                if (neighbour != null)
                {
                    sec = neighbour.SectorIndex;
                    Manager.RemoveAllAbstractNodesOnSectorEdge(area.SectorGrid[0][sec], edgeIndexNeighbourSector);
                    break;
                }
            }

            if (sec != -1) // if we haven't found any tiles, no reason to try and build connections
            {
                // build nodes on edge
                bool sectorNodesOpen  = false;
                int  openLength       = -1;
                int  startNodeOfGroup = 0;

                for (int i = 0; i < maxStep; i++)
                {
                    var tile1 = area.TileGrid[(int)startInSector.x + (int)direction.x * i][
                        (int)startInSector.y + (int)direction.y * i];
                    var tile2 = area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * i][
                        (int)startInNeighbourSector.y + (int)direction.y * i];

                    if (tile1 != null && tile2 != null && !tile1.Blocked && !tile2.Blocked &&
                        Manager.WorldData.TileManager.TilesWithinRangeGeneration(tile1, tile2))
                    {
                        // starting point of a new connection/gate between sectors
                        if (!sectorNodesOpen)
                        {
                            sectorNodesOpen = true;
                        }

                        openLength++;
                    }
                    else
                    {
                        if (sectorNodesOpen) // if we have had a couple of open nodes couples
                        {
                            // small enough to represent with 1 transition
                            if (openLength < Manager.MaxGateSize)
                            {
                                int  steps         = Mathf.FloorToInt(openLength * 0.5f) + startNodeOfGroup;
                                Tile neighbourTile =
                                    area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * steps][
                                        (int)startInNeighbourSector.y + (int)direction.y * steps];
                                Manager.CreateSectorNodes(sector, neighbourTile.SectorIndex, edgeIndex,
                                                          edgeIndexNeighbourSector,
                                                          area.TileGrid[(int)startInSector.x + (int)direction.x * steps][
                                                              (int)startInSector.y + (int)direction.y * steps], neighbourTile, area);
                            }
                            else
                            {
                                // to large, 2 transitions. on on each end
                                int  multilayer    = startNodeOfGroup;
                                Tile neighbourTile =
                                    area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * multilayer][
                                        (int)startInNeighbourSector.y + (int)direction.y * multilayer];
                                Manager.CreateSectorNodes(sector, neighbourTile.SectorIndex, edgeIndex,
                                                          edgeIndexNeighbourSector,
                                                          area.TileGrid[(int)startInSector.x + (int)direction.x * multilayer][
                                                              (int)startInSector.y + (int)direction.y * multilayer], neighbourTile, area);

                                multilayer    = (startNodeOfGroup + openLength);
                                neighbourTile =
                                    area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * multilayer][
                                        (int)startInNeighbourSector.y + (int)direction.y * multilayer];
                                Manager.CreateSectorNodes(sector, neighbourTile.SectorIndex, edgeIndex,
                                                          edgeIndexNeighbourSector,
                                                          area.TileGrid[(int)startInSector.x + (int)direction.x * multilayer][
                                                              (int)startInSector.y + (int)direction.y * multilayer], neighbourTile, area);
                            }

                            openLength      = -1;
                            sectorNodesOpen = false;
                        }

                        startNodeOfGroup = i + 1;
                    }
                }

                if (sectorNodesOpen) // if we have had a couple of open nodes couples
                {
                    if (openLength < Manager.MaxGateSize)
                    {
                        int  steps         = Mathf.FloorToInt(openLength * 0.5f) + startNodeOfGroup;
                        Tile neighbourTile =
                            area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * steps][
                                (int)startInNeighbourSector.y + (int)direction.y * steps];
                        Manager.CreateSectorNodes(sector, neighbourTile.SectorIndex, edgeIndex,
                                                  edgeIndexNeighbourSector,
                                                  area.TileGrid[(int)startInSector.x + (int)direction.x * steps][
                                                      (int)startInSector.y + (int)direction.y * steps], neighbourTile, area);
                    }
                    else
                    {
                        // to large, 2 transitions. on on each end
                        int  multilayer    = startNodeOfGroup;
                        Tile neighbourTile =
                            area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * multilayer][
                                (int)startInNeighbourSector.y + (int)direction.y * multilayer];
                        Manager.CreateSectorNodes(sector, neighbourTile.SectorIndex, edgeIndex,
                                                  edgeIndexNeighbourSector,
                                                  area.TileGrid[(int)startInSector.x + (int)direction.x * multilayer][
                                                      (int)startInSector.y + (int)direction.y * multilayer], neighbourTile, area);

                        multilayer    = (startNodeOfGroup + openLength);
                        neighbourTile =
                            area.TileGrid[(int)startInNeighbourSector.x + (int)direction.x * multilayer][
                                (int)startInNeighbourSector.y + (int)direction.y * multilayer];
                        Manager.CreateSectorNodes(sector, neighbourTile.SectorIndex, edgeIndex,
                                                  edgeIndexNeighbourSector,
                                                  area.TileGrid[(int)startInSector.x + (int)direction.x * multilayer][
                                                      (int)startInSector.y + (int)direction.y * multilayer], neighbourTile, area);
                    }
                }
            }
        }