public MyTradeRoute getNext(MyTradeRoute tradeRoute, bool reverse = false)
 {
     for (int i = 0; i < list.Count; i++)
     {
         MyTradeRoute tr = list [i];
         if (tradeRoute == tr)
         {
             if (!reverse)
             {
                 if (i < list.Count - 1)
                 {
                     return(list [i + 1]);
                 }
                 else
                 {
                     return(list [i]);
                 }
             }
             else
             {
                 if (i > 0)
                 {
                     return(list [i - 1]);
                 }
                 else
                 {
                     return(list [i]);
                 }
             }
         }
     }
     return(null);
 }
 void init()
 {
     mTrans = transform;
     if (tradeRouteMain != null)
     {
         tradeRouteMain.Update();
         tradeRoute = tradeRouteMain;
         initPos();
         hadLastTown = false;
         canMoveShip = true;
         mTargetRot  = mTrans.localRotation;
         mTargetPos  = mTrans.localPosition;
         mOffset.x   = Random.Range(0.0f, 10.0f);
         mOffset.y   = Random.Range(0.0f, 10.0f);
     }
 }
    /// <summary>
    /// Finds the next trade route connected to the specified town.
    /// </summary>

    public MyTradeRoute FindNext(MyTradeRoute tradeRoute, Vector3 town, bool reverse)
    {
        bool         found = false;
        MyTradeRoute first = null;
        MyTradeRoute last  = null;

        foreach (MyTradeRoute tr in list)
        {
            if (tr == tradeRoute)
            {
                // Now that we've found the current node, if we're going in reverse, we can use the last node
                if (reverse && last != null)
                {
                    return(last);
                }

                // Remember that we've found the current node
                found = true;
            }
            else if (tr.GetConnectedTown(town))
            {
                // If the current node has already been found and we're going in order, we're done
                if (found && !reverse)
                {
                    return(tr);
                }

                // Remember this node
                if (first == null)
                {
                    first = tr;
                }
                last = tr;
            }
        }

        // If we were going in reverse, just return the last available node
        if (reverse)
        {
            return((last == null) ? tradeRoute : last);
        }

        // Going in order? Just return the first node.
        return((first == null) ? tradeRoute : first);
    }
    void Update()
    {
        // If this is a brand new ship, dock it at the first town

        // Calculate the bobble rotation
        Vector3 rot = new Vector3(Mathf.Sin(mOffset.x + Time.time * 0.7326f) * 0.75f, 0f,
                                  Mathf.Sin(mOffset.y + Time.time * 1.2265f) * 1.5f);
        Quaternion bobble = Quaternion.Euler(rot);

        if (tradeRoute == null)
        {
            mTrans.rotation = bobble;
            if (tail != null)
            {
                tail.SetActiveRecursively(false);
            }
        }
        else
        {
            if (!hadLastTown)
            {
                initPos();
            }
            //DockShip (tradeRoute.town0);

            // If it's time to start moving the ship, do that
            if (mStartTime < Time.time && canMoveShip)
            {
                if (tail != null)
                {
                    tail.SetActiveRecursively(true);
                }

                // Ships should start with the speed of 0 and accelerate gradually
                //float acceleration = 0.05f * prefab.acceleration * Time.deltaTime;
                float acceleration = mAcceleration * Time.deltaTime;

                // Adjust the traveling speed
                speed = Mathf.Min(speed + acceleration, maxSpeed);
                //speed = speed + acceleration;

                // Distance the ship has traveled since it left the dock
                distance += speed * Time.deltaTime;

                // Sampling factor in 0-1 range
                float length = tradeRoute.length;
                float factor = Mathf.Clamp01(distance / length);
                mfinishPercent = factor;
                Vector3 nextPos    = Vector3.zero;
                bool    hadNextPos = false;
                //Debug.Log("mLastTown == tradeRoute.town0==" + mLastTown.ToString() + " == " +tradeRoute.town0.ToString());
                if (mLastTown == tradeRoute.town0)
                {
                    // Traveling from Town0 to Town1
                    float time = Interpolation.Linear(0f, length, factor);
                    mTargetPos = tradeRoute.normalizedPath.Sample(time, SplineV.SampleType.Linear);
                    nextPos    = tradeRoute.normalizedPath.Sample(time + 1f, SplineV.SampleType.Linear);
                    hadNextPos = true;
                    if (factor == 1f || Vector3.Distance(transform.localPosition, tradeRoute.town1) <= arriveDistance)
                    {
                        DockShip(tradeRoute.town1);
                        speed      = 0f;
                        distance   = 0f;
                        tradeRoute = tradeRouteMain.getNext(tradeRoute);
                    }
                }
                else
                {
                    if (style == Style.PingPong)
                    {
                        // Traveling from Town1 to Town0
                        float time = Interpolation.Linear(length, 0f, factor);
                        mTargetPos = tradeRoute.normalizedPath.Sample(time, SplineV.SampleType.Linear);
                        nextPos    = tradeRoute.normalizedPath.Sample(time - 1f, SplineV.SampleType.Linear);
                        hadNextPos = true;
                        if (factor == 1f)
                        {
                            DockShip(tradeRoute.town0);
                            speed      = 0f;
                            distance   = 0f;
                            tradeRoute = tradeRouteMain.getNext(tradeRoute, true);
                        }
                    }
                    else if (style == Style.Loop)
                    {
                        tradeRoute = tradeRouteMain;
                        DockShip(tradeRouteMain.town0);
                        speed    = 0f;
                        distance = 0f;
                    }
                    else
                    {
                        canMoveShip = false;
                        if (tail != null)
                        {
                            tail.SetActiveRecursively(false);
                        }

                        if (finishCallback != null)
                        {
                            finishCallback();
                        }
                    }
                }

                // Calculate the rotation
                if (hadNextPos)
                {
                    Vector3 diff = nextPos - mTargetPos;
                    if (diff.magnitude > 0.01f)
                    {
                        mTargetRot = Quaternion.LookRotation(diff);
                    }
                }
            }
            else
            {
                //speed = 0f;
                //distance = 0f;
            }

            // Update the position
            {
                float factor = Time.deltaTime * 5.0f;
                //mTrans.position = Vector3.Lerp (mTrans.position, mTargetPos, factor);
                mTrans.localPosition = Vector3.Lerp(mTrans.localPosition, mTargetPos, factor);
                //mTrans.rotation = Quaternion.Slerp (mTrans.rotation, bobble * mTargetRot, factor);
                mTrans.localRotation = Quaternion.Slerp(mTrans.localRotation, bobble * mTargetRot, factor);
            }
        }
    }
    public override void OnInspectorGUI()
    {
        tradeToute = target as MyTradeRoute;
        DrawDefaultInspector();
        NGUIEditorTools.DrawSeparator();

        RaycastHit hitt;

        if (tradeToute.KeyTradeRouteNodes != null)
        {
            for (int i = 0; i < tradeToute.KeyTradeRouteNodes.Count - 1; i++)
            {
                Debug.DrawLine(tradeToute.KeyTradeRouteNodes [i], tradeToute.KeyTradeRouteNodes [i + 1]);
            }
        }

        EditorGUILayout.BeginHorizontal();
        {
            EditorGUILayout.LabelField("Editor Trade Route");
            if (GUILayout.Button("Editor"))
            {
                tradeToute.isEditMode = !tradeToute.isEditMode;
                isEditor = tradeToute.isEditMode;
//				tradeToute.Update();
            }
        }
        EditorGUILayout.EndHorizontal();
        if (isEditor)
        {
            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField("在运行模式下移动该对象到某个点,点击Add即可以添加该点到路径中");
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField("路径关键点");
            }
            EditorGUILayout.EndHorizontal();
            if (tradeToute.KeyTradeRouteNodes == null)
            {
                tradeToute.KeyTradeRouteNodes = new System.Collections.Generic.List <UnityEngine.Vector3> ();
            }
            for (int i = 0; i < tradeToute.KeyTradeRouteNodes.Count; i++)
            {
                Vector3 keyPoint = tradeToute.KeyTradeRouteNodes [i];
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.Vector3Field("Key Point." + i, keyPoint);
                    if (GUILayout.Button("Location"))
                    {
                        tradeToute.transform.position = keyPoint;
                        return;
                    }
                    if (GUILayout.Button("Delecte"))
                    {
                        tradeToute.KeyTradeRouteNodes.RemoveAt(i);
                        tradeToute.Update();
                        return;
                    }
                }
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.BeginHorizontal();
            {
                newPoint  = EditorGUILayout.Vector3Field("New Key Point", tradeToute.transform.position);
                GUI.color = Color.green;
                if (GUILayout.Button("Add"))
                {
                    tradeToute.KeyTradeRouteNodes.Add(newPoint);
                    tradeToute.Update();
                }
                GUI.color = Color.white;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            {
                if (GUILayout.Button("Apply"))
                {
                }
            }
            EditorGUILayout.EndHorizontal();
        }
    }