Beispiel #1
0
    public void FindPath(Tile target)
    {
        tacticsMove.ComputeWalkableAdjacencyLists(target);
        tacticsMove.GetCurrentTile();

        var openList   = new List <Tile>();
        var closedList = new List <Tile>();

        openList.Add(tacticsMove.CurrentTile);
        //currentTile.parent = ??
        tacticsMove.CurrentTile.H = Vector3.Distance(tacticsMove.CurrentTile.transform.position, target.transform.position);
        tacticsMove.CurrentTile.F = tacticsMove.CurrentTile.H;

        while (openList.Count > 0)
        {
            var t = FindLowestF(openList);

            closedList.Add(t);

            if (t == target)
            {
                ActualTargetTile = FindEndTile(t);
                tacticsMove.MoveToTile(ActualTargetTile);
                return;
            }

            foreach (var tile in t.AdjacencyList)
            {
                if (closedList.Contains(tile))
                {
                    //Do nothing, already processed
                }
                else if (openList.Contains(tile))
                {
                    var tempG = t.G + Vector3.Distance(tile.transform.position, t.transform.position);

                    if (tempG < tile.G)
                    {
                        tile.Parent = t;

                        tile.G = tempG;
                        tile.F = tile.G + tile.H;
                    }
                }
                else
                {
                    tile.Parent = t;

                    tile.G = t.G + Vector3.Distance(tile.transform.position, t.transform.position);
                    tile.H = Vector3.Distance(tile.transform.position, target.transform.position);
                    tile.F = tile.G + tile.H;

                    openList.Add(tile);
                }
            }
        }

        //todo - what do you do if there is no path to the target tile?
        Debug.Log("Path not found");
        //End turn for now
        turnManager.EndTurn();
    }