public static MovementPath DirectWithObstacles(Point start, Point end, Rectangle acceptableArea) { MovementPath returnPath = new MovementPath(null); returnPath.AddPoint(start); Point currentPoint = start; MovementDirection lastMovement = MovementDirection.None; int obstacleProbability = 5; List<Point> obstacles = new List<Point>(); //work toward the 'finish' while (currentPoint.x != end.x && currentPoint.y != end.y) { //try to continue in a straight line //if(lastMovement != MovementDirection.None && currentPoint.x } returnPath.AddPoint(end); return returnPath; }
public static MovementPath DrunkWalk(Point start, Point end, Rectangle acceptableArea) { MovementPath newPath = new MovementPath(null); newPath.AddPoint(start); Point currentPoint = start; Point midPoint = end; Point point1 = end; Point point2 = null; for (int deviations = 0; deviations < 7; deviations++) { point1 = Point.MidPoint(start, point1).Shift(MovementPath.randomDirection(), 3); if (acceptableArea.Contains(point1)) { newPath.AddPoint(point1); } if (point2 != null) { point2 = Point.MidPoint(point2, end).Shift(MovementPath.randomDirection(), 3); if (acceptableArea.Contains(point2)) { newPath.AddPoint(point2); } } else { point2 = point1; } } newPath.AddPoint(end); return newPath; }
//resolve the shortest path between two given points public static MovementPath ResolvePath2(Point start, Point end, Stage stage = null) { MovementPath newPath = new MovementPath(null); if (stage == null) { stage = Stage.CurrentStage; } // The last point added Point currentPoint = start; //need better pathfinding and collision detection later // Add the beginning newPath.AddPoint(start); // List all of the avenues we've explored List<PathLeg> pathLegs = new List<PathLeg>(); // Start from the start and explore the legs // March repeatedly through each path, until reaching the exit or a dead end //if there's horizontal distance to cover between the points, cover it first while(currentPoint.x != end.x) { // If we can move to the next tile, do so if (stage.GetTileAt((int)currentPoint.x + 1, (int)currentPoint.y).GetWalkable()) { currentPoint = new Point(currentPoint.x + 1, currentPoint.y); newPath.AddPoint(currentPoint); } // If not, switch to vertical differencing ... else { } } return newPath; }
public static MovementPath Patrol(Point origin, Point destination) { MovementPath newPath = new MovementPath(null); newPath.AddPoint(origin); newPath.AddPoint(destination); newPath.SetLooping(true); return newPath; }
/// <summary> /// Randomize a path from start to finish. Assume no obstacles. /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public static MovementPath old_DrunkWalk(Point start, Point end, Rectangle acceptableArea) { MovementPath newPath = new MovementPath(null); //the number of steps taken in this direction so far int stepsTaken = 0; //the maximum number of 'steps' the 'drunk' can take in one direction before stumbling / changing direction int maxSteps = 5; //the current position of the path Point currentPos = start; //the next position Point nextPos; //the last cardinal direction the character has walked Direction lastDirection; //walk until we get to the end while (currentPos != end) { /* //if we have moved previously, and haven't walked so far as to have to stumble if (lastDirection != "" && stepsTaken < maxSteps) { //throw a weighted random to select if we move on this direction again } */ //pick a random direction lastDirection = MovementPath.randomDirection(); //make sure it's a valid (non negative) point nextPos = currentPos.Shift(lastDirection); if(newPath.Contains(nextPos) == false && acceptableArea.Contains(nextPos)) { newPath.AddPoint(nextPos); currentPos = nextPos; } //otherwise, we'll just try again next time! } return newPath; }
/// <summary> /// Return every single node from the start to the finish, the full path of this movementpath. /// </summary> /// <returns></returns> public MovementPath FullPath() { MovementPath returnPath = new MovementPath(null); int currentPos = 0; int totalPoints = this.nodes.Count - 1; Point currentPoint = this.nodes[0]; returnPath.AddPoint(this.nodes[0]); Point comparePoint; //loop through all of the points while (currentPos < totalPoints) { comparePoint = this.nodes[currentPos + 1]; //walk towards each point while (currentPoint.x != comparePoint.x || currentPoint.y != comparePoint.y) { if (currentPoint.x < comparePoint.x) { currentPoint.x = currentPoint.x + 1; } else if (currentPoint.x > comparePoint.x) { currentPoint.x = currentPoint.x - 1; } else if (currentPoint.y < comparePoint.y) { currentPoint.y = currentPoint.y + 1; } else { currentPoint.y = currentPoint.y - 1; } returnPath.AddPoint(currentPoint); } currentPos++; } return returnPath; }