コード例 #1
0
    public void UpdateBuildableCells(HexCell startCell, int maxDistance, bool buildmode)
    {
        List <HexCell> cellToFind = new List <HexCell>();

        HideIndicator();
        if (!buildmode)
        {
            for (int i = 0; i < cells.Length; i++)
            {
                cells[i].buildable = false;
            }
            return;
        }
        for (int i = 0; i < cells.Length; i++)
        {
            if (startCell.DistanceTo(cells[i]) <= maxDistance && cells[i].CanbeCellConstructTarget())
            {
                cells[i].buildable = true;
                cells[i].indicator.gameObject.SetActive(true);
                cells[i].indicator.SetColor(Indicator.BuildColor);
            }
            else
            {
                cells[i].buildable = false;
            }
        }
    }
コード例 #2
0
    public List <HexCell> GetTeleporterBuildableCells(HexCell startCell, int maxDistance)
    {
        List <HexCell> cellToFind = new List <HexCell>();

        //HideIndicator();
        for (int i = 0; i < cells.Length; i++)
        {
            if (startCell.DistanceTo(cells[i]) <= maxDistance && cells[i].hexType == HexType.Plain && cells[i].pawn == null && cells[i].building == null)
            {
                cellToFind.Add(cells[i]);
            }
        }
        return(cellToFind);
    }
コード例 #3
0
    public void RevealCellsFrom(HexCell cell)
    {
        Queue <HexCell> revealCells = new Queue <HexCell>();

        revealCells.Enqueue(cell);

        while (revealCells.Count > 0)
        {
            HexCell cellToReveal = revealCells.Dequeue();
            RevealCell(cellToReveal);
            hiddenCells.Remove(cellToReveal);
            for (HexDirection dir = (HexDirection)0; dir <= HexDirection.NW; dir++)
            {
                HexCell nextCell = cellToReveal.GetNeighbour(dir);
                if (nextCell != null && nextCell.DistanceTo(cell) <= revealRadius)
                {
                    if (hiddenCells.Contains(nextCell))
                    {
                        revealCells.Enqueue(nextCell);
                    }
                }
            }
        }
    }
コード例 #4
0
    public void FindPath(HexCell fromCell, HexCell toCell)
    {
        if (fromCell == toCell || !toCell.CanbeDestination())
        {
            return;
        }

        List <HexCell> cellToFind = new List <HexCell>();

        currentRoutes.Clear();

        // Set distance to max value
        for (int i = 0; i < cells.Length; i++)
        {
            cells[i].Distance = int.MaxValue;
            cells[i].prevCell = null;
        }

        fromCell.Distance = 0;
        cellToFind.Add(fromCell);
        while (cellToFind.Count > 0)
        {
            HexCell cell = cellToFind[0];
            cellToFind.RemoveAt(0);
            if (cell == toCell)
            {
                break;
            }

            for (HexDirection dir = (HexDirection)0; dir <= (HexDirection)5; dir++)
            {
                HexCell nextCell = cell.GetNeighbour(dir);
                if (nextCell != null)
                {
                    int distance = cell.Distance;
                    if (!nextCell.CanbeDestination())
                    {
                        continue;
                    }
                    else if (nextCell.hexType == HexType.Plain)
                    {
                        distance = cell.Distance + 1;
                    }
                    else if (nextCell.hexType == HexType.Forest || nextCell.hexType == HexType.Swamp)
                    {
                        distance = cell.Distance + 2;
                    }


                    nextCell.heuristicDistance = nextCell.DistanceTo(toCell);
                    if (nextCell.Distance == int.MaxValue)
                    {
                        distance         += nextCell.heuristicDistance;
                        nextCell.Distance = distance;
                        cellToFind.Add(nextCell);
                        nextCell.prevCell = cell;
                    }
                    else if (nextCell.Distance > distance + nextCell.heuristicDistance)
                    {
                        nextCell.Distance = distance + nextCell.heuristicDistance;
                        nextCell.prevCell = cell;
                    }
                }
            }
            cellToFind.Sort((x, y) => x.Distance.CompareTo(y.Distance));
        }
        pathLength = 0;
        currentRoutes.Clear();
        if (toCell.prevCell != null)
        {
            HexCell prev = toCell;
            while (prev != fromCell)
            {
                currentRoutes.Insert(0, prev);
                pathLength += (prev.hexType == HexType.Plain)?1:2;
                prev        = prev.prevCell;
            }
        }
    }