void Update_DoMovement(float deltaTime) { if (currTile == destTile) { path_AStar = null; return; } if (nextTile == null || nextTile == currTile) { //get the next tile from pathfinding if (path_AStar == null || path_AStar.Length() == 0) { path_AStar = new Path_AStar(WorldController.Instance.World, currTile, destTile); if (path_AStar.Length() == 0) { Debug.LogError("Path_AStar returned no path to destination"); AbandonJob(); path_AStar = null; return; } nextTile = path_AStar.GetNextTile(); } nextTile = path_AStar.GetNextTile(); //removes it from the path list if (nextTile == currTile) { Debug.Log("Update_DoMovement::NextTile is curr tile ?"); } } float totalDisToTravel = Vector3Int.Distance(currTile, nextTile); //total distance from A to B float movementCost = 1; ENTERABILITY enterability = ENTERABILITY.Yes; if (WorldController.Instance.World.foundationGameMap.ContainsKey(nextTile) == true) { movementCost = WorldController.Instance.World.foundationGameMap[nextTile].movementCost; try{ enterability = WorldController.Instance.World.foundationGameMap[nextTile].IsEnterable(WorldController.Instance.World.foundationGameMap[nextTile]); } catch { enterability = ENTERABILITY.Never; } if (movementCost > 0 && enterability == ENTERABILITY.Never) { enterability = ENTERABILITY.Yes; } } if (enterability == ENTERABILITY.Never) { Debug.LogError("Character " + name + " was trying to enter an unwalkable tile"); nextTile = currTile; path_AStar = null; return; } else if (enterability == ENTERABILITY.Soon) { //Have to wait to enter the tile, this is likely a door //No bailing on the movement/path, but we do return now and don't actually process the movement; return; } float distanceThisFrame = speed / movementCost * deltaTime; //how much distance can character tavel this update float percantageThisFrame = distanceThisFrame / totalDisToTravel; //how much is that in percentage movementPercentage += percantageThisFrame; //increase percentage moved if (movementPercentage >= 1) { //Destination reached currTile = nextTile; movementPercentage = 0; } if (cbCharacterMoved != null) { cbCharacterMoved(this); } }
void DoMovement() { if (curTile == DestTile) { // We made it StopMove(); return; } if (nextTile == null || nextTile == curTile) { // Get the next tile if (path == null || path.Length() == 0) { // Generate path path = new Path_AStar(activeArea, curTile, DestTile); if (path.Length() == 0) { StopMove(); // Debug.LogError("Enemy's PathAStar did not return a path!"); return; } // Ignore the first tile since we are on it nextTile = path.Dequeue(); } nextTile = path.Dequeue(); SetDirection(); ENTERABILITY nextTileEnterability = nextTile.CanEnter(); switch (nextTileEnterability) { case ENTERABILITY.Never: // Cant go on StopMove(); return; } if (nextTile == curTile) { StopMove(); return; } } // How much distance can we travel this frame? float distToTravel = Mathf.Sqrt( Mathf.Pow(curTile.X - nextTile.X, 2) + Mathf.Pow(curTile.Y - nextTile.Y, 2)); // How much distance can we travel this frame? float distThisFrame = speed.GetValue() / nextTile.MovementCost * Time.deltaTime; float perThisFrame = distThisFrame / distToTravel; movePercent += perThisFrame; if (movePercent >= 1) { movePercent = 0; curTile = nextTile; } }