public static void PerformCharacterMovement(Character charToMove) { float currentDestinationX = charToMove.movementProfile.currentDestinationPixels.X; float currentDestinationY = charToMove.movementProfile.currentDestinationPixels.Y; float currentLocationX = charToMove.pixelPosition.X; float currentLocationY = charToMove.pixelPosition.Y; //if the character is within deltaX or detaY of the destination, move them there and make the next segment active. float distanceFromDestX = Math.Abs(currentDestinationX - currentLocationX); float distanceFromDestY = Math.Abs(currentDestinationY - currentLocationY); if (distanceFromDestX < Character.movementSpeedX || distanceFromDestY < Character.movementSpeedY) { //switch to next segment along character path int indexOfCurrentSegment = charToMove.movementProfile.currentpath.characterPath.IndexOf(charToMove.movementProfile.currentSegment); int numberOfSegmentsInPath = charToMove.movementProfile.currentpath.characterPath.Count(); if (indexOfCurrentSegment == numberOfSegmentsInPath - 1) { //we're done, stop movement charToMove.state = charState.still; charToMove.position.X = charToMove.movementProfile.finalDestination.xPos; charToMove.position.Y = charToMove.movementProfile.finalDestination.yPos; } else { //We're not done - there are more segments in this character's current path. Move to the next, then call the movement function again. charToMove.pixelPosition = new Vector2(currentDestinationX,currentDestinationY); charToMove.movementProfile.currentSegment = charToMove.movementProfile.currentpath.characterPath[indexOfCurrentSegment + 1]; charToMove.movementProfile.currentDestination = charToMove.movementProfile.currentSegment.segmentTiles.Last(); Tile newDestinationTile = charToMove.movementProfile.currentDestination; Vector2 newDestinationVector = new Vector2(newDestinationTile.xPos, newDestinationTile.yPos); charToMove.movementProfile.currentDestinationPixels = charToMove.getPixelPositionByTilePosition(newDestinationVector); PerformCharacterMovement(charToMove); } } if (currentDestinationX > currentLocationX) { currentLocationX += Character.movementSpeedX; } else if (currentDestinationX < currentLocationX) { currentLocationX -= Character.movementSpeedX; } else { //currentLocation == currentDestination } if (currentDestinationY > currentLocationY) { currentLocationY += Character.movementSpeedY; } else if (currentDestinationY < currentLocationX) { currentLocationY -= Character.movementSpeedY; } else { //currentLocation == currentDestination } Vector2 newCharLocation = new Vector2(currentLocationX, currentLocationY); charToMove.pixelPosition = newCharLocation; }
public MovementProfile(MovementPath moveProfile, PathSegment pathSegment, Character charToMove) { currentpath = moveProfile; currentSegment = pathSegment; currentDestination = pathSegment.segmentTiles.Last(); Vector2 currentDestinationVector = new Vector2(currentDestination.xPos, currentDestination.yPos); finalDestination = currentpath.characterPath.Last().segmentTiles.Last(); Vector2 finalDestinationVector = new Vector2(finalDestination.xPos,finalDestination.yPos); currentDestinationPixels = charToMove.getPixelPositionByTilePosition(currentDestinationVector); finalDestinationPixels = charToMove.getPixelPositionByTilePosition(finalDestinationVector); }