private void PerformNextMove() { bool canContinue = true; //Check if we have successfully moved, otherwise reattempt is occupier is moving if ((int)currentPath[1].X == elementToMove.getWorldPositionX() && (int)currentPath[1].Y == elementToMove.getWorldPositionY()) { //remove that step currentPath.RemoveAt(0); } else { if (associatedMap.getOccupied(new Vector2(currentPath[1].X, currentPath[1].Y))) { if (associatedMap.getOccupyingElement(new Vector2(currentPath[1].X, currentPath[1].Y)).GetMovable()) { if (elementToMove.GetStuck()) { //already stuck, stop movement elementToMove.KillLinkedMovement(); this.ShutdownSmoothly(); } else { elementToMove.SetStuck(true); } elementToMove.SetStatusMessage("Someone is in my spot!"); this.ShutdownSmoothly(); //instead of kill canContinue = false; if (associatedMap.getOccupied(elementToMove.GetFinalDestination())) { if (!associatedMap.getOccupyingElement(elementToMove.GetFinalDestination()).Moving()) { reattemptCounter++; EventMoveTo reattempt = new EventMoveTo(associatedGame, associatedMap, elementToMove, associatedMap.FindNearest(originalDestination, "Empty"), this.gameTime); //transfer the calling event to the new event as we are giving up control reattempt.setCallingEvent(this.GetCallingEvent()); reattempt.SetReattemptCounter(reattemptCounter); reattempt.SetOriginalDestination(originalDestination); elementToMove.ReplaceLinkedMovement(reattempt); callingEventManager.AddEvent(reattempt); } else { reattemptCounter++; EventMoveTo reattempt = new EventMoveTo(associatedGame, associatedMap, elementToMove, destination, this.gameTime); //transfer the calling event to the new event as we are giving up control reattempt.setCallingEvent(this.GetCallingEvent()); reattempt.SetReattemptCounter(reattemptCounter); elementToMove.ReplaceLinkedMovement(reattempt); callingEventManager.AddEvent(reattempt); } } else { reattemptCounter++; EventMoveTo reattempt = new EventMoveTo(associatedGame, associatedMap, elementToMove, destination, this.gameTime); //transfer the calling event to the new event as we are giving up control reattempt.setCallingEvent(this.GetCallingEvent()); reattempt.SetReattemptCounter(reattemptCounter); elementToMove.ReplaceLinkedMovement(reattempt); callingEventManager.AddEvent(reattempt); } } } } if (canContinue) { Event nextMove = new EventMoveTo(associatedGame, associatedMap, elementToMove, currentPath[1], this.gameTime); callingEventManager.AddEvent(nextMove); //suspend until that move event ends this.Suspend(nextMove); } else { this.SetComplete(); } }