Exemple #1
0
    // Body of instructions to apply in every step
    public override DecoratorCellState[,] OnCellStep(int x, int y, DecoratorCellState[,] map)
    {
        if (map[x, y] == DecoratorCellState.False && x + 1 < map.GetLength(0) && y + 1 < map.GetLength(1) && x > 0 && y > 0)
        {
            int emptyNeighboursCells = checkNeighboursForBuildings(map, x, y);
            if (emptyNeighboursCells > 3 && B2BGUtils.GetRand01() < _decorationProbability)
            {
                map[x, y]         = DecoratorCellState.Origin;
                map[x + 1, y]     = DecoratorCellState.Fragment;
                map[x, y + 1]     = DecoratorCellState.Fragment;
                map[x + 1, y + 1] = DecoratorCellState.Fragment;

                GameObject g          = Instantiate(_buildings[Random.Range(0, _buildings.Length)]);
                GameObject parentCell = _terrainData[x, y];
                Bounds     bounds;
                if (g.GetComponent <Renderer>() == null)
                {
                    bounds = g.GetComponentInChildren <Renderer>().bounds;
                }
                else
                {
                    bounds = g.GetComponent <Renderer>().bounds;
                }
                g.transform.position  = parentCell.transform.position;
                g.transform.position += new Vector3(1, 0, 1);
                g = RotateRandomNSEW(g);
                g.transform.SetParent(parentCell.transform);
                _decorationData[x, y] = g;
            }
        }
        return(map);
    }
    //Returns the number of cells in a ring around (x,y) that are alive.
    private int countAliveNeighbours(DecoratorCellState[,] map, int x, int y)
    {
        int count = 0;

        for (int i = -1; i < 2; i++)
        {
            for (int j = -1; j < 2; j++)
            {
                int neighbour_x = x + i;
                int neighbour_y = y + j;

                //If we're looking at the middle point
                if (i == 0 && j == 0)
                {
                    //Do nothing, we don't want to add ourselves in!
                }
                //In case the index we're looking at it off the edge of the map
                else if (neighbour_x < 0 || neighbour_y < 0 || neighbour_x >= map.GetLength(0) || neighbour_y >= map.GetLength(1))
                {
                    count = count + 1;
                }
                //Otherwise, a normal check of the neighbour
                else if (map[neighbour_x, neighbour_y] == DecoratorCellState.True)
                {
                    count = count + 1;
                }
            }
        }
        return(count);
    }
 public override DecoratorCellState[,] OnCellStep(int x, int y, DecoratorCellState[,] map)
 {
     if (map[x, y] == DecoratorCellState.False && x + 1 < map.GetLength(0) && y + 1 < map.GetLength(1) && x > 0 && y > 0)
     {
         if (B2BGUtils.GetRand01() < _decorationProbability)
         {
             map[x, y] = DecoratorCellState.Tree;
             GameObject parentCell = _terrainData[x, y];
             GameObject g          = Instantiate(_treesArray[Random.Range(0, _treesArray.Length)]);
             Bounds     bounds;
             if (g.GetComponent <Renderer>() == null)
             {
                 bounds = g.GetComponentInChildren <Renderer>().bounds;
             }
             else
             {
                 bounds = g.GetComponent <Renderer>().bounds;
             }
             g.transform.position = parentCell.transform.position;
             g.transform.SetParent(parentCell.transform);
             _decorationData[x, y] = g;
         }
     }
     return(map);
 }
    private DecoratorCellState[,] DoSimulationStep(DecoratorCellState[,] oldMap)
    {
        DecoratorCellState[,] newMap = new DecoratorCellState[width, height];
        //Loop over each row and column of the map
        for (int x = 0; x < oldMap.GetLength(0); x++)
        {
            for (int y = 0; y < oldMap.GetLength(1); y++)
            {
                int nbs = countAliveNeighbours(oldMap, x, y);
                //The new value is based on our simulation rules
                //First, if a cell is alive but has too few neighbours, kill it.
                if (oldMap[x, y] == DecoratorCellState.True)
                {
                    if (nbs < deathLimit)
                    {
                        newMap[x, y] = DecoratorCellState.False;
                    }
                    else
                    {
                        newMap[x, y] = DecoratorCellState.True;
                    }
                }
                //Otherwise, if the cell is dead now, check if it has the right number of neighbours to be 'born'
                else
                {
                    if (nbs > birthLimit)
                    {
                        newMap[x, y] = DecoratorCellState.True;
                    }
                    else
                    {
                        newMap[x, y] = DecoratorCellState.False;
                    }
                }

                if (oldMap[x, y] == DecoratorCellState.RoadDiagonal || oldMap[x, y] == DecoratorCellState.RoadSmooth || oldMap[x, y] == DecoratorCellState.RoadStripe)
                {
                    // Re write the new map with the road data
                    newMap[x, y] = oldMap[x, y];
                }
            }
        }


        return(newMap);
    }
    public override DecoratorCellState[,] OnCellStep(int x, int y, DecoratorCellState[,] map)
    {
        if (Random.Range(0f, 1f) < chanceToStartAlive && map[x, y] == DecoratorCellState.True)
        {
            map[x, y] = DecoratorCellState.True;
        }
        else if (map[x, y] == DecoratorCellState.True)
        {
            map[x, y] = DecoratorCellState.False;
        }
        if (x <= 10 && y <= 10 || (x == y) || (((x > 0 && y > 0)) && x + 1 == y - 1) || (((x < map.GetLength(0) && y < map.GetLength(1))) && x - 1 == y + 1))
        {
            map[x, y] = DecoratorCellState.True;
        }


        return(map);
    }