public void SetCellTerrainAt(Vector2Int position, Cell.TERRAIN_TYPE newType)
    {
        if (position.x < 0 || position.x >= _mapSize.x || position.y < 0 || position.y >= _mapSize.y)
        {
            Debug.LogError("IndexOutOfBounds. " + position);
        }

        _cells[position.x, position.y].SetTerrainType(newType);
    }
Example #2
0
    private int NextDijkstraTerrainIteration(ref List <List <Cell> > availableCellsList, int listIndex, Cell.TERRAIN_TYPE desiredTerrain, Vector2Int startCell)
    {
        List <Cell> availableCells = availableCellsList[listIndex];

        if (availableCells.Count == 0)
        {
            return(0);
        }

        //Choosing initial cell
        Cell initialCell = null;

        float minDistance    = 100000;
        int   indexToClosest = -1;

        for (int i = availableCells.Count - 1; i >= 0; i--)
        {
            Cell c = availableCells[i];

            Vector2Int dist = c.GetPosition() - startCell;
            float      d    = dist.magnitude;

            if (d < minDistance && c.GetTerrainType() == Cell.TERRAIN_TYPE.empty)
            {
                minDistance    = d;
                indexToClosest = i;
            }
        }

        if (indexToClosest == -1)
        {
            return(0);
        }

        initialCell = availableCells[indexToClosest];

        for (int i = availableCells.Count - 1; i >= 0; i--)
        {
            Cell c = availableCells[i];
            if (c.GetTerrainType() != Cell.TERRAIN_TYPE.empty || c == initialCell)
            {
                availableCells.RemoveAt(i);
                continue;
            }
        }
        //////

        //Adding surrounding cells to available list
        for (int x = initialCell.GetPosition().x - 1; x <= initialCell.GetPosition().x + 1; x++)
        {
            for (int y = initialCell.GetPosition().y - 1; y <= initialCell.GetPosition().y + 1; y++)
            {
                Vector2Int checkPosition = new Vector2Int(x, y);

                if (checkPosition == initialCell.GetPosition() ||
                    !_map.IsPositionViable(checkPosition) ||
                    availableCells.Contains(_map.GetCellAt(checkPosition)) ||
                    _map.GetCellAt(checkPosition).GetTerrainType() != Cell.TERRAIN_TYPE.empty)
                {
                    continue;
                }

                availableCells.Insert(0, _map.GetCellAt(checkPosition));
            }
        }
        //////

        initialCell.SetTerrainType(desiredTerrain);
        if (desiredTerrain == Cell.TERRAIN_TYPE.earth)
        {
            initialCell.GetGameObject().GetComponent <SpriteRenderer>().color = earthCellPrefab.GetComponent <SpriteRenderer>().color;
        }
        else if (desiredTerrain == Cell.TERRAIN_TYPE.sand)
        {
            initialCell.GetGameObject().GetComponent <SpriteRenderer>().color = sandCellPrefab.GetComponent <SpriteRenderer>().color;
        }
        else
        {
            initialCell.GetGameObject().GetComponent <SpriteRenderer>().color = waterCellPrefab.GetComponent <SpriteRenderer>().color;
        }

        return(1);
    }