public override void Apply(Path p) { //Good Game Vector3[] vectorPath = IntMath.Int3s2Vector3s(p.vectorPath).ToArray(); //VInt3[] vectorPath = p.vectorPath.ToArray(); if (vectorPath == null || vectorPath.Length <= 2) { return; } //Good Game List <Vector3> newPath = new List <Vector3>(); //List<VInt3> newPath = new List<VInt3>(); newPath.Add(vectorPath[0]); TurnConstructor.turningRadius = turningRadius; for (int i = 1; i < vectorPath.Length - 1; i++) { List <Turn> turnList = new List <Turn>(); TurnConstructor.Setup(i, vectorPath); turnConstruct1.Prepare(i, vectorPath); turnConstruct2.Prepare(i, vectorPath); TurnConstructor.PostPrepare(); if (i == 1) { turnConstruct1.PointToTangent(turnList); turnConstruct2.PointToTangent(turnList); } else { turnConstruct1.TangentToTangent(turnList); turnConstruct2.TangentToTangent(turnList); } EvaluatePaths(turnList, newPath); //Last point if (i == vectorPath.Length - 2) { turnConstruct1.TangentToPoint(turnList); turnConstruct2.TangentToPoint(turnList); } EvaluatePaths(turnList, newPath); } newPath.Add(vectorPath[vectorPath.Length - 1]); //Good Game //p.vectorPath = newPath; p.vectorPath = IntMath.Vector3s2Int3s(newPath); }
public override void Apply(Path p) { // This should never trigger unless some other modifier has messed stuff up if (p.vectorPath == null) { Debug.LogWarning("Can't process NULL path (has another modifier logged an error?)"); return; } //Good Game //List<Vector3> path = null; List <VInt3> path = null; switch (smoothType) { case SmoothType.Simple: path = SmoothSimple(p.vectorPath); break; case SmoothType.Bezier: path = SmoothBezier(p.vectorPath); break; case SmoothType.OffsetSimple: path = SmoothOffsetSimple(p.vectorPath); break; case SmoothType.CurvedNonuniform: //Good Game //path = CurvedNonuniform(p.vectorPath); break; path = IntMath.Vector3s2Int3s(CurvedNonuniform(IntMath.Int3s2Vector3s(p.vectorPath))); break; } if (path != p.vectorPath) { //Good Game //ListPool<Vector3>.Release(ref p.vectorPath); ListPool <VInt3> .Release(ref p.vectorPath); p.vectorPath = path; } }
/** Calculates desired velocity. * Finds the target path segment and returns the forward direction, scaled with speed. * A whole bunch of restrictions on the velocity is applied to make sure it doesn't overshoot, does not look too far ahead, * and slows down when close to the target. * /see speed * /see endReachedDistance * /see slowdownDistance * /see CalculateTargetPoint * /see targetPoint * /see targetDirection * /see currentWaypointIndex */ protected new Vector3 CalculateVelocity(Vector3 currentPosition) { if (path == null || path.vectorPath == null || path.vectorPath.Count == 0) { return(Vector3.zero); } //Good Game //List<Vector3> vPath = path.vectorPath; List <Vector3> vPath = IntMath.Int3s2Vector3s(path.vectorPath); if (vPath.Count == 1) { vPath.Insert(0, currentPosition); } if (currentWaypointIndex >= vPath.Count) { currentWaypointIndex = vPath.Count - 1; } if (currentWaypointIndex <= 1) { currentWaypointIndex = 1; } while (true) { if (currentWaypointIndex < vPath.Count - 1) { //There is a "next path segment" float dist = XZSqrMagnitude(vPath[currentWaypointIndex], currentPosition); //Mathfx.DistancePointSegmentStrict (vPath[currentWaypointIndex+1],vPath[currentWaypointIndex+2],currentPosition); if (dist < pickNextWaypointDist * pickNextWaypointDist) { lastFoundWaypointPosition = currentPosition; lastFoundWaypointTime = Time.time; currentWaypointIndex++; } else { break; } } else { break; } } Vector3 dir = vPath[currentWaypointIndex] - vPath[currentWaypointIndex - 1]; Vector3 targetPosition = CalculateTargetPoint(currentPosition, vPath[currentWaypointIndex - 1], vPath[currentWaypointIndex]); dir = targetPosition - currentPosition; dir.y = 0; float targetDist = dir.magnitude; float slowdown = Mathf.Clamp01(targetDist / slowdownDistance); this.targetDirection = dir; if (currentWaypointIndex == vPath.Count - 1 && targetDist <= endReachedDistance) { if (!reachedEndOfPath) { reachedEndOfPath = true; OnTargetReached(); } //Send a move request, this ensures gravity is applied return(Vector3.zero); } Vector3 forward = tr.forward; float dot = Vector3.Dot(dir.normalized, forward); float sp = maxSpeed * Mathf.Max(dot, minMoveScale) * slowdown; #if ASTARDEBUG Debug.DrawLine(vPath[currentWaypointIndex - 1], vPath[currentWaypointIndex], Color.black); Debug.DrawLine(GetFeetPosition(), targetPosition, Color.red); Debug.DrawRay(targetPosition, Vector3.up, Color.red); Debug.DrawRay(GetFeetPosition(), dir, Color.yellow); Debug.DrawRay(GetFeetPosition(), forward * sp, Color.cyan); #endif if (Time.deltaTime > 0) { sp = Mathf.Clamp(sp, 0, targetDist / (Time.deltaTime * 2)); } return(forward * sp); }