private void followPlayer() { if (pet.CurrentBehavior != Pet.behavior_walking) { pet.CurrentBehavior = Pet.behavior_walking; } if (currentPath == null || savedFarmerPosition != farmer.getTileLocation()) { generatePath(); } if (currentPath != null && currentPath.Count > 0) { var target = PathFindingUtil.getTargetPosition(currentPath.First()); var currentPos = pet.position; if (Vector2.Distance(currentPos, target) < 4) { currentPath.Remove(currentPath.First()); pet.position = target; } else { var velocity = Utility.getVelocityTowardPoint(currentPos, target, 4); pet.xVelocity = velocity.X; pet.yVelocity = velocity.Y * -1; if (Math.Abs(velocity.X) > Math.Abs(velocity.Y)) { pet.facingDirection = velocity.X >= 0 ? 1 : 3; } else { pet.facingDirection = velocity.Y >= 0 ? 2 : 0; } pet.setMovingInFacingDirection(); pet.animateInFacingDirection(Game1.currentGameTime); } } else { currentPath = null; provider.forceUpdate(); } }
private void generatePath() { try { currentPath = PathFindingUtil.FindPathToFarmer(pet, farmer); // save farmer position, don't include in final pathing savedFarmerPosition = currentPath.Last(); currentPath.Remove(savedFarmerPosition); currentPath.Remove(currentPath.Last()); } catch (Exception) { currentPath = null; provider.setCurrentAction(Action.SITTING); } }