Beispiel #1
0
    // анимация генерации алгоритма Growing tree
    //IEnumerator Anim3(List<DelCell> walls)
    //{
    //    //yield return new WaitForSeconds(2);
    //    var waitsec = new WaitForSeconds(SpeedAnimation);
    //    for (int i = 0; i < walls.Count; i++)
    //    {
    //        var w1 = walls[i].p;

    //        if (walls[i].type == 0)
    //        {
    //            tileMap[w1.i, w1.j].SetColor(Color.red);
    //            if (SpeedAnimation != 0)
    //                yield return waitsec;
    //        }
    //        else if (walls[i].type == 1)
    //        {
    //            tileMap[w1.i, w1.j].SetColor(Color.white);

    //            Point[] arr = new Point[] {
    //                new Point(w1.i, w1.j+1),
    //                new Point(w1.i, w1.j-1),
    //                new Point(w1.i+1, w1.j),
    //                new Point(w1.i-1, w1.j)};

    //            foreach (var a in arr)
    //            {
    //                if (a.i > 0 && a.j > 0 && a.i < height - 1 && a.j < width - 1)
    //                {
    //                    if (tileMap[a.i, a.j].type == 1)
    //                    {
    //                        tileMap[a.i, a.j].SetColor(Color.white);
    //                    }
    //                }
    //            }

    //            if (SpeedAnimation != 0)
    //                yield return waitsec;
    //        }
    //        else if (walls[i].type == 2)
    //        {
    //            tileMap[w1.i, w1.j].SetType(1);
    //            tileMap[w1.i, w1.j].SetColor(Color.red);

    //            if (SpeedAnimation != 0)
    //                yield return waitsec;
    //        }
    //    }
    //    coroutine = null;
    //    yield return waitsec;
    //}

    void AnimPathImmd(CollectPath cpath)
    {
        var listUsed = new List <NodeAS>();

        for (int i = 0; i < cpath.workList.Count; i++)
        {
            var tt = cpath.workList[i];
            tileMap[tt.i, tt.j].SetColor(Color.yellow);
            listUsed.Add(new NodeAS(tt.i, tt.j, true));

            NodeAS[] temp = new NodeAS[] {
                GetNodeAS(tt.i - 1, tt.j),
                GetNodeAS(tt.i + 1, tt.j),
                GetNodeAS(tt.i, tt.j - 1),
                GetNodeAS(tt.i, tt.j + 1)
            };

            foreach (var retVal in temp)
            {
                if (retVal != null && retVal.isWalkable && !listUsed.Contains(retVal))
                {
                    tileMap[retVal.i, retVal.j].SetColor(Color.cyan);
                }
            }
        }

        foreach (var tile in cpath.pathList)
        {
            tileMap[tile.i, tile.j].SetColor(Color.red);
        }
    }
Beispiel #2
0
    public NodeAS GetCopy()
    {
        NodeAS node = new NodeAS(j, i, isWalkable);

        node.gScore = gScore;
        node.hScore = hScore;
        node.parent = parent;

        return(node);
    }
Beispiel #3
0
    public override bool Equals(object obj)
    {
        NodeAS node = obj as NodeAS;

        if (node.i == i && node.j == j)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Beispiel #4
0
    // анимация поиска пути всех 3 алгоритмов
    //IEnumerator AnimPath(CollectPath cpath)
    //{
    //    var waitsec = new WaitForSeconds(SpeedAnimation);
    //    var listUsed = new List<NodeAS>();
    //    for (int i = 0; i < cpath.workList.Count; i++)
    //    {
    //        var tt = cpath.workList[i];
    //        tileMap[tt.i, tt.j].SetColor(Color.yellow);
    //        listUsed.Add(new NodeAS(tt.i, tt.j, true));

    //        NodeAS[] temp = new NodeAS[] {
    //            GetNodeAS(tt.i - 1, tt.j),
    //            GetNodeAS(tt.i + 1, tt.j),
    //            GetNodeAS(tt.i, tt.j - 1),
    //            GetNodeAS(tt.i, tt.j + 1)};

    //        foreach (var retVal in temp)
    //        {
    //            if (retVal != null && retVal.isWalkable && !listUsed.Contains(retVal))
    //                tileMap[retVal.i, retVal.j].SetColor(Color.cyan);
    //        }
    //        if (SpeedAnimation != 0)
    //            yield return waitsec;
    //    }

    //    foreach (var tile in cpath.pathList)
    //    {
    //        tileMap[tile.i, tile.j].SetColor(Color.red);
    //        yield return new WaitForSeconds(0.0001f);
    //    }

    //    coroutine = null;
    //}


    public static NodeAS GetNodeAS(int i, int j)
    {
        if (i >= 0 && j >= 0)
        {
            if (j < width && i < height)
            {
                var type = tileMap[i, j].type;
                var node = new NodeAS(i, j, type == 0 ? false : true);
                return(node);
            }
        }
        return(null);
    }
Beispiel #5
0
    // функция проходит от конечной точки до старта и запоминает этот путь
    private void RetracePath(NodeAS startNodeAS, NodeAS endNodeAS)
    {
        NodeAS currentNodeAS = endNodeAS;

        while (true)
        {
            pathList.Add(currentNodeAS);
            if (currentNodeAS == startNodeAS)
            {
                break;
            }
            currentNodeAS = currentNodeAS.parent;
        }

        pathList.Reverse();
    }
Beispiel #6
0
    private List <NodeAS> GetNeighbours(NodeAS NodeAS)
    {
        List <NodeAS> retList = new List <NodeAS>();

        NodeAS[] temp = new NodeAS[] {
            matrix[NodeAS.i - 1, NodeAS.j],
            matrix[NodeAS.i + 1, NodeAS.j],
            matrix[NodeAS.i, NodeAS.j - 1],
            matrix[NodeAS.i, NodeAS.j + 1]
        };

        foreach (var retVal in temp)
        {
            if (retVal != null && retVal.isWalkable && retVal.isOpen)
            {
                retList.Add(retVal);
            }
        }

        return(retList);
    }
Beispiel #7
0
    // конструктор класса и здесь выполняется алгоритм
    public PathFinderAStar(NodeAS start, NodeAS goal)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

        height = TileSetManager.height;
        width  = TileSetManager.width;
        matrix = new NodeAS[height, width];

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                matrix[i, j]        = TileSetManager.GetNodeAS(i, j);
                matrix[i, j].isOpen = true;
            }
        }

        start = matrix[start.i, start.j];
        goal  = matrix[goal.i, goal.j];

        pathList = new List <NodeAS>();
        workList = new List <NodeAS>();
        openSet  = new List <NodeAS>();

        openSet.Add(start);
        start.isOpen = false;
        start.gScore = 0;
        start.hScore = GetDistance(start, goal);

        while (openSet.Count > 0)
        {
            int index = GetMinFNodeAS();
            if (index < 0)
            {
                break;
            }
            var current = openSet[index];
            workList.Add(current);
            openSet.RemoveAt(index);

            if (current.Equals(goal))
            {
                RetracePath(start, current);
                break;
            }

            foreach (var neighbor in GetNeighbours(current))
            {
                float newGScore = current.gScore + GetDistance(current, neighbor);

                neighbor.gScore = newGScore;
                neighbor.hScore = GetDistance(neighbor, goal);
                neighbor.fScore = newGScore + neighbor.hScore;
                neighbor.parent = current;
                neighbor.isOpen = false;
                openSet.Add(neighbor);
            }
        }

        sw.Stop();
        time = (sw.ElapsedTicks / (float)Stopwatch.Frequency) * 1000.0f;
    }
Beispiel #8
0
 private float GetDistance(NodeAS posA, NodeAS posB)
 {
     return(Vector2.Distance(posA.GetVector2(), posB.GetVector2()));
 }