示例#1
0
    private int searchDirection(Cell celaInici, Cell.RoomType roomtype)
    {
        var rowCells = GridManager.Instance.GetRowCells(celaInici.Y);

        Debug.Log("Search direction. Row Count : " + rowCells.Count);
        var celesDreta = rowCells.FindAll(cell => cell.X > celaInici.X && cell.GetDirtLevel() > cell.GetTimesPassed() && cell.GetCellType() == Cell.CellType.Floor && cell.GetRoomType() == roomtype);

        Debug.Log("Search direction. Right Count : " + celesDreta.Count);
        var celesEsquerra = rowCells.FindAll(cell => cell.X <celaInici.X && cell.GetDirtLevel()> cell.GetTimesPassed() && cell.GetCellType() == Cell.CellType.Floor && cell.GetRoomType() == roomtype);

        Debug.Log("Search direction. Left Count : " + celesEsquerra.Count);

        if (celesEsquerra == null || celesEsquerra.Count == 0)
        {
            return(1);
        }
        if (celesDreta == null || celesDreta.Count == 0)
        {
            return(-1);
        }
        if (celesEsquerra.Count >= celesDreta.Count)
        {
            return(1);
        }
        else
        {
            return(-1);
        }
    }
示例#2
0
    private List <Cell> searchPath(Cell.RoomType roomtype)
    {
        var direction = searchDirection(currentCell, roomtype);

        Debug.Log("Direction : " + direction);
        var llista = new List <Cell>();

        if (currentCell.GetDirtLevel() > currentCell.GetTimesPassed())
        {
            llista.Add(currentCell);
            Debug.Log("Em quedo netejant la mateixa cela");
            return(llista);
        }
        var horizontalcell = GridManager.Instance.GetCell(currentCell.X + direction, currentCell.Y);

        if (horizontalcell != null && horizontalcell.GetCellType() == Cell.CellType.Floor && horizontalcell.GetRoomType() == roomtype && horizontalcell.GetDirtLevel() > horizontalcell.GetTimesPassed())
        {
            llista.Add(horizontalcell);
            Debug.Log("Move Horizontal: " + horizontalcell.ToString());
            return(llista);
        }

        var nextRowCell = GridManager.Instance.GetNextRowCell(currentCell.X, currentCell.Y, roomtype);

        if (nextRowCell != null)
        {
            llista = searchPathA(currentCell, nextRowCell);
            llista.RemoveAt(0);
            Debug.Log("Move Horizontal A*: " + nextRowCell.ToString());
            return(llista);
        }

        var verticalcell = GridManager.Instance.GetCell(currentCell.X, currentCell.Y + 1);

        if (verticalcell != null && verticalcell.GetCellType() == Cell.CellType.Floor && verticalcell.GetRoomType() == roomtype && verticalcell.GetDirtLevel() > verticalcell.GetTimesPassed())
        {
            llista.Add(verticalcell);
            return(llista);
        }

        var nextColumnCell = GridManager.Instance.GetNextRowCell(currentCell.X, currentCell.Y + 1, roomtype);

        if (nextColumnCell != null)
        {
            llista = searchPathA(currentCell, nextColumnCell);
            llista.RemoveAt(0);
            return(llista);
        }

        Debug.LogError("ERROR: Could not find next cell!!!");
        return(llista);
    }
示例#3
0
    public Cell StartCell(Cell.RoomType roomtype)
    {
        var  celesRoom = listCell.FindAll(cell => cell.GetRoomType() == roomtype && cell.GetCellType() == Cell.CellType.Floor);
        Cell startCell = celesRoom[0];

        for (int i = 1; i < celesRoom.Count; i++)
        {
            if (celesRoom[i].X <= startCell.X && celesRoom[i].Y <= startCell.Y)
            {
                startCell = celesRoom[i];
            }
        }
        return(startCell);
    }
示例#4
0
 public void CleanRoom(Cell.RoomType roomtype)
 {
     Debug.Log("---- START CLEANING ROOM ----");
     while (!GridManager.Instance.RoomCleaned(roomtype))
     {
         // Buscar camí
         if (battery <= LOW_BATTERY)
         {
             var nearestChargeCell = GridManager.Instance.NearestChargeCell(currentCell);
             var goChargeCell      = searchPathA(currentCell, nearestChargeCell);
             goChargeCell.RemoveAt(0);
             addCellsToPath(goChargeCell);
             Debug.Log("Battery after charge: " + battery);
             var comeBack = searchPathA(nearestChargeCell, currentCell);
             comeBack.RemoveAt(0);
             for (int i = comeBack.Count - 1; i >= 0; i--)
             {
                 if (comeBack[i].GetTimesPassed() < comeBack[i].GetDirtLevel() && GridManager.Instance.LowerCellsClean(comeBack[i].Y))
                 {
                     if (i < comeBack.Count - 1)
                     {
                         comeBack.RemoveRange(i + 1, comeBack.Count - i - 1);
                     }
                     break;
                 }
             }
             addCellsToPath(comeBack);
             currentCell = comeBack[comeBack.Count - 1];
         }
         else
         {
             var nextCells = searchPath(roomtype);
             addCellsToPath(nextCells);
             currentCell = nextCells[nextCells.Count - 1];
         }
     }
     if ((int)roomtype < (GridManager.Instance.GetRoomCount() - 1))
     {
         var nextRoomStartCell = GridManager.Instance.StartCell(roomtype + 1);
         Debug.Log("nextRoomStartCell: " + nextRoomStartCell);
         var nextCellsToNextRoom = searchPathA(currentCell, nextRoomStartCell);
         nextCellsToNextRoom.RemoveAt(0);
         addCellsToPath(nextCellsToNextRoom);
         currentCell = nextCellsToNextRoom[nextCellsToNextRoom.Count - 1];
         Debug.Log("CurrentCell:" + currentCell.ToString());
         Debug.Log("Move to next room: " + (roomtype + 1));
     }
 }
示例#5
0
    public Cell GetNextRowCell(int x, int y, Cell.RoomType roomtype)
    {
        var allRowCell = listCell.FindAll(cell => cell.Y == y && cell.GetDirtLevel() > cell.GetTimesPassed() && cell.GetCellType() == Cell.CellType.Floor && cell.GetRoomType() == roomtype);

        if (allRowCell != null && allRowCell.Count > 0)
        {
            var cellsCount = allRowCell.Count;
            if (Math.Abs(allRowCell[0].X - x) < Math.Abs(allRowCell[cellsCount - 1].X - x))
            {
                return(allRowCell[0]);
            }
            else
            {
                return(allRowCell[cellsCount - 1]);
            }
        }
        return(null);
    }
示例#6
0
 public bool RoomCleaned(Cell.RoomType roomtype)
 {
     return(listCell.Find(cell => cell.GetRoomType() == roomtype && cell.GetCellType() == Cell.CellType.Floor && cell.GetDirtLevel() > cell.GetTimesPassed()) == null);
 }