Commit 7d1e5e5f authored by 15박보승's avatar 15박보승

Remove Contains function from A*. Optimization completed.

parent a0205e78
...@@ -261,13 +261,13 @@ MonoBehaviour: ...@@ -261,13 +261,13 @@ MonoBehaviour:
bounds: bounds:
m_Center: {x: 0, y: 0, z: 0} m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 10, y: 10, z: 0} m_Extent: {x: 10, y: 10, z: 0}
pointInterval: 0.768 pointInterval: 0.614
agentRadius: 0.1 agentRadius: 0.1
blockMask: blockMask:
serializedVersion: 2 serializedVersion: 2
m_Bits: 256 m_Bits: 256
isOptimizing: 1 isOptimizing: 1
complementLevel: 10 complementLevel: 8
enableGizmos: 1 enableGizmos: 1
--- !u!4 &178212235 --- !u!4 &178212235
Transform: Transform:
......
...@@ -32,6 +32,11 @@ public class PriorityQueue<T> ...@@ -32,6 +32,11 @@ public class PriorityQueue<T>
{ {
heap.HeapUpdated(item); heap.HeapUpdated(item);
} }
public IEnumerator<T> GetEnumerator()
{
return heap.GetEnumerator();
}
} }
class Heap<T> class Heap<T>
...@@ -219,4 +224,9 @@ class Heap<T> ...@@ -219,4 +224,9 @@ class Heap<T>
TryWithParent(Parent(index)); TryWithParent(Parent(index));
} }
} }
public IEnumerator<T> GetEnumerator()
{
return list.GetEnumerator();
}
} }
\ No newline at end of file
...@@ -425,6 +425,7 @@ namespace BS ...@@ -425,6 +425,7 @@ namespace BS
List<Node> closed = new List<Node>(); List<Node> closed = new List<Node>();
queue.Enqueue(start); queue.Enqueue(start);
start.state = 1;
while (queue.Count > 0) while (queue.Count > 0)
{ {
Node cur = queue.Dequeue(); Node cur = queue.Dequeue();
...@@ -436,6 +437,7 @@ namespace BS ...@@ -436,6 +437,7 @@ namespace BS
if(cur.gridPosition == goal.gridPosition) if(cur.gridPosition == goal.gridPosition)
{ {
closed.Add(cur); closed.Add(cur);
cur.state = -1;
break; break;
} }
...@@ -443,8 +445,8 @@ namespace BS ...@@ -443,8 +445,8 @@ namespace BS
foreach (var adj in cur.adjacencies) foreach (var adj in cur.adjacencies)
{ {
Node adjNode = nodes[adj]; Node adjNode = nodes[adj];
bool isOpened = queue.Contains(nodes[adj]); bool isOpened = nodes[adj].state > 0;
bool isClosed = closed.Contains(nodes[adj]); bool isClosed = nodes[adj].state < 0;
if (isOpened) if (isOpened)
{ {
...@@ -462,10 +464,11 @@ namespace BS ...@@ -462,10 +464,11 @@ namespace BS
adjNode.cost = cur.cost + Vector2.Distance(cur.worldPositon, adjNode.worldPositon); adjNode.cost = cur.cost + Vector2.Distance(cur.worldPositon, adjNode.worldPositon);
adjNode.CalculateScore(to); adjNode.CalculateScore(to);
queue.Enqueue(adjNode); queue.Enqueue(adjNode);
adjNode.state = 1;
} }
} }
cur.state = -1;
closed.Add(cur); closed.Add(cur);
} }
...@@ -492,14 +495,16 @@ namespace BS ...@@ -492,14 +495,16 @@ namespace BS
{ {
path.Add(to); path.Add(to);
} }
/*
foreach(var node in nodes.Values) foreach(var close in closed)
{ {
node.cost = 0; close.state = 0;
node.parent = null;
node.score = 0;
} }
*/ foreach(var open in queue)
{
open.state = 0;
}
return path; return path;
} }
...@@ -632,15 +637,7 @@ namespace BS ...@@ -632,15 +637,7 @@ namespace BS
cost = 0; cost = 0;
score = 0; score = 0;
parent = null; parent = null;
} state = 0;
public Node(Node node, Node parent, float cost = 0)
{
this.gridPosition = node.gridPosition;
this.worldPositon = node.worldPositon;
this.adjacencies = node.adjacencies;
this.cost = cost;
this.parent = parent;
score = 0;
} }
public Vector2Int gridPosition; public Vector2Int gridPosition;
...@@ -649,7 +646,7 @@ namespace BS ...@@ -649,7 +646,7 @@ namespace BS
public float cost; public float cost;
public float score; public float score;
public Node parent; public Node parent;
public int state; // -1 : closed, 1 : opened, 0 : neutral
public void CalculateScore(Vector3 destination) public void CalculateScore(Vector3 destination)
{ {
score = cost + GetHeuristic(destination); score = cost + GetHeuristic(destination);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment