/// <summary> /// Drive locomotion to walk towards point. /// Returns: /// 1 - arrived at location. /// 0 - walking(pathing) to location. /// -1 - can't reach location. /// </summary> public int WalkTo(Vector2 Location, int OccupiedId = 0) { StandUp(); // Destination position can be shifted, but can only be shifted by 0.25 max. Therefore a successful walk // is 0.3 from destination. if ((mPosition - Location).sqrMagnitude <= (0.3f * 0.3f)) { CancelWalking(); return(1); } if (_pathCounter >= 20) { _pathCounter = 0; //mNavPath = CPathFinder.FindPathSmoothed(mOwner, mWorld.mMap, mPosition, mPathDest); // TODO: Make sure owner is a valid player? System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); mNRNavPath = CNavRectPather.FindPath(mWorld.mMap.mNavMeshes[mOwner], mPosition, Location, OccupiedId); sw.Stop(); //Debug.LogWarning("Path: " + sw.Elapsed.TotalMilliseconds + "ms"); if (mNRNavPath != null) { // Eliminate first node because we are already standing on it //mNRNavPath = mNRNavPath.mNextMove; mPathing = true; } else { mPathing = false; mPathDest = Vector2.zero; } } return(0); }
private void _UpdatePosition() { if (!mActive) { return; } Vector2 targetPos = mPosition; if (mPathing) { // If we have a waypoint we must navigate to it. if (mNRNavPath != null) { while (mNRNavPath.mNextMove != null && CNavRectPather.IsNodeReachable(mWorld.mMap, mOwner, mPosition, mNRNavPath.mNextMove)) { mNRNavPath = mNRNavPath.mNextMove; } // Face direction to target. // Movement Code //Vector2 target = new Vector2(mNavPath.mPosition.X * 0.5f + 0.25f, mNavPath.mPosition.Y * 0.5f + 0.25f); Vector2 target = mNRNavPath.mPosition; Vector2 dir = target - mPosition; //mDirection = Vector3.Slerp(mDirection.ToWorldVec3(), dir.normalized.ToWorldVec3(), 0.5f).ToWorldVec2(); mDirection = dir.normalized; float speed = mSpeed; if (mCarryingPickup != null) { speed = 2; } speed *= CWorld.SECONDS_PER_TICK; //targetPos = mPosition + (mDirection * speed); // Target is closer than walk speed if (dir.SqrMagnitude() <= speed * speed) { targetPos = target; } else { targetPos = mPosition + (mDirection * speed); } mRotation = Quaternion.LookRotation(new Vector3(mDirection.x, 0.0f, mDirection.y)); /* * if (dir.magnitude > 0.1f) * { * float speed = mSpeed; * speed *= CWorld.SECONDS_PER_TICK; * * dir.Normalize(); * targetPos = mPosition + (dir * speed); * mRotation = Quaternion.LookRotation(new Vector3(dir.x, 0.0f, dir.y)); * } */ } // Path to exact path location else { if (mPathDest != Vector2.zero) { // Movement Code Vector2 target = mPathDest; Vector2 dir = target - mPosition; if (dir.magnitude > 0.1f) { float speed = mSpeed; if (mCarryingPickup != null) { speed = 2; } speed *= CWorld.SECONDS_PER_TICK; dir.Normalize(); targetPos = mPosition + (dir * speed); mRotation = Quaternion.LookRotation(new Vector3(dir.x, 0.0f, dir.y)); } else { // Pathing complete mPathing = false; mPathDest = Vector2.zero; } } } } if (!mPathing && mCollide) { targetPos += mBeingPushedForce; } else { mBeingPushedForce = Vector2.zero; } if (mPosition != targetPos) { if (mCollide) { mPosition = mWorld.mMap.Move(mPosition, targetPos, 0.15f); } else { // Move at unit speed to target mPosition = targetPos; } } mBeingPushedForce *= 0.2f; // Check if we have hit the nav point /* * if (mNavPath != null) * { * Vector2 target = new Vector2(mNavPath.mPosition.X * 0.5f + 0.25f, mNavPath.mPosition.Y * 0.5f + 0.25f); * Vector2 dir = target - mPosition; * if (dir.magnitude <= 0.1f) * { * mNavPath = mNavPath.mNextMove; * mPosition = target; * } * } */ // Snap to waypoint if we are close enough? if (mNRNavPath != null) { Vector2 target = mNRNavPath.mPosition; Vector2 dir = target - mPosition; if (dir.magnitude <= 0.001f) { mNRNavPath = mNRNavPath.mNextMove; mPosition = target; } // TODO: Test for end of path and indicate such. } CalcBounds(); }