示例#1
0
    void GenerateNodes(TileRules nodetype, int amount, Dictionary <string, List <SpawnPoint> > spawns, int size)
    {
        List <SpawnPoint> availableSpawns = new List <SpawnPoint>();

        foreach (string key in spawns.Keys)
        {
            if (nodetype.CheckLimitations(key))
            {
                availableSpawns.AddRange(spawns[key]);
            }
        }
        for (int i = 0; i < amount; i++)
        {
            if (tempEdges.Count != 0 && nodetype.GetSelf() == tree)
            {
                tempEdges.Clear();
            }
            int        iterator = Random.Range(0, availableSpawns.Count);
            SpawnPoint spawn    = availableSpawns[iterator];
            GenerateArea(size, spawn.x, spawn.y, nodetype, true);
            availableSpawns.Remove(spawn);
            if (nodetype.GetSelf() == tree)
            {
                List <Vector3Int> actualEdges = new List <Vector3Int>();
                if (tempEdges.Count == 0)
                {
                    continue;
                }                                      // for(amount)
                foreach (Vector3Int selfPosition in tempEdges)
                {
                    if (nodes.GetTile(new Vector3Int(selfPosition.x + 1, selfPosition.y, 0)) == null)
                    {
                        actualEdges.Add(selfPosition);
                        continue;
                    }
                    else if (nodes.GetTile(new Vector3Int(selfPosition.x, selfPosition.y + 1, 0)) == null)
                    {
                        actualEdges.Add(selfPosition);
                        continue;
                    }
                    else if (nodes.GetTile(new Vector3Int(selfPosition.x - 1, selfPosition.y, 0)) == null)
                    {
                        actualEdges.Add(selfPosition);
                        continue;
                    }
                    else if (nodes.GetTile(new Vector3Int(selfPosition.x, selfPosition.y - 1, 0)) == null)
                    {
                        actualEdges.Add(selfPosition);
                        continue;
                    }
                }
                GameObject.Find("Grid").GetComponent <Environment>().AddForest(actualEdges);
            }
        }
    }
示例#2
0
    void GenerateArea(int length, int x, int y, TileRules tile, bool onExisting = false)
    {
        if (Random.Range(0, 10) > 5)
        {
            length -= 1;
        }
        if (length <= 0)
        {
            return;
        }
        length -= 1;
        if (Random.Range(0, 10) <= 2)
        {
            length += 1;
        }

        // Create tile on current position
        if (onExisting && map.GetTile(new Vector3Int(x, y, 0)) != null)
        {
            if (CheckTilesAt(x, y, nodes, tile) && CheckTilesAt(x, y, map, tile))
            {
                SetNewTileOnNodes(new Vector3Int(x, y, 0), tile.GetSelf());
            }
        }
        else if (!onExisting && CheckTilesAt(x, y, nodes, tile) && CheckTilesAt(x, y, map, tile))
        {
            SetNewTileOnNodes(new Vector3Int(x, y, 0), tile.GetSelf());
        }

        bool TilesAdded = false;

        if (y < height / 2)   // north
        {
            for (int north = 1; north < length + Random.Range(0, 2); north++)
            {
                if (CheckTilesAt(x, y + north, nodes, tile) && CheckTilesAt(x, y + north, map, tile) && y + north < height / 2)
                {
                    if ((onExisting && map.GetTile(new Vector3Int(x, y + north, 0)) == null))
                    {
                        break;
                    }
                    TilesAdded = true;
                    SetNewTileOnNodes(new Vector3Int(x, y + north, 0), tile.GetSelf());
                }
                else
                {
                    break;
                }
            }
        }
        if (x > -width / 2)     // west
        {
            for (int west = 1; west < length + Random.Range(0, 2); west++)
            {
                if (CheckTilesAt(x - west, y, nodes, tile) && CheckTilesAt(x - west, y, map, tile) && x - west > -width / 2)
                {
                    if (onExisting && map.GetTile(new Vector3Int(x - west, y, 0)) == null)
                    {
                        break;
                    }
                    TilesAdded = true;
                    SetNewTileOnNodes(new Vector3Int(x - west, y, 0), tile.GetSelf());
                }
                else
                {
                    break;
                }
            }
        }
        if (y > -height / 2)   // south
        {
            for (int south = 1; south < length + Random.Range(0, 2); south++)
            {
                if (CheckTilesAt(x, y - south, nodes, tile) && CheckTilesAt(x, y - south, map, tile) && y - south > -height / 2)
                {
                    if (onExisting && map.GetTile(new Vector3Int(x, y - south, 0)) == null)
                    {
                        break;
                    }
                    TilesAdded = true;
                    SetNewTileOnNodes(new Vector3Int(x, y - south, 0), tile.GetSelf());
                }
                else
                {
                    break;
                }
            }
        }
        if (x > -width / 2)     // west
        {
            for (int east = 1; east < length + Random.Range(0, 2); east++)
            {
                if (CheckTilesAt(x + east, y, nodes, tile) && CheckTilesAt(x + east, y, map, tile) && x + east < width / 2)
                {
                    if (onExisting && map.GetTile(new Vector3Int(x + east, y, 0)) == null)
                    {
                        break;
                    }
                    TilesAdded = true;
                    SetNewTileOnNodes(new Vector3Int(x + east, y, 0), tile.GetSelf());
                }
                else
                {
                    break;
                }
            }
        }

        if (TilesAdded)
        {
            if (y + 1 < height / 2)
            {
                GenerateArea(length, x, y + 1, tile, onExisting);
            }
            if (y - 1 > -height / 2)
            {
                GenerateArea(length, x, y - 1, tile, onExisting);
            }
            if (x + 1 < width / 2)
            {
                GenerateArea(length, x + 1, y, tile, onExisting);
            }
            if (x - 1 > -width / 2)
            {
                GenerateArea(length, x - 1, y, tile, onExisting);
            }
        }
        return;
    }