Exemplo n.º 1
0
        private void InitializeGame()
        {
            //Cache grid data.
            gridInfo = GridGenerator.Instance.GridInfo;

            itemCells.Clear();
            foreach (KeyValuePair <Vector2, Cell> kvp in gridInfo)
            {
                //Reset cells.
                kvp.Value.cellMonobehaviour.ResetData();

                //CacheItemCells();
                if (kvp.Value.cellMonobehaviour.ContainsItem)
                {
                    itemCells.Add(kvp.Value.cellMonobehaviour);
                }
            }

            //Make cell(0,0) as selected on start.
            currentCell = gridInfo[new Vector2(0, 0)];
            currentCell.cellMonobehaviour.Select();
            lastClickedCell = currentCell.cellMonobehaviour;
            itemCellsFound  = 0;
            moves           = Constants.MAX_MOVES;
            UIManager.Instance.UpdateMoves(moves);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if a cell contains an item, otherwise finds closest item cell.
        /// </summary>
        /// <param name="position">Position of current cell</param>
        /// <returns>Distance(Cell count) to closest item cell</returns>
        public int DistanceToNearestItemCell(CellMono cell)
        {
            //If current cell contains an item return.
            if (cell.ContainsItem)
            {
                return(0);
            }
            else
            {
                Vector2 pos = cell.gridIndex;
                Vector2 closestItemCellIndex = itemCells[0].gridIndex;
                //Assuming the minimum distance is between pos and first cell in itemCells.
                int minDistance = (int)Mathf.Max(Mathf.Abs(pos.x - itemCells[0].gridIndex.x), Mathf.Abs(pos.y - itemCells[0].gridIndex.y));
                int distance    = 0;

                for (int i = 1; i < itemCells.Count; i++)
                {
                    distance = (int)Mathf.Max(Mathf.Abs(pos.x - itemCells[i].gridIndex.x), Mathf.Abs(pos.y - itemCells[i].gridIndex.y));

                    if (distance < minDistance)
                    {
                        minDistance          = distance;
                        closestItemCellIndex = itemCells[i].gridIndex;
                    }
                }

                return(minDistance);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Called when user clicks on a cell.
        /// </summary>
        /// <param name="cell"></param>
        public void OnClickedCell(CellMono cell)
        {
            if (lastClickedCell.Equals(cell))
            {
                return;
            }

            //Check if moves are over.
            if (moves <= 0)
            {
                UIManager.Instance.ShowPopup(Constants.MOVES_OVER);
                return;
            }

            //Update moves
            moves--;
            UIManager.Instance.UpdateMoves(moves);

            ExamineCell(cell);

            //Check if all item cells found.
            if (itemCellsFound == Constants.MAX_ITEMS_CELLS)
            {
                UIManager.Instance.ShowPopup(Constants.WIN_MESSAGE);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Examines a cell and its neighbours for item cells and sets appropriate color.
        /// </summary>
        /// <param name="cell">Cell to be examined.</param>
        private void ExamineCell(CellMono cell)
        {
            Vector2 source   = cell.gridIndex;
            int     distance = DistanceToNearestItemCell(cell);

            if (distance == 0)
            {
                cell.onProcessed.Invoke(ItemColor.Red);
                itemCellsFound++;
            }
            else if (distance <= 2)
            {
                cell.onProcessed.Invoke(ItemColor.Yellow);
            }
            else
            {
                cell.onProcessed.Invoke(ItemColor.Green);
            }

            lastClickedCell = cell;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initialzes grid and computes links between cells.
        /// </summary>
        private void InitializeGridInfo()
        {
            int cellIndex = 0;

            for (int i = 0; i < rows; i++)        //Rows
            {
                for (int j = 0; j < columns; j++) //Columns
                {
                    Vector2 index = new Vector2(i, j);

                    CellMono cellMono = cells[cellIndex];

                    //Create cell object
                    Cell cell = new Cell(index, cellMono);
                    gridInfo.Add(index, cell);
                    cellIndex++;

                    //Making all the cells as unvisited by default.
                    unVisited.Add(cell);
                }
            }

            ComputeLinks();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generates a grid of specified size(rows and columns).
        /// </summary>
        private void GenerateGrid()
        {
            Vector2 currentSpawnPosition = initialPosition;

            for (int i = 0; i < rows; i++)        //Rows
            {
                for (int j = 0; j < columns; j++) //Columns
                {
                    Vector2    index = new Vector2(i, j);
                    GameObject obj   = Instantiate(cellPrefab, currentSpawnPosition, cellPrefab.transform.rotation, gridParent);
                    obj.name = "Cell(" + i + "," + j + ")";

                    CellMono cellM = obj.GetComponent <CellMono>();
                    cellM.Init(index, "(" + i + ", " + j + ")");

                    Cell cell = new Cell(index, cellM);

                    gridInfo.Add(index, cell);

                    //Making all the cells as unvisited by default.
                    unVisited.Add(cell);

                    //Set y as per cell size.
                    currentSpawnPosition.y += cellSize;
                }

                //Reset y for next column
                currentSpawnPosition.y = initialPosition.y;

                currentSpawnPosition.x += cellSize;
            }

            ComputeLinks();

            //DisplayNeighbourInfo();
        }
Exemplo n.º 7
0
 /// <summary>
 /// Constructor to initialize cell object.
 /// </summary>
 /// <param name="pos">Index of the cell in grid</param>
 /// <param name="cellM">Reference to CellMono script</param>
 public Cell(Vector2 pos, CellMono cellM)
 {
     this.position          = pos;
     this.cellMonobehaviour = cellM;
 }