void SortNodes(List <D_Node> items) { if (items.Count > 0) { bool isSorted = false; int lUnsorted = items.Count; while (!isSorted) { isSorted = true; for (int i = 0; i < lUnsorted; i++) { if (i + 1 < items.Count) { if (items[i + 1].g_score < items[i].g_score) { D_Node tmp = items[i + 1]; items[i + 1] = items[i]; items[i] = tmp; } } } lUnsorted--; } } }
void ConnectNodes() { int nodeCounter = 0; foreach (D_Node node in nodes) { D_Node tmp = SearchForNode(new Vector3(node.position.x, node.position.y, node.position.z - 1)); if (tmp != null) { node.connections.Add(tmp); } tmp = SearchForNode(new Vector3(node.position.x, node.position.y, node.position.z + 1)); if (tmp != null) { node.connections.Add(tmp); } tmp = SearchForNode(new Vector3(node.position.x - 1, node.position.y, node.position.z)); if (tmp != null) { node.connections.Add(tmp); } tmp = SearchForNode(new Vector3(node.position.x + 1, node.position.y, node.position.z)); if (tmp != null) { node.connections.Add(tmp); } nodeCounter++; } }
void MakeNodes() { Collider[] coll; bool waitForNext = false; bool doPlayerOnce = false; bool doTargetOnce = false; Vector3 lastNodePos = Vector3.zero; int currNode = 0; for (float i = -size.x; i < size.x; i++) { for (float j = -size.z; j < size.z; j++) { coll = Physics.OverlapBox(new Vector3(i, size.y, j), sizeOfBox / 2); foreach (Collider col in coll) { foreach (string tag in tags) { if (col.gameObject.CompareTag(tag) && !waitForNext) { nodes.Add(new D_Node(2, new Vector3(i, size.y, j))); waitForNext = true; } } if (col.gameObject.CompareTag(this.gameObject.tag) && !waitForNext) { if (!doPlayerOnce) { nodes.Add(new D_Node(0, new Vector3(i, size.y, j))); startNode = nodes[currNode]; waitForNext = true; doPlayerOnce = true; } } if (col.gameObject.CompareTag(target.gameObject.tag) && !waitForNext) { if (!doTargetOnce) { nodes.Add(new D_Node(-1, new Vector3(i, size.y, j))); targetNode = nodes[currNode]; waitForNext = true; doTargetOnce = true; } } } if (!waitForNext) { nodes.Add(new D_Node(1, new Vector3(i, size.y, j))); } currNode++; waitForNext = false; } } ConnectNodes(); }
bool isInOpen(D_Node value) { for (int i = 0; i < openList.Count; i++) { if (openList[i] == value) { return(true); } } return(false); }
bool isInClosesd(D_Node value) { for (int i = 0; i < closedList.Count; i++) { if (closedList[i] == value) { return(true); } } return(false); }
void LookForPath() { // do some sort of for loop in here currentNode = startNode; bool pathFound = false; int operations = 0; while (!pathFound) { if (!isInClosesd(currentNode)) { closedList.Add(currentNode); } for (int i = 0; i < currentNode.connections.Count; i++) { if (!isInOpen(currentNode.connections[i]) && !isInClosesd(currentNode.connections[i])) { currentNode.connections[i].g_score += currentNode.g_score; if (currentNode.connections[i].pastNode == null) { currentNode.connections[i].pastNode = currentNode; } openList.Add(currentNode.connections[i]); } } SortNodes(openList); if (openList.Count > 0) { currentNode = openList[0]; openList.Remove(currentNode); } if (currentNode == targetNode) { pathFound = true; } operations++; } pathFound = false; Debug.Log(operations); while (!pathFound) { if (currentNode != null) { path.Add(currentNode); currentNode = currentNode.pastNode; } else { pathFound = true; } } }
void CheckNodes(D_Node a, D_Node b) { }