コード例 #1
0
 private void handlePlayerMovement(GridCell newCell)
 {
     if (newCell.Equals(currentPelletCell))
     {
         scoreController.onPlayerPickup();
         Destroy(currentPellet);
         spawnNewPellet();
     }
 }
コード例 #2
0
        public void Update(Grid grid)
        {
            if (_startRow >= grid.RowCount || _startColumn >= grid.ColumnCount)
            {
                throw new ArgumentException("The grid is too small to hold source cell index", nameof(grid));
            }

            GridCell sourceCell = grid[_startRow, _startColumn];

            sourceCell.Tags.Add(PriorityNodeKey, CreatePriorityNode(sourceCell, 0));

            foreach (GridCell cell in grid.GetCells().Where(cell => !sourceCell.Equals(cell)))
            {
                PriorityQueueNode <int, CellNodeValue> priorityQueueNode = CreatePriorityNode(cell, int.MaxValue);
                cell.Tags.Add(PriorityNodeKey, priorityQueueNode);
            }

            var priorityQueue = new PriorityQueue <CellNodeValue, int>(grid.Size);

            foreach (GridCell cell in grid.GetCells())
            {
                PriorityQueueNode <int, CellNodeValue> cellTag = GetPriorityNodeOnCell(cell);
                priorityQueue.Enqueue(cellTag, cellTag.Priority);
            }

            while (priorityQueue.Count > 0)
            {
                PriorityQueueNode <int, CellNodeValue> minDistanceNode = priorityQueue.Dequeue();
                GridCell minDistanceCell = minDistanceNode.Value.Current;
                foreach (GridCell neighbor in minDistanceCell.Links)
                {
                    int altDistance = minDistanceNode.Priority + 1;
                    PriorityQueueNode <int, CellNodeValue> neighborNode = GetPriorityNodeOnCell(neighbor);
                    if (altDistance < neighborNode.Priority)
                    {
                        neighborNode.Value.Previous = minDistanceCell;
                        priorityQueue.UpdatePriority(neighborNode, altDistance);
                    }
                }
            }

            foreach (GridCell cell in grid.GetCells())
            {
                PriorityQueueNode <int, CellNodeValue> node = GetPriorityNodeOnCell(cell);
                cell.Tags.Remove(PriorityNodeKey);
                cell.Tags[ResolvingDistance] = node.Priority;
            }
        }
コード例 #3
0
ファイル: Draggable.cs プロジェクト: andrewcylaw/cactus
        // Allows for dragging + showing the GridCell that this GameObject will snap to
        public void OnMouseDrag()
        {
            transform.position = GetMousePosition() + initClickPosition;

            List <Collider2D> overlap  = new List <Collider2D>();
            int      numOverlap        = mainCollider.OverlapCollider(noFilter.NoFilter(), overlap);
            GridCell newOverlappedCell = default;
            float    largestOverlapAmt = default; // "largest" = most negative number

            if (numOverlap > 0)
            {
                // Find the Collider2D with the most overlap
                for (int i = 0; i < numOverlap; i++)
                {
                    Collider2D curCollider = overlap[i];

                    if (!gridManager.IsGridCell(curCollider))
                    {
                        continue;
                    }

                    float overlapAmount = mainCollider.Distance(curCollider).distance;
                    if (overlappedCell == null || overlapAmount < largestOverlapAmt)
                    {
                        // Have to check if GridCell or not
                        newOverlappedCell = gridManager.GetGridCell(curCollider);
                        largestOverlapAmt = overlapAmount;
                    }
                }

                if (overlappedCell != null && !overlappedCell.Equals(newOverlappedCell))
                {
                    DeselectGridCell(overlappedCell);
                }

                if (newOverlappedCell != null)
                {
                    newOverlappedCell.tile.GetComponent <SpriteRenderer>().color = Color.grey;
                }

                overlappedCell = newOverlappedCell;
            }
        }