예제 #1
0
 public void PutEquipment(EquipmentVisual e)
 {
     if (IsActive)
     {
         Cell closestCell = GetClosestCell(Input.mousePosition);
         ManageStatesAfterPut(e, closestCell);
     }
 }
예제 #2
0
 public void ManageDrag(EquipmentVisual e)
 {
     if (IsActive)
     {
         Cell        closestCell  = GetClosestCell(Input.mousePosition);
         List <Cell> hoveredCells = FindCellsWithPatternAndCenter(e, closestCell.x, closestCell.y);
         ValidateEquipmentPosition(e, hoveredCells);
     }
 }
예제 #3
0
    private void ManageStatesAfterPut(EquipmentVisual e, Cell cell)
    {
        if (CanPutEquipmentOnCell(e, cell))
        {
            int x       = cell.x;
            int y       = cell.y;
            int cols    = e.Cols;
            int rows    = e.Rows;
            int offsetX = cols / 2;
            int offsetY = rows / 2;
            for (int i = 0; i < cols; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    // The grid may not contain the current index
                    // It happens when we put ghost rows/cols to have centered equipments
                    if (GridContains(x + i - offsetX, y + j - offsetY))
                    {
                        Cell gridCell = gameMatrix[x + i - offsetX, y + j - offsetY];
                        // The cell can be null if it's an empty cell
                        if (e.Layout.GetCell(i, j) != null)
                        {
                            Cell newCell = Instantiate(e.Layout.GetCell(i, j), gridCell.transform.position, Quaternion.identity, transform);
                            gameMatrix[x + i - offsetX, y + j - offsetY] = newCell;
                            newCell.x = gridCell.x;
                            newCell.y = gridCell.y;
                            newCell.SetImage(e.Layout.GetSprite(i, j));
                            Destroy(gridCell.gameObject);

                            newCell.tempState = TempCellState.NAN;
                            CellState equipCellState = CellState.Empty;
                            equipCellState = e.GetLayoutState(i, j);

                            if (x + i <= columnCount && x + i >= 0 && y + j <= rowCount && y + j >= 0 &&
                                equipCellState != CellState.Empty)
                            {
                                if (equipCellState == CellState.Used)
                                {
                                    newCell.state = CellState.Used;
                                }
                                if (equipCellState == CellState.Open &&
                                    (gridCell.state == CellState.Inactive || gridCell.state == CellState.Open))
                                {
                                    newCell.state = CellState.Open;
                                }
                            }
                        }
                    }
                }
            }
        }
        ResetTempStates();
    }
예제 #4
0
    public bool ValidateEquipmentPosition(EquipmentVisual e, List <Cell> gridCells)
    {
        List <CellState> stateList = new List <CellState>();

        foreach (CellState c in e.GetAllStates())
        {
            stateList.Add(c);
        }
        int expectedCellsCount = stateList.Count(c => c != CellState.Inactive);

        return(gridCells.Count == expectedCellsCount);
    }
예제 #5
0
 // WARNING: this method also changes the TempCellState of the cells
 // TODO: avoid this side effect
 private bool CanPutEquipmentOnCell(EquipmentVisual e, Cell c)
 {
     if (IsActive)
     {
         List <Cell> gridCells = FindCellsWithPatternAndCenter(e, c.x, c.y);
         bool        canPut    = e.GetAllStates().Count(st => st != CellState.Empty) == gridCells.Count;
         foreach (Cell currCell in gridCells)
         {
             canPut = canPut && currCell.tempState == TempCellState.OK;
         }
         return(canPut);
     }
     return(false);
 }
예제 #6
0
    public List <Cell> FindCellsWithPatternAndCenter(EquipmentVisual e, int x, int y)
    {
        ResetTempStates();
        List <Cell> subset  = new List <Cell>();
        int         cols    = e.Cols;
        int         rows    = e.Rows;
        int         offsetX = cols / 2;
        int         offsetY = rows / 2;

        for (int i = 0; i < e.Cols; i++)
        {
            for (int j = 0; j < e.Rows; j++)
            {
                int idx = x + i - offsetX;
                int idy = y + j - offsetY;
                if (GridContains(idx, idy))
                {
                    Cell      gridCell       = gameMatrix[idx, idy];
                    CellState equipCellState = e.GetLayoutState(i, j);

                    if (idx <= columnCount && idx >= 0 && idy <= rowCount && idy >= 0 &&
                        equipCellState != CellState.Empty)
                    {
                        subset.Add(gridCell);
                        if (equipCellState == CellState.Used)
                        {
                            gridCell.tempState = gridCell.state == CellState.Open ? TempCellState.OK : TempCellState.NOK;
                        }
                        if (equipCellState == CellState.Open)
                        {
                            if (gridCell.state == CellState.Open || gridCell.state == CellState.Inactive)
                            {
                                gridCell.tempState = TempCellState.OK;
                            }
                            else
                            {
                                gridCell.tempState = TempCellState.NOK;
                            }
                        }
                    }
                }
            }
        }
        return(subset);
    }