Exemplo n.º 1
0
        protected override void OnFork(ForkEventArgs args)
        {
            int[] connections;
            if (AllowDirectionChange)
            {
                int connection = Random.Range(0, args.Junction.ConnectionsCount);
                args.SelectedConnectionIndex = connection;
            }
            else
            {
                if (SplineFollow.Speed >= 0)
                {
                    connections = args.Junction.GetOutputs();
                    if (connections.Length == 0)
                    {
                        connections = args.Junction.GetInputs();
                    }
                }
                else
                {
                    connections = args.Junction.GetInputs();
                    if (connections.Length == 0)
                    {
                        connections = args.Junction.GetOutputs();
                    }
                }

                if (connections.Length > 0)
                {
                    int connection = Random.Range(0, connections.Length);
                    args.SelectedConnectionIndex = connections[connection];
                }
            }
        }
Exemplo n.º 2
0
 protected virtual void OnFork(ForkEventArgs args)
 {
 }
Exemplo n.º 3
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);
        }