Inheritance: MonoBehaviour
Esempio n. 1
0
 void OnDisable()
 {
     if (instance == this)
     {
         instance = null;
     }
 }
Esempio n. 2
0
    // Instantiate walls?
    void BuildWalls()
    {
        int cellsPerSide = CellMaster.CellsPerSide;

        var verticalWalls = new Cell[cellsPerSide, cellsPerSide];

        for (int x = 0; x < CellMaster.CellsPerSide - 1; x++)
        {
            int wallType = Random.Range(0, wallPrefabs.Length);
            for (int y = 0; y < CellMaster.CellsPerSide; y++)
            {
                if (!CellMaster.GetCellAt(x, y).canGoEast)
                {
                    CreateWall(CellMaster.GetCellAt(x, y), Direction.East, wallType);
                    verticalWalls[x, y] = CellMaster.GetCellAt(x, y);

                    if (y > 0 && verticalWalls[x, y - 1] == null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), true);
                    }
                }
                else
                {
                    wallType = Random.Range(0, wallPrefabs.Length);
                    if (y > 0 && verticalWalls[x, y - 1] != null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), true);
                    }
                }
            }
        }

        var horizontalWalls = new Cell[cellsPerSide, cellsPerSide];

        for (int y = 0; y < cellsPerSide - 1; y++)
        {
            int wallType = Random.Range(0, wallPrefabs.Length);
            for (int x = 0; x < cellsPerSide; x++)
            {
                if (!CellMaster.GetCellAt(x, y).canGoNorth)
                {
                    CreateWall(CellMaster.GetCellAt(x, y), Direction.North, wallType);
                    horizontalWalls[x, y] = CellMaster.GetCellAt(x, y);

                    if (x > 0 && horizontalWalls[x - 1, y] == null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), false);
                    }
                }
                else
                {
                    wallType = Random.Range(0, wallPrefabs.Length);
                    if (x > 0 && horizontalWalls[x - 1, y] != null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), false);
                    }
                }
            }
        }
    }
Esempio n. 3
0
    private void CheckToHaveNotSetted(CellMaster mast)
    {
        for (int i = 0; i < unit.Count - 1; i++)
        {
            Vector2Int pos = new Vector2Int((int)unit[i].transform.position.x, (int)unit[i].transform.position.y);

            if (CheckToNeighboor(pos, mast))
            {
                SetCell(pos, mast, false);
            }
        }
    }
Esempio n. 4
0
    public void SetCell(Vector2Int pos, CellMaster mast, bool isFirst)
    {
        int errorAmount = 0;

        Vector2Int startPos   = pos;
        Vector2Int prevousPos = pos;

        _cellGenerate.Cells[startPos.x, startPos.y].SelectColor(mast);
        if (mast == CellMaster.enemy)
        {
            _enemyCells.Add(_cellGenerate.Cells[startPos.x, startPos.y].gameObject);
        }

        startPos += _direction[Random.Range(0, _direction.Length)];
        if (isFirst)
        {
            _maxCells--;
        }
        System.Random rnd = new System.Random();

        for (int i = 0; i < _maxCells; i++)
        {
            for (int j = 0; j < _direction.Length; j++)
            {
                startPos += _direction[j];
                if (CheckToCanSelect(startPos, mast))
                {
                    _cellGenerate.Cells[startPos.x, startPos.y].SelectColor(mast);
                    DeleteNaghbourWall(startPos, mast);
                    _enemyScore++;
                    prevousPos = startPos;
                    if (mast == CellMaster.enemy)
                    {
                        _enemyCells.Add(_cellGenerate.Cells[startPos.x, startPos.y].gameObject);
                    }
                    break;
                }
                else
                {
                    startPos = prevousPos;
                }
            }
            if (errorAmount > 10000)
            {
                break;
            }
            errorAmount++;
        }
        if (mast == CellMaster.enemy)
        {
            Invoke(nameof(GameQueue), 0.3f);
        }
    }
Esempio n. 5
0
    public bool CheckToCanSelect(Vector2Int pos, CellMaster mast)
    {
        if (CheckToArea(pos))
        {
            if (_cellGenerate.Cells[pos.x, pos.y].CellMast == CellMaster.empty && CheckToNeighboor(pos, mast))
            {
                return(true);
            }
        }


        return(false);
    }
Esempio n. 6
0
 public bool CheckToNeighboor(Vector2Int pos, CellMaster mast)
 {
     for (int i = 0; i < _direction.Length; i++)
     {
         Vector2Int cPos = pos + _direction[i];
         if (CheckToArea(cPos))
         {
             if (_cellGenerate.Cells[cPos.x, cPos.y].CellMast == mast)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Esempio n. 7
0
    void Awake()
    {
        if (instance)
        {
            Destroy(this);
            return;
        }
        else
        {
            instance = this;

            cellsByRoom = new List <List <Cell> >();

            int numCells = Mathf.FloorToInt(areaSize / cellSize);
            instance._areaSize = numCells * cellSize;

            cells = new Cell[numCells, numCells];
            for (int x = 0; x < numCells; x++)
            {
                for (int y = 0; y < numCells; y++)
                {
                    Cell newCell = new Cell(x, y);
                    if (x == 0)
                    {
                        newCell.canGoWest = false;
                    }
                    if (x == numCells - 1)
                    {
                        newCell.canGoEast = false;
                    }
                    if (y == 0)
                    {
                        newCell.canGoSouth = false;
                    }
                    if (y == numCells - 1)
                    {
                        newCell.canGoNorth = false;
                    }

                    cells[x, y] = newCell;
                }
            }

            GenLayout();
        }
    }
Esempio n. 8
0
 public void DeleteNaghbourWall(Vector2Int pos, CellMaster mast)
 {
     for (int i = 0; i < _direction.Length; i++)
     {
         Vector2Int cPos = pos + _direction[i];
         if (CheckToArea(cPos))
         {
             if (_cellGenerate.Cells[cPos.x, cPos.y].CellMast == mast)
             {
                 _cellGenerate.Cells[pos.x, pos.y].SwitchWall(i, false);
                 _cellGenerate.Cells[cPos.x, cPos.y].SwitchWall(ReverseWall(i), false);
             }
             else
             {
                 _cellGenerate.Cells[cPos.x, cPos.y].SwitchWall(i, true);
             }
         }
     }
 }
Esempio n. 9
0
    public Cell GetCellInDirection(Direction dir)
    {
        switch (dir)
        {
        case Direction.North:
            if (y < CellMaster.CellsPerSide - 1)
            {
                return(CellMaster.GetCellAt(x, y + 1));
            }
            break;

        case Direction.South:
            if (y > 0)
            {
                return(CellMaster.GetCellAt(x, y - 1));
            }
            break;

        case Direction.East:
            if (x < CellMaster.CellsPerSide - 1)
            {
                return(CellMaster.GetCellAt(x + 1, y));
            }
            break;

        case Direction.West:
            if (x > 0)
            {
                return(CellMaster.GetCellAt(x - 1, y));
            }
            break;

        default:
            Debug.LogWarning("Invalid direction " + dir);
            break;
        }
        return(null);
    }
Esempio n. 10
0
 void Awake()
 {
     ms_instance = this;
     m_cells     = new Dictionary <string, Cell>();
 }
Esempio n. 11
0
 static bool ClearBetween(Vector3 fromPoint, Vector3 toPoint)
 {
     fromPoint.y = toPoint.y = 1f;
     return(!Physics.Linecast(fromPoint + Vector3.up, toPoint + Vector3.up, CellMaster.GetPathAffectingLayers()));
 }
Esempio n. 12
0
    public static Vector3[] PathTo(Vector3 fromPoint, Vector3 toPoint)
    {
        fromPoint.y = toPoint.y = 0;

        Cell startCell = CellMaster.GetCellAt(fromPoint);
        Cell endCell   = CellMaster.GetCellAt(toPoint);

        if (startCell.x < 0 || endCell.y < 0)
        {
            Debug.Log("Invalid path start and/or end");
            return(null);
        }

        // A* setup
        var evaluated  = new List <Cell>();
        var toEvaluate = new List <Cell>();
        var cameFrom   = new Dictionary <Cell, Cell>();

        var costTo   = new Dictionary <Cell, int>();
        var costFrom = new Dictionary <Cell, float>();

        var totalCost = new Dictionary <Cell, float>();

        toEvaluate.Add(startCell);
        costTo.Add(startCell, 0);
        costFrom.Add(startCell, HeuristicDist(startCell, endCell));
        totalCost.Add(startCell, costFrom[startCell]);

        // A* loop
        while (toEvaluate.Count > 0)
        {
            Cell  evalPoint  = null;
            float lowestCost = float.MaxValue;
            foreach (var cell in toEvaluate)    // perhaps ove toeval to a heap or something
            {
                float newCost = totalCost[cell];
                if (newCost < lowestCost)
                {
                    lowestCost = newCost;
                    evalPoint  = cell;
                }
            }

            // if reached dest
            if (evalPoint == endCell)
            {
                var cellPath = WalkPath(cameFrom, evalPoint);

                var path = new List <Vector3>();
                path.Add(fromPoint);

                for (int i = 1; i < cellPath.Count - 1; i++)
                {
                    path.Add(cellPath[i].centrePosition);
                }

                path.Add(toPoint);

                path = SmoothPath(path);
                return(path.ToArray());
            }

            toEvaluate.Remove(evalPoint);
            evaluated.Add(evalPoint);

            var neighbours = new List <Cell>();
            if (evalPoint.canGoNorth && evalPoint.GetCellInDirection(Direction.North).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.North));
            }
            if (evalPoint.canGoSouth && evalPoint.GetCellInDirection(Direction.South).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.South));
            }
            if (evalPoint.canGoEast && evalPoint.GetCellInDirection(Direction.East).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.East));
            }
            if (evalPoint.canGoWest && evalPoint.GetCellInDirection(Direction.West).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.West));
            }

            foreach (Cell newPoint in neighbours)
            {
                if (evaluated.Contains(newPoint))
                {
                    continue;
                }

                int tentCostTo = costTo[evalPoint] + 1;

                if (!toEvaluate.Contains(newPoint))
                {
                    toEvaluate.Add(newPoint);
                    costTo.Add(newPoint, tentCostTo);
                    costFrom.Add(newPoint, HeuristicDist(newPoint, endCell));
                    totalCost.Add(newPoint, tentCostTo + costFrom[newPoint]);
                    cameFrom.Add(newPoint, evalPoint);
                }
                else if (tentCostTo < costTo[newPoint])
                {
                    costTo[newPoint]    = tentCostTo;
                    cameFrom[newPoint]  = evalPoint;
                    totalCost[newPoint] = tentCostTo + costFrom[newPoint];
                }
            }
        }

        Debug.Log("No path found from " + fromPoint + " to " + toPoint);
        return(null);
    }
Esempio n. 13
0
 void OnDisable()
 {
     if ( instance == this )
         instance = null;
 }
Esempio n. 14
0
    void Awake()
    {
        if ( instance )
        {
            Destroy( this );
            return;
        }
        else
        {
            instance = this;

            cellsByRoom = new List<List<Cell>>();

            int numCells = Mathf.FloorToInt ( areaSize / cellSize );
            instance._areaSize = numCells * cellSize;

            cells = new Cell[ numCells, numCells ];
            for ( int x = 0; x < numCells; x++ )
            {
                for ( int y = 0; y < numCells; y++ )
                {
                    Cell newCell = new Cell( x, y );
                    if ( x == 0 )
                        newCell.canGoWest = false;
                    if ( x == numCells - 1)
                        newCell.canGoEast = false;
                    if ( y == 0 )
                        newCell.canGoSouth = false;
                    if ( y == numCells - 1 )
                        newCell.canGoNorth = false;

                    cells[x,y] = newCell;
                }
            }

            GenLayout ();
        }
    }