Exemplo n.º 1
0
    private void GenerateWallsFromInnerBounds(List <RegionConnections> possibleConnections)
    {
        List <Direction> directions = new List <Direction>()
        {
            Direction.NORTH,
            Direction.EAST,
            Direction.SOUTH,
            Direction.WEST
        };

        BoundsInt outerBounds = new BoundsInt(innerBounds.min, innerBounds.size);

        walls = new List <Wall>();
        for (int i = 0; i < directions.Count; i++)
        {
            Direction wallDir = directions[i];

            RegionConnections conns = possibleConnections.Find(x => x.direction == wallDir);

            List <BoundsInt> newPossibleConnections = new List <BoundsInt>();

            for (int j = 0; conns != null && j < conns.boundsList.Count; j++)
            {
                BoundsInt newConnection = new BoundsInt(conns.boundsList[j].position + Vector3Int.RoundToInt(innerBounds.center), conns.boundsList[j].size);
                newPossibleConnections.Add(newConnection);
            }

            BoundsInt wallBounds;
            switch (wallDir)
            {
            case Direction.NORTH:
                wallBounds        = new BoundsInt(innerBounds.x, innerBounds.yMax, 0, innerBounds.size.x, Wall.thicknessNorth, 1);
                outerBounds.yMax += Wall.thicknessNorth;
                break;

            case Direction.EAST:
                wallBounds        = new BoundsInt(innerBounds.xMax, innerBounds.y, 0, Wall.thicknessEast, innerBounds.size.y, 1);
                outerBounds.xMax += Wall.thicknessWest;
                break;

            case Direction.SOUTH:
                wallBounds        = new BoundsInt(innerBounds.x, innerBounds.y - Wall.thicknessSouth, 0, innerBounds.size.x, Wall.thicknessSouth, 1);
                outerBounds.yMin -= Wall.thicknessSouth;
                break;

            case Direction.WEST:
                wallBounds        = new BoundsInt(innerBounds.x - Wall.thicknessWest, innerBounds.y, 0, Wall.thicknessWest, innerBounds.size.y, 1);
                outerBounds.xMin -= Wall.thicknessWest;
                break;

            default:
                throw new Exception("Unknown wall direction! '" + wallDir.ToString() + "'");
            }

            Wall newWall = new Wall(wallDir, wallBounds, newPossibleConnections);
            walls.Add(newWall);
        }

        this.outerBounds = outerBounds;
    }
    protected virtual void Convert()
    {
        //if (regionFurnitures == null)
        regionFurnitures = new List <RegionFurnitures>();
        for (int i = 0; i < furnitureParent.transform.childCount; i++)
        {
            GameObject obj = furnitureParent.transform.GetChild(i).gameObject;
            if (obj.transform.childCount > 0)
            {
                RegionFurnitures rf = new RegionFurnitures(obj.name);

                for (int j = 0; j < obj.transform.childCount; j++)
                {
                    Transform trans = obj.transform.GetChild(j).transform;
                    rf.AddPosition(trans.localPosition, trans.rotation, trans.localScale);
                }

                regionFurnitures.Add(rf);
            }
        }

        //if (regionConnections == null)
        regionConnections = new List <RegionConnections>();
        for (int i = 0; i < connectionParent.transform.childCount; i++)
        {
            GameObject  obj  = connectionParent.transform.GetChild(i).gameObject;
            Component[] cmps = obj.GetComponents(typeof(BoxCollider2D));

            //Debug.Log(cmps.Length + " colliders found for " + obj.name);

            if (cmps.Length > 0)
            {
                RegionConnections con = new RegionConnections(obj.name);
                for (int j = 0; j < cmps.Length; j++)
                {
                    Bounds bounds = ((BoxCollider2D)cmps[j]).bounds;
                    bounds.min -= pos;
                    bounds.max -= pos;
                    con.AddConnection(bounds);
                }

                regionConnections.Add(con);
            }
        }
    }
    protected virtual void OnDrawGizmos()
    {
        if (drawGizmos)
        {
            Gizmos.color = Color.white;
            Gizmos.DrawWireCube(pos, size + Vector3.one * 2.1f);

            Gizmos.color = Color.green;
            Gizmos.DrawWireCube(pos + Vector3.forward, size - Vector3.one * 0.1f);

            if (regionFurnitures != null)
            {
                Gizmos.color = Color.blue;
                for (int i = 0; i < regionFurnitures.Count; i++)
                {
                    RegionFurnitures rf = regionFurnitures[i];
                    for (int j = 0; j < rf.spawnTransforms.Count; j++)
                    {
                        // Convert to world coords
                        Gizmos.DrawCube(rf.spawnTransforms[j].position + pos, Vector3.one * 0.25f);
                    }
                }
            }

            if (regionConnections != null)
            {
                Gizmos.color = Color.magenta;
                for (int i = 0; i < regionConnections.Count; i++)
                {
                    RegionConnections rc = regionConnections[i];
                    for (int j = 0; j < rc.boundsList.Count; j++)
                    {
                        // Are world coords already
                        Gizmos.DrawCube(rc.boundsList[j].center + pos, rc.boundsList[j].size - Vector3.one * 0.25f);
                    }
                }
            }
        }
    }
Exemplo n.º 4
0
    private BoundsInt GetRandomConnection(VariantRegion varRegion, Direction dir)
    {
        RegionConnections conns = varRegion.connections.Find(x => x.direction == dir);

        return(GetRandomConnection(conns.boundsList));
    }
Exemplo n.º 5
0
    private Region CreateRegionAtRandomConnection(Wall w, bool createCorridor, PremadeRegion regionLayout = null)
    {
        CustomRegion         customRegion;
        List <VariantRegion> possibleRegionLayouts;
        List <BoundsInt>     possibleConnections = new List <BoundsInt>(w.possibleConnections);

        if (regionLayout == null)
        {
            if (createCorridor)
            {
                customRegion = corridorLayouts;
            }
            else
            {
                customRegion = roomLayouts;
            }
            //Debug.Log("Create Region At Random Connection - " + "useSeperateVerticalRegions: " + customRegion.useSeperateVerticalRegions + " | Wall is Vertical: " + w.isVertical);
            if (customRegion.useSeperateVerticalRegions && !w.isVertical)
            {
                possibleRegionLayouts = customRegion.verticalRegionVariations;
            }
            else
            {
                possibleRegionLayouts = customRegion.regionVariations;
            }
        }
        else
        {
            //Debug.Log("Creating a premade region.");
            customRegion = new CustomRegion();

            VariantRegion vr;
            if (regionLayout.GetType() == typeof(VariantRegion))
            {
                vr = (VariantRegion)regionLayout;
            }
            else
            {
                vr = new VariantRegion(regionLayout);
            }
            possibleRegionLayouts = new List <VariantRegion>()
            {
                vr
            };
            customRegion.regionVariations = possibleRegionLayouts;
            customRegion.type             = regionLayout.type;
        }
        while (possibleConnections.Count > 0)
        {
            // Get and remove a connection from the list
            BoundsInt con = possibleConnections[Random.Range(0, possibleConnections.Count)];
            possibleConnections.Remove(con);

            List <VariantRegion> regionLayouts = new List <VariantRegion>(possibleRegionLayouts);

            while (regionLayouts.Count > 0)
            {
                // Get and remove a layout from the list to avoid duplicate checking
                VariantRegion layout = regionLayouts[Random.Range(0, regionLayouts.Count)];
                regionLayouts.Remove(layout);

                // Test layout connections against possbile connections
                RegionConnections vrcs = layout.connections.Find(x => x.direction == Region.GetOppositeDirection(w.dir));
                if (vrcs == null)
                {
                    continue;
                }

                List <BoundsInt> variantConnections = new List <BoundsInt>(vrcs.boundsList);
                while (variantConnections.Count > 0)
                {
                    // Get and remove a connection from the list
                    BoundsInt varcon = variantConnections[Random.Range(0, variantConnections.Count)];
                    variantConnections.Remove(varcon);

                    Vector3Int alignmentOffset = AlignConnections(con, varcon, w.dir);

                    Vector3Int size     = new Vector3Int(layout.innerRegionWidth, layout.innerRegionLength, 1);
                    Vector3Int position = Vector3Int.RoundToInt(alignmentOffset - new Vector3(size.x / 2f, size.y / 2f));

                    testBounds = Region.GetOuterBounds(position, size);


                    bool isOverlapping = false;
                    for (int i = 0; i < regions.Count; i++)
                    {
                        if (Region.BoundsOverlap(regions[i].outerBounds, testBounds, 1))
                        {
                            isOverlapping = true;
                            break;
                        }
                    }

                    // Place region
                    if (!isOverlapping)
                    {
                        Region newRegion = new Region(layout, position);

                        if (newRegion.type == RegionType.None)
                        {
                            newRegion.type = customRegion.type;
                        }

                        if (newRegion.floorTiles == null)
                        {
                            newRegion.floorTiles = customRegion.tilesets[Random.Range(0, customRegion.tilesets.Count)];
                        }
                        return(newRegion);
                    }
                }
            }
        }

        // No new Region could be created
        return(null);
    }