protected override void ActorMoveUpdate(GameObject actor) { base.ActorMoveUpdate(actor); TileBasedFoundPath path = (TileBasedFoundPath)pathTable[actor]; //if (GoToNextPoint(actor, path.pathEdges[0].EndPos(), path.speed)) if (Vector3.Distance(actor.transform.position, path.paths[0].Position()) <= path.tol) { if (path.paths.Count <= 1) { // special case for hidden stairs GameObject curTile = path.paths[0].Object(); if (curTile) { actor.transform.SetParent(curTile.transform); } // arrived at the final position if (path.endEvent != null) { Services.eventManager.QueueEvent(path.endEvent); } pathTable.Remove(actor); Services.gameEvents.StopMan(actor); } else { GameObject curTile = path.paths[1].Object(); if (curTile) { actor.transform.SetParent(curTile.transform); } Services.gameEvents.SetManTargetPosition(actor, path.paths[1].Position(), path.tol, path.paths[1].pathType); path.paths.RemoveAt(0); } } }
public override bool FindPath(GameObject actor, Vector3 endPos, float clampTol = 0.5F) { recentPath = new TileBasedFoundPath(); Vector3 startPos = actor.transform.position; GameObject startTile = FindNearestTile(startPos); GameObject endTile = FindNearestTile(endPos); if (startTile == null || endTile == null) { return(false); } recentPath.endUnit = endTile; // re-number all the pathpoints Dictionary <GameObject, int> IDs = new Dictionary <GameObject, int>(); int N = 0; foreach (GameObject tile in tiles) { if (!IDs.ContainsKey(tile)) { IDs.Add(tile, N); ++N; } } // init path matrix float[,] path = new float[tiles.Count, tiles.Count]; TileEdge.MovementType[,] pathTypes = new TileEdge.MovementType[tiles.Count, tiles.Count]; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { path[i, j] = float.MaxValue; } } foreach (GameObject edge in tileEdges) { GameObject p0 = edge.GetComponent <TileEdge>().t0; GameObject p1 = edge.GetComponent <TileEdge>().t1; if (p0.GetComponent <Tile>().enabled&& p1.GetComponent <Tile>().enabled) { int id0 = IDs[p0]; int id1 = IDs[p1]; path[id0, id1] = path[id1, id0] = Vector3.Distance(p0.transform.position, p1.transform.position); pathTypes[id0, id1] = pathTypes[id1, id0] = edge.GetComponent <TileEdge>().type; } //Debug.Log(p0 + " " + p1 + " " + id0 + " " + id1 + " " + edge.GetComponent<TileEdge>().type); } float[] d = new float[N]; int[] preTile = new int[N]; List <int> queue = new List <int>(); bool[] inQueue = new bool[N]; for (int i = 0; i < N; ++i) { inQueue[i] = false; preTile[i] = -1; d[i] = float.MaxValue; } int startID = IDs[startTile]; int endID = IDs[endTile]; int head = 0; int tail = 0; queue.Add(startID); inQueue[startID] = true; ++tail; d[startID] = 0; while (head < tail) { int o = queue[head]; inQueue[o] = false; for (int i = 0; i < N; ++i) { if (d[i] > d[o] + path[o, i]) { d[i] = d[o] + path[o, i]; if (!inQueue[i]) { queue.Add(i); inQueue[i] = true; ++tail; preTile[i] = o; } } } ++head; } if (d[endID] >= float.MaxValue) { return(false); } int curTile = endID; recentPath.InsertAtHead(endPos); while (curTile != -1) { if (preTile[curTile] == -1) { recentPath.InsertAtHead(tiles[curTile]); } else { //Debug.Log(curTile + " " + preTile[curTile] + " " + pathTypes[curTile, preTile[curTile]]); recentPath.InsertAtHead(tiles[curTile], pathTypes[curTile, preTile[curTile]]); } curTile = preTile[curTile]; } recentPath.InsertAtHead(startPos); recentPath.isComplete = true; return(true); }