public void endMove() { if (_controller != null) { _controller.StopMove(); } isAutoMove = false; _movePath = null; }
public MTBPath GetPath(Vector3 startingPos, Vector3 goalPos, bool needsGround = false) { MTBPath pathObject = new MTBPath(startingPos, goalPos); pathObject.needsGround = needsGround; lock (this.pathQueue) { this.pathQueue.Enqueue(pathObject); } return(pathObject); }
public bool startAutoMove(Vector3 targetPosition, float speed = 1F) { _movespeed = speed; WorldPos startWorldPos = Terrain.GetWorldPos(gameObject.transform.position); Vector3 startPos = new Vector3(startWorldPos.x, startWorldPos.y, startWorldPos.z); _movePath = MTBPathFinder.Instance.GetPath(startPos, targetPosition, true); isAutoMove = true; _moveStep = 0; return(_movePath.pathData != null); }
private void FindPath(MTBPath path) { openList.Clear(); closedList.Clear(); while (queuePathNode.Count > 0) { SavePathNode(queuePathNode.Dequeue()); } Vector3 a; // PathNode startNode = new PathNode(path.startPos, null, 0, path.goalPos); PathNode startNode = GetNewPathNode(path.startPos, null, 0, path.goalPos); queuePathNode.Enqueue(startNode); startNode.owner = startNode; PathNode currentNode = startNode; bool pathFound = false; bool noPath = false; int movementCost = 0; stopWatch.Reset(); stopWatch.Start(); try { while (!pathFound && !noPath) { for (int i = 0; i < positions.Length; i++) { positions[i] = currentNode.position + positionsOffset[i]; } // Analyze surrounding path nodes // PathNode[] nodes = new PathNode[positions.Length]; for (int i = 0; i < nodes.Length; i++) { nodes[i] = null; } PathNode lowestCostNode = null; // Check which ones are walkable and add them to the nodes-array for (int i = 0; i < positions.Length; i++) { // Movement cost from this to the surrounding block int currentMovementCost = (int)(Vector3.Distance(positions[i], currentNode.position) * 10); // Check if this node is walkable if (isGenerated((int)positions[i].x, (int)positions[i].y, (int)positions[i].z) && !ListContains(closedList, positions[i]) && !HasBlockInPos((int)positions[i].x, (int)positions[i].y, (int)positions[i].z) && // Walkable check (!path.needsGround || HasBlockInPos((int)positions[i].x, (int)positions[i].y - 1, (int)positions[i].z))) { // Add node to the nodes-array if (openList.ContainsKey(positions[i])) { nodes[i] = openList[positions[i]]; } else { // nodes[i] = new PathNode(positions[i], currentNode, movementCost + currentMovementCost, path.goalPos); nodes[i] = GetNewPathNode(positions[i], currentNode, movementCost + currentMovementCost, path.goalPos); openList.Add(positions[i], nodes[i]); queuePathNode.Enqueue(nodes[i]); } } // Check for lowest cost if (nodes[i] != null && (lowestCostNode == null || nodes[i].completeCost < lowestCostNode.completeCost)) { lowestCostNode = nodes[i]; } } if (lowestCostNode == null) { noPath = true; break; } if (currentNode.position.x == path.goalPos.x && currentNode.position.z == path.goalPos.z) { pathFound = true; } // Put the lowest cost node on the closed list if (currentNode.owner.position == lowestCostNode.owner.position) { currentNode.owner.nextNode = lowestCostNode; } else { currentNode.nextNode = lowestCostNode; } closedList.Add(currentNode.position); currentNode = lowestCostNode; } } catch (System.Exception exception) { noPath = true; } stopWatch.Stop(); if (noPath) { path.SetPathData(null); } else { // This is needed because in the closedlist there can be movements which are like // front, right // this should be done in one step frontright and this gets achieved by generating an array from the path node's linked list. pathData.Clear(); PathNode cNode = startNode; while (cNode != null) { pathData.Add(cNode.position); cNode = cNode.nextNode; } path.SetPathData(pathData.ToArray()); } path.runtime = (float)stopWatch.Elapsed.TotalMilliseconds; }