/// <summary> /// Moves to given position and returns the first point of the path. /// <param name="followMode">Set this to true, if the variable mTarget /// has been set and the MoveTo is supposed to follow a /// GameObject</param> /// </summary> public Vector2? MoveTo(Vector2 targetPosition, bool followMode = false) { //Debug.WriteLine("Move from " + mPosition + " to " + targetPosition + " received, Lieutenant!"); if (!followMode) mTarget = null; mDestination = targetPosition; mPath = PathFinder.GetInstance().CalculatePath(Position, targetPosition, mOwner.GetType()); if (mPath != null) { // For debug drawing if (GlobalValues.GetInstance().mShowPaths) CodeTest.AddPathToDraw(mPath, Position); mNextWayPoint = mPath.mPoints.Pop(); ResetUpdateCounters(mNextWayPoint); return mNextWayPoint; } Debug.WriteLine("Got no path!"); mNextWayPoint = null; return null; }
/// <summary> /// Initiates the recalculation of the Position /// </summary> /// <returns>Returns true as long as it is moving.</returns> public bool Update() { if (mPath == null || (ReachedNextWayPoint() && mPath.mPoints.Count == 0)) return false; if (ReachedNextWayPoint()) { mNextWayPoint = mPath.mPoints.Pop(); ResetUpdateCounters(mNextWayPoint); } ProcessMovement(); if (mPath == null) return false; // it may be that the previous call stops us mCurrentUpdateCounter++; if (mCurrentUpdateCounter > mEstimatedUpdatesToNextPoint + GlobalValues.GetInstance().mSkipWaypointTimeout) { // Looks like we can't reach the waypoint, what now? // Try to abbreviate (skip waypoint): if (mPath.mPoints.Count > 0 && PathFinder.GetInstance() .DirectConnection(Position, mPath.mPoints.Peek(), mOwner.GetType())) { mNextWayPoint = mPath.mPoints.Pop(); ResetUpdateCounters(mNextWayPoint); } else if (mPath.mPoints.Count == 0 && (Position - mDestination).Length()<150) { // Stop movement as it doesn't matter anymore mPath = null; mNextWayPoint = null; return false; } } return true; }
public FreeMovement(IMovable owner) { mSpeed = owner.MovementSpeed; mOwner = owner; mPath = new Path(new Stack<Vector2>()); }
/// <summary> /// Adds a path to CodeTest.mPathlistToDraw, such that the given path /// will be drawn on the screen. /// </summary> /// <param name="path">the path to be drawn</param> /// <param name="startPosition">the start position to pre-add which by default is not part of a path</param> public static void AddPathToDraw(Path path, Vector2 startPosition) { List<Vector2> list = new List<Vector2>(path.mPoints); list.Insert(0, startPosition); mPathlistToDraw.Add(list); }