1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
 

public class AStar
{
    public class ANode : IComparable
    {
        public Vector3 Location { get; private set; }
        public int CostFromStart { get; private set; }
        public float CostToGoal { get; private set; }
        public ANode Parent { get; private set; }
 
        public ANode(Vector3 location)
        {
            this.Location = location;
        }
 
        public bool LocationEqual(ANode other)
        {
            if (other.Location == this.Location)
            {
                return true;
            }
            return false;
        }
        public int CompareTo(object other)
        {
            if ((other is ANode) == falsereturn 0;
            return CostFromStart.CompareTo((other as ANode).CostFromStart);
        }
 
        public void SetFromCost(int cost)
        {
            CostFromStart = cost;
        }
 
        public void SetToCost(float cost)
        {
            CostToGoal = cost;
        }
 
        public void SetParent(ANode node)
        {
            Parent = node;
        }
    }
 
    PriorityQueue<ANode> openList;
    List<ANode> closeList;
    ANode startNode;
    ANode goalNode;
 
    public AStar(Vector3 location, Vector3 destination)
    {
        openList = new PriorityQueue<ANode>();
        closeList = new List<ANode>();
        goalNode = new ANode(destination);
        startNode = new ANode(location);
        startNode.SetFromCost(0);
        startNode.SetToCost(PathCostEstimate(startNode));
        openList.Add(startNode);
        AddProximityNode(startNode);
 
        Clac();
    }
 
    private float PathCostEstimate(ANode node)
    {
        return Mathf.Pow(goalNode.Location.x - node.Location.x, 2+
               Mathf.Pow(goalNode.Location.y - node.Location.y, 2);
    }
 
    public bool Clac()
    {
        while (openList.Count != 0 && openList.Count < 20000)
        {
            ANode node = openList.Pop();
            Debug.Log("Pos:" + node.Location + " FromCost:" + node.CostFromStart +
                      " ToCost:" + node.CostToGoal);
            if (goalNode.LocationEqual(node))
                return true;
            else
            {
                AddProximityNode(node);
            }
            closeList.Add(node);
 
        }
        Debug.Log(openList.Count);
        return false;
    }
 
    public void AddProximityNode(ANode center)
    {
        int tempX;
        int tempY;
        for (int x = -1; x < 2; x++)
        {
            if (x != 0)
            {
                tempX = (int)center.Location.x + x;
                tempY = (int)center.Location.y;
                if (tempX >= 0)
                {
                    if (TileLayer.instance.GetWalkable(tempX, tempY))
                    {
                        ANode node = new ANode(new Vector3(tempX, tempY));
                        node.SetFromCost(center.CostFromStart + 1);
                        node.SetToCost(PathCostEstimate(node));
                        node.SetParent(center);
                        openList.Add(node);
                    }
                }
            }
        }
        for (int y = -1; y < 2; y++)
        {
            if (y != 0)
            {
                tempX = (int)center.Location.x;
                tempY = (int)center.Location.y + y;
                if (tempY >= 0)
                {
                    if (TileLayer.instance.GetWalkable(tempX, tempY))
                    {
                        ANode node = new ANode(new Vector3(tempX, tempY));
                        node.SetFromCost(center.CostFromStart + 1);
                        node.SetToCost(PathCostEstimate(node));
                        node.SetParent(center);
                        openList.Add(node);
                    }
                }
            }
        }
    }
 
}
 
 
cs


아직 다만든건 아니지만 테스트하면서 로그찍어보고 멘붕


viewimage.php?id=2abcdd23dad63db0&no=29bcc427b38477a16fb3dab004c86b6fd0548bb7fdb3d15f44497483ceabfeab9ae5a257fc5ccdf63d44ea1ab638a57d9fdf8aeb7349a6e88f


2만번 루프 결과


시작은 0. 0 목적지 0. 10


알고리즘 이해 자체를 잘못한거 같아서 다시 글들 읽어봐야겟음..