Exemplo n.º 1
0
    public Region(PremadeRegion premadeRegion, Vector3Int position)
    {
        id = NEXT_ID++;

        this.innerBounds = new BoundsInt(position.x, position.y, 0, premadeRegion.innerRegionWidth, premadeRegion.innerRegionLength, 1);

        this.type = premadeRegion.type;

        this.furnitures = new List <RegionFurnitures>(premadeRegion.furnitures);
        if (this.variableFurnitures == null)
        {
            this.variableFurnitures = new List <VariableRegionFurnitures>();
        }

        this.floorTiles = premadeRegion.tileset;

        this.placedFurnitures = new List <GameObject>();

        GenerateWallsFromInnerBounds(premadeRegion.connections);
    }
Exemplo n.º 2
0
    protected override void ApplyRegionToScriptableObject()
    {
        if (createPrefab)
        {
            premadeRegionObject = (PremadeRegion)ScriptableObject.CreateInstance(typeof(PremadeRegion));
        }

        if (premadeRegionObject != null && (createPrefab || updatePrefab))
        {
            premadeRegionObject.Init(this.name, innerRegionWidth, innerRegionLength, regionFurnitures, regionConnections, floorTileset);

            updatePrefab = false;
        }


        if (createPrefab)
        {
            // http://wiki.unity3d.com/index.php/CreateScriptableObjectAsset

            string path = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (path == "")
            {
                path = "Assets";
            }
            else if (Path.GetExtension(path) != "")
            {
                path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
            }

            // Create Premade Region
            string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + gameObject.name + " (Premade Region).asset");
            AssetDatabase.CreateAsset(premadeRegionObject, assetPathAndName);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = premadeRegionObject;

            createPrefab = false;
        }
    }
Exemplo n.º 3
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);
    }