Beispiel #1
0
        void BuildPath(int clickedCellIndex)
        {
            DestroyLOSMarkers();

            if (isSelectingStart)
            {
                // Selects start cell
                cellStartIndex = clickedCellIndex;
                grid.CellToggle(cellStartIndex, true, Color.yellow);
            }
            else
            {
                // Clicked on the end cell, then show the path
                // First clear color of start cell
                grid.CellToggle(cellStartIndex, false, Color.white);
                // Get Path
                List <int> path = grid.FindPath(cellStartIndex, clickedCellIndex, cellGroupMask: CELLS_ALL_NAVIGATABLE);
                // Color the path
                if (path != null)
                {
                    for (int k = 0; k < path.Count; k++)
                    {
                        grid.CellFadeOut(path [k], Color.green, 1f);
                    }
                }
            }
            isSelectingStart = !isSelectingStart;
        }
        // Highlight cells sequentially on each frame
        IEnumerator HighlightCell()
        {
            currentCol++;
            // run across the grid row by row
            if (currentCol >= grid.columnCount)
            {
                currentCol = 0;
                currentRow++;
                if (currentRow >= grid.rowCount)
                {
                    currentRow = 0;
                }
            }
            // get cell at current grid position and color it with fade out option
            Cell cell = grid.CellGetAtPosition(currentCol, currentRow);

            if (cell != null)
            {
                int   cellIndex = grid.CellGetIndex(cell);
                float duration  = Random.value * 2.5f + 0.5f;
                Color color     = new Color(Random.value, Random.value, Random.value);
                grid.CellFadeOut(cellIndex, color, duration);
            }
            // trigger next iteration after this frame
            yield return(new WaitForEndOfFrame());

            StartCoroutine(HighlightCell());
        }
        void TriggerRandomCell(Color color)
        {
            // We get a random vector from -0.5..0.5 on both X and Y (z is ignored)
            Vector2 localPosition = Random.onUnitSphere * 0.5f;
            Cell    cell          = grid.CellGetAtPosition(localPosition);

            if (cell != null)
            {
                int   cellIndex = grid.CellGetIndex(cell);
                float duration  = Random.value * 2.5f + 0.5f;
                grid.CellFadeOut(cellIndex, color, duration);
            }
        }