/// <summary> /// Get a job from the world list and tests it reachability. /// </summary> /// <returns>True if succeeded</returns> void GetNewJob() { myJob = currTile.world.jobQueue.Dequeue(); if (myJob == null) { // There was no job on the queue for us, so just return false. return; } // We have a Job! // Immediately check if the job tile is reachable // NOTE: We might not be pathing to it right away. // Set job's nearest walkable tile as destination //destTile = GetNearestWalkableNeighbour(myJob.tile); destTile = myJob.tile; // Job Ended Callback myJob.RegisterJobCancelCallback(OnJobEnded); myJob.RegisterJobCompleteCallback(OnJobEnded); // Try to get a pathfinding from character to destTile. pathAStar = new Path_AStar(WorldController.Instance.world, currTile, destTile); if (pathAStar.Lenght() == 0) { // Pathfinding couldn't get a path to the destination. Debug.LogError("Path_AStar returned no path to target job tile!"); // Re-enqueue Job, set current to null and return false. AbandonJob(); return; } return; }
void Update_Movement(float deltaTime) { if (currTile == destTile) { path = null; return; // we are where we want to be; } if (nextTile == null || nextTile == currTile) { //pathfind next tile if (path == null || path.Lenght() == 0) { path = new Path_AStar(currTile.World, currTile, destTile); if (path.Lenght() == 0) { //Debug.Log ("no path to dest"); //FIXME job maybe must be reenqued instead; AbandonJob(); //FIXME cancel Job; return; } } //grab next tile nextTile = path.DequeueNextTile(); if (nextTile == currTile) { //Debug.LogError ("Update movement - next tile is currtile"); } } //total distance float distToTravel = Mathf.Sqrt(Mathf.Pow(currTile.X - nextTile.X, 2) + Mathf.Pow(currTile.Y - nextTile.Y, 2)); // float distThisFrame = speed * deltaTime; float percThisFrame = distThisFrame / distToTravel; movementPercentage += percThisFrame; if (movementPercentage >= 1) { currTile = nextTile; movementPercentage = 0; // FIXME => overshotmovement????? } }
void Update_DoMovement(float deltaTime) { if (currTile == destTile) { pathAStar = null; return; // We're already where we want to be. } // currTile = Tile I'm in and in process of leaving. // nextTile = Tile I'm currently entering. // destTile = Our final destination -- we never walk here directly but use it for the pathfinding. if (nextTile == null || nextTile == currTile) { // I don't have a nextTile so I get it from the pathfinder. if (pathAStar == null || pathAStar.Lenght() == 0){ // I don't have a pathfinder so I create one. pathAStar = new Path_AStar(WorldController.Instance.world,currTile, destTile); if (pathAStar.Lenght() == 0) { // Pathfinding couldn't get a path to the destination. Debug.LogError("Path_AStar returned no path to destination!"); // Re-enqueue Job and set current to null. AbandonJob(); return; } // Let's ignore the first tile, because that's the tile we're currently in. nextTile = pathAStar.Dequeue(); } // Now we have a path. // Grab next waypoint from the path system nextTile = pathAStar.Dequeue(); if (nextTile == currTile) { Debug.LogError("Update_DoMovement -- nextTile is currTile?"); } } // At this point we have a valid nextTile // Total distance. float distToTravel = Mathf.Sqrt(Mathf.Pow(currTile.X - nextTile.X, 2f) + Mathf.Pow(currTile.Y - nextTile.Y, 2f)); if (nextTile.IsEnterable() == ENTERABILITY.Never) { // Most likely a wall got built after path creation. // FIXME: When a wall gets spawned we should invalidate our pathfinding imediatale Debug.LogError("Update_DoMovement -- Trying to move to an unwalkable tile or walkable tile with moveCost of 0."); nextTile = null; // Our next tile is a no-go. pathAStar = null; // clearly our pathfinding info is out of date. return; } else if (nextTile.IsEnterable() == ENTERABILITY.Soon) { // The tile we are trying to enter is technically walkable (i.e. not a wall), // but are we allowed to enter it right now. // We return without processing the movement. return; } // Distance travelled this update. float distThisFrame = speed/nextTile.movementCost * deltaTime; // Percentage to destination this frame. float percThisFrame = distThisFrame / distToTravel; // Add that to overall percentage travelled. movementPercentage += percThisFrame; if (movementPercentage >= 1) { // We have reached our destination // TODO: Get next destination tile from pathfinding system. // TODO: If there are no more tiles, we are in the real destination currTile = nextTile; movementPercentage = 0; } if (cbCharacterChanged != null) { cbCharacterChanged(this); } }