Example #1
0
    bool ConfigMatches(GridDungeonModel Model, IntVector Point, CellSpatialConfig3D Config)
    {
        var centerCellInfo = Model.GetGridCellLookup(Point.x, Point.z);
        var neighbors      = Config.NeighborConfig;

        for (int i = 0; i < neighbors.Length; i++)
        {
            int code = neighbors[i];
            if (code == 0)
            {
                // Don't care about this cell
                continue;
            }
            int dx = i % 3;
            int dz = i / 3;
            dx--; dz--;    // bring to -1..1 range (from previous 0..2)
            //dy *= -1;
            int x = Point.x + dx;
            int z = Point.z + dz;

            var  cellInfo = Model.GetGridCellLookup(x, z);
            bool empty    = cellInfo.CellType == CellType.Unknown;
            if (!centerCellInfo.ContainsDoor)
            {
                empty |= IsRoomCorridor(centerCellInfo.CellType, cellInfo.CellType);
            }
            if (!empty && centerCellInfo.CellType == CellType.Room && cellInfo.CellType == CellType.Room && centerCellInfo.CellId != cellInfo.CellId)
            {
                if (!mergeRoomCorridor)
                {
                    empty = true;
                }
            }
            if (!empty)
            {
                var cell0 = Model.GetCell(cellInfo.CellId);
                var cell1 = Model.GetCell(centerCellInfo.CellId);
                if (cell0.Bounds.Location.y != cell1.Bounds.Location.y)
                {
                    empty = true;
                }
            }

            if (code == 1 && empty)
            {
                // We were expecting a non-empty space here, but it is empty
                return(false);
            }
            else if (code == 2 && !empty)
            {
                // We were expecting a empty space here, but it is not empty
                return(false);
            }
        }

        // Matches, all tests have passed
        return(true);
    }
Example #2
0
    void DrawCells(List <int> cellIds, Material mat)
    {
        if (gridModel == null || gridModel.Config == null)
        {
            return;
        }
        var gridSize = gridModel.Config.GridCellSize;

        mat.SetPass(0);

        GL.Begin(GL.QUADS);

        foreach (var cellId in cellIds)
        {
            var cell = gridModel.GetCell(cellId);
            if (cell == null)
            {
                continue;
            }

            var start = cell.Bounds.Location * gridSize;
            var size  = cell.Bounds.Size * gridSize;

            DrawQuad(start, size);
        }

        GL.End();
    }
Example #3
0
    void PostInitializeForGridBuilder(Dungeon dungeon, GridDungeonModel gridModel)
    {
        var _groupInfoArray = GameObject.FindObjectsOfType <DungeonItemGroupInfo>();

        Dictionary <int, DungeonItemGroupInfo> groupObjectByCellId = new Dictionary <int, DungeonItemGroupInfo>();

        foreach (var groupInfo in _groupInfoArray)
        {
            if (groupInfo.dungeon == dungeon)
            {
                var cellId = groupInfo.groupId;
                var cell   = gridModel.GetCell(cellId);
                if (cell == null || cell.CellType == CellType.Unknown)
                {
                    continue;
                }


                string objectNamePrefix = "";
                if (cell.CellType == CellType.Room)
                {
                    objectNamePrefix = "Room_";
                }
                else
                {
                    groupObjectByCellId[cell.Id] = groupInfo;

                    objectNamePrefix = (cell.CellType == CellType.Corridor) ? "CorridorBlock_" : "CorridorPad_";
                }

                if (objectNamePrefix.Length == 0)
                {
                    objectNamePrefix = "Cell_";
                }

                string groupName = objectNamePrefix + cell.Id;
                groupInfo.gameObject.name = groupName;
            }
        }


        var visited           = new HashSet <int>();
        int clusterCounter    = 1;
        var oldGroupsToDelete = new List <GameObject>();

        foreach (var groupInfo in groupObjectByCellId.Values)
        {
            var cellId = groupInfo.groupId;
            if (visited.Contains(cellId))
            {
                continue;
            }

            var clusters     = GridBuilderUtils.GetCellCluster(gridModel, cellId);
            var itemsToGroup = new List <GameObject>();

            // Mark all cluster cells as visited
            foreach (var clusterItemId in clusters)
            {
                visited.Add(clusterItemId);
                if (groupObjectByCellId.ContainsKey(clusterItemId))
                {
                    var clusterItemGroupInfo = groupObjectByCellId[clusterItemId];
                    for (int i = 0; i < clusterItemGroupInfo.transform.childCount; i++)
                    {
                        var childObject = clusterItemGroupInfo.transform.GetChild(i);
                        itemsToGroup.Add(childObject.gameObject);
                    }
                    oldGroupsToDelete.Add(clusterItemGroupInfo.gameObject);
                }
            }

            int clusterId = clusterCounter++;
            GroupItems(itemsToGroup.ToArray(), "Corridor_" + clusterId, dungeon, clusterId);
        }

        groupObjectByCellId.Clear();

        // Destroy the inner group info objects
        foreach (var itemToDestory in oldGroupsToDelete)
        {
            EditorDestroyObject(itemToDestory);
        }
    }
Example #4
0
        void BuildGridWaypoints(GridDungeonModel gridModel, LevelMarkerList markers)
        {
            mode2D = gridModel.Config.Mode2D;

            // Destroy all existing waypoints
            DestroyAllWaypoints();

            var cellToWaypoint = new Dictionary <int, Waypoint>();

            int idCounter = 1;

            var wall2DPositions = new HashSet <IntVector>();

            if (mode2D)
            {
                foreach (var marker in markers)
                {
                    if (marker.SocketType == DungeonConstants.ST_WALL2D)
                    {
                        wall2DPositions.Add(marker.gridPosition);
                    }
                }
            }

            // Create a waypoint on each cell
            foreach (var cell in gridModel.Cells)
            {
                if (mode2D)
                {
                    if (wall2DPositions.Contains(cell.Bounds.Location))
                    {
                        // Don't want to create a waypoint on a wall tile
                        continue;
                    }
                }
                var worldPos = MathUtils.GridToWorld(gridModel.Config.GridCellSize, cell.CenterF);
                worldPos += waypointOffset;
                if (mode2D)
                {
                    worldPos = FlipYZ(worldPos);
                }
                var waypointObject = Instantiate(waypointTemplate, worldPos, Quaternion.identity) as GameObject;
                waypointObject.transform.parent = waypointParent.transform;

                var waypoint = waypointObject.GetComponent <Waypoint>();
                waypoint.id = idCounter++;
                cellToWaypoint.Add(cell.Id, waypoint);
            }

            // Connect adjacent waypoints
            foreach (var cellId in cellToWaypoint.Keys)
            {
                var waypoint          = cellToWaypoint[cellId];
                var cell              = gridModel.GetCell(cellId);
                var adjacentWaypoints = new List <Waypoint>();
                var visited           = new HashSet <int>();
                foreach (var adjacentCellId in cell.AdjacentCells)
                {
                    if (visited.Contains(GetHash(cellId, adjacentCellId)))
                    {
                        continue;
                    }

                    var adjacentCell = gridModel.GetCell(adjacentCellId);
                    // add only if there is a direct path to it (through a door or stair or open space)
                    bool directPath = HasDirectPath(gridModel, cell, adjacentCell);
                    if (directPath)
                    {
                        if (cellToWaypoint.ContainsKey(adjacentCellId))
                        {
                            var adjacentWaypoint = cellToWaypoint[adjacentCellId];
                            adjacentWaypoints.Add(adjacentWaypoint);
                            visited.Add(GetHash(cellId, adjacentCellId));
                            visited.Add(GetHash(adjacentCellId, cellId));
                        }
                    }
                }
                waypoint.AdjacentWaypoints = adjacentWaypoints.ToArray();
            }
        }