Beispiel #1
0
    public void GenerateMap(ref int[] map, BaseMapContext mapGenContext)
    {
        _context    = (BSPContext)mapGenContext;
        _bspGenData = (BSPGeneratorData)_context.GeneratorData;


        if (_bspGenData.IsSeeded)
        {
            URandom.state = JsonUtility.FromJson <URandom.State>(_bspGenData.Seed);
        }
        else
        {
            Debug.Log("Current state: " + JsonUtility.ToJson(URandom.state));
        }


        int[,] mapAux = new int[_bspGenData.MapSize.x, _bspGenData.MapSize.y];
        mapAux.Fill <int>(_bspGenData.NoTile);

        var tree = new BSPNode();

        _context.Tree = tree;
        tree.context  = _context;
        tree.left     = tree.right = null;
        tree.area     = new BSPRect(1, 1, _bspGenData.MapSize.x - 2, _bspGenData.MapSize.y - 2);

        GenerateRooms(ref mapAux);
        GeneratorUtils.ConvertGrid(mapAux, out map);
    }
Beispiel #2
0
    public bool Split()
    {
        float            hsplitRoll      = URandom.value;
        BSPGeneratorData bspData         = context.BSPData;
        bool             horizontalSplit = hsplitRoll < bspData.HorizontalSplitChance;
        float            hRatio          = area.Width / (float)area.Height;
        float            vRatio          = 1 / hRatio;

        if (hRatio >= 1.0f + bspData.VerticalSplitRatio)
        {
            horizontalSplit = false;
        }
        else if (vRatio > 1.0f + bspData.HorizontalSplitRatio)
        {
            horizontalSplit = true;
        }

        int maxSize = 0;
        int minSize = 0;

        if (horizontalSplit)
        {
            maxSize = area.Height - bspData.MinAreaSize.x;
            minSize = bspData.MinAreaSize.x;
        }
        else
        {
            maxSize = area.Width - bspData.MinAreaSize.y;
            minSize = bspData.MinAreaSize.x;
        }

        if (maxSize <= minSize)
        {
            return(false);
        }

        int splitValue = URandom.Range(minSize, maxSize + 1);

        left         = new BSPNode();
        left.context = context;

        right         = new BSPNode();
        right.context = context;

        if (horizontalSplit)
        {
            left.area  = new BSPRect(area.Row, area.Col, splitValue, area.Width);
            right.area = new BSPRect(area.Row + splitValue, area.Col, area.Height - splitValue, area.Width);
        }
        else
        {
            left.area  = new BSPRect(area.Row, area.Col, area.Height, splitValue);
            right.area = new BSPRect(area.Row, area.Col + splitValue, area.Height, area.Width - splitValue);
        }

        left.Split();
        right.Split();
        return(true);
    }