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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
| using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using TMPro; using UnityEngine; using UnityEngine.PlayerLoop; public enum nodeType { Walkable, Obstacle };
public class Node { public nodeType nodetype; public Vector2 pos; public int x, y; public Node parent; public int G, H; public int F { get { return G + H; } }
public Node(nodeType nodetype, Vector2 pos, int x, int y) { this.nodetype = nodetype; this.pos = pos; this.x = x; this.y = y; } public Node() { } } public class ASTAR : MonoBehaviour { public Vector2 origin; public bool isMoving = true; public float speed = 0.1f; public float stopDistance = 0.1f; public float updateTime = 1f; public Vector2 target;
public List<Node> path = new List<Node>(); List<Node> openList = new List<Node>(); List<Node> closeList = new List<Node>(); public Vector2 mapCenter; public float height, width; public LayerMask WhatLayer; [Range(0.002f, 1f)] public float NodeRadius = 0.2f; public Node[,] nodes; public bool arrive; public int H_Manhattan(Node node, Node end) { return 10 * ((int)(Math.Abs(node.x - end.x)) + (int)(Math.Abs(node.y - end.y))); }
public void InitMap() { nodes = new Node[(int)(width / NodeRadius / 2), (int)(height / NodeRadius / 2)]; for (int i = 0; i < (int)(width / NodeRadius / 2); i++) { for (int j = 0; j < (int)(height / NodeRadius / 2); j++) { Vector2 center = new Vector2(mapCenter.x + NodeRadius + 2 * NodeRadius * (i - width / NodeRadius / 4), mapCenter.y - NodeRadius + 2 * NodeRadius * (height / NodeRadius / 4 - j)); bool isWall = Physics2D.OverlapCircle(center, NodeRadius - 0.001f, WhatLayer); nodes[i, j] = new Node( isWall ? nodeType.Obstacle : nodeType.Walkable, center, i, j ); } }
} public void FindPath(Vector2 startPos, Vector2 endPos) { if (startPos.x < mapCenter.x - width / 2 || startPos.x > mapCenter.x + width / 2 || startPos.y > mapCenter.y + height / 2 || startPos.y < mapCenter.y - height / 2 || endPos.x < mapCenter.x - width / 2 || endPos.x > mapCenter.x + width / 2 || endPos.y > mapCenter.y + height / 2 || endPos.y < mapCenter.y - height / 2) { path.Clear(); return; } Node start = getNode(startPos); Node end = getNode(endPos); if (end.nodetype == nodeType.Obstacle) { path.Clear(); return; } closeList.Clear(); openList.Clear();
start.parent = null; start.G = 0; start.H = H_Manhattan(start, end); openList.Add(start); for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; int g = (i * j) != 0 ? 14 : 10; FindNearNode(start.x + i, start.y + j, g, start, end); } } while (openList.Count > 0) { Node curnode = openList[0]; for (int i = 0; i < openList.Count; i++) { if (openList[i].F <= curnode.F && openList[i].H <= curnode.H) curnode = openList[i]; } openList.Remove(curnode); closeList.Add(curnode);
if (curnode == end) { generatePath(start, end); return; } for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; int g = (i * j) != 0 ? 14 : 10; FindNearNode(curnode.x + i, curnode.y + j, g, curnode, end); } } } generatePath(start, end); } private void generatePath(Node start, Node end) { path.Clear(); if (end != null) { Node temp = end; while (temp != start) { path.Add(temp); temp = temp.parent; } path.Reverse(); } }
public Node getNode(Vector2 pos) { Vector2 t; t.x = (pos.x - mapCenter.x - NodeRadius) / (2 * NodeRadius) + width / NodeRadius / 4; t.y = -((pos.y - mapCenter.y + NodeRadius) / (2 * NodeRadius) - height / NodeRadius / 4);
return nodes[(int)t.x, (int)t.y]; } private void FindNearNode(int x, int y, int g, Node parent, Node end) { if (x < 0 || x >= (int)(width / NodeRadius / 2) || y < 0 || y >= (int)(height / NodeRadius / 2)) return; Node node = nodes[x, y]; if (node == null || node.nodetype == nodeType.Obstacle || closeList.Contains(node) || openList.Contains(node)) return; node.parent = parent; node.G = parent.G + g; node.H = H_Manhattan(node, end); openList.Add(node); } IEnumerator Move() { int count = 0; while (count < path.Count) { while (Vector2.Distance(path[count].pos, transform.position) > stopDistance) { transform.position = Vector3.Lerp(transform.position, path[count].pos, speed); Vector3 temp = transform.localScale; if (transform.position.x > path[count].pos.x) { temp.x = -Math.Abs(temp.x); } else { temp.x = Math.Abs(temp.x); } transform.localScale = temp; yield return null; } count++; } arrive = true; } public void startMove(Vector2 target) { if (path.Count == 0) { InitMap(); } StopCoroutine("Move"); isMoving = true; arrive = false; FindPath(transform.position, target); if (path.Count == 0) { stopMove(); } else { this.target = target; StartCoroutine("Move"); } } public void stopMove() { StopCoroutine("Move"); isMoving = false; } public void NodeChange(Vector2 vector,nodeType nodeType) { getNode(vector).nodetype = nodeType; } private void Start() { InitMap(); } }
|