private void CheckBranches(int curveIndex) { int pointIndex = curveIndex * 3; if (m_t >= 1.0f) { pointIndex += 3; } m_curveIndex = curveIndex; if (m_spline.HasBranches(pointIndex)) { ForkEventArgs args = new ForkEventArgs(m_spline, pointIndex); Fork.Invoke(args); if (args.SelectBranchIndex > -1 && args.SelectBranchIndex < args.Branches.Length) { Debug.Log("CurveIndex " + m_curveIndex); Debug.Log("Selected Branch " + args.SelectBranchIndex); m_spline = args.Branches[args.SelectBranchIndex]; m_t = 0.0f; m_curveIndex = 0; } } }
private void Move(float deltaTime, bool allowRecursiveCall) { float v, deltaT; v = m_spline.GetVelocity(m_t).magnitude; v *= m_spline.CurveCount; deltaT = (deltaTime * Speed) / v;// F.1 int pointIndex; if (deltaT >= 0) { pointIndex = m_spline.GetCurveIndex(m_t); } else { pointIndex = m_spline.GetCurveIndex(m_t) + 1; } int nextPointIndex; float nextT = m_t + deltaT; bool endOfSpline = nextT <= 0.0f || 1.0f <= nextT; if (endOfSpline) { if (deltaT >= 0) { nextPointIndex = (pointIndex + 1) % m_spline.PointsCount; } else { nextPointIndex = (m_spline.PointsCount + pointIndex - 1) % m_spline.PointsCount; } } else { if (deltaT >= 0) { nextPointIndex = m_spline.GetCurveIndex(nextT); } else { nextPointIndex = m_spline.GetCurveIndex(nextT) + 1; } } bool continueIfLoop = IsLoop; if (pointIndex != nextPointIndex) { //Debug.Log("Point Index " + pointIndex + " Next Point Index " + nextPointIndex); JunctionBase junction = m_spline.GetJunction(nextPointIndex); if (junction != null) { ForkEventArgs args = new ForkEventArgs(m_spline, nextPointIndex); Fork.Invoke(args); int connection = -1; if (args.SelectedConnectionIndex == -1) { if (StopAtJunction) { IsRunning = false; return; } if (endOfSpline) { if (deltaT > 0) { connection = junction.FirstOut(); if (connection == -1) { connection = junction.FirstIn(); } } else { connection = junction.FirstIn(); if (connection == -1) { connection = junction.FirstOut(); } } if (connection == -1) { continueIfLoop = false; } } } else { connection = args.SelectedConnectionIndex; } if (connection != -1) { float nextPointT = m_spline.GetTAt(nextPointIndex); deltaT = m_t + deltaT - nextPointT; float remDeltaTime = deltaT * v / Speed; //Calculated using F.1 m_spline = junction.GetSpline(connection); int splinePointIndex = junction.GetSplinePointIndex(connection); m_t = m_spline.GetTAt(splinePointIndex); bool isOut = junction.IsOut(connection); bool isIn = junction.IsIn(connection); if (isOut && !isIn && Speed < 0 || isIn && !isOut && Speed > 0) { Speed *= -1; } if (allowRecursiveCall) { Move(remDeltaTime, false); return; } else { v = m_spline.GetVelocity(m_t).magnitude; v *= m_spline.CurveCount; deltaT = (remDeltaTime * Speed) / v;// F.1 } } } } MoveOrStop(deltaT, endOfSpline, continueIfLoop); }