pointInfo CreatePointInfo(Vector3 worldPos)
    {
        // create material instance first.
        if (_chainMatInst == null)
        {
            _chainMatInst = Utils.CloneMaterial(_chainMat);
            //_chainMatInst.mainTexture = _thunderTexture.TexturePtr;
        }

        pointInfo  pi = new pointInfo();
        GameObject go = GameObject.Instantiate(_chainRes) as GameObject;

        pi.chain = go;
        pi.chain.GetComponentInChildren <Renderer>().sharedMaterial = _chainMatInst;
        pi.chainTrans        = go.transform;
        pi.chainTrans.parent = LevelManager.Singleton.BulletRoot;

        //pi.spark = GameObject.Instantiate(_spark) as GameObject;
        //pi.sparkParticle = pi.spark.GetComponent<ParticleSystem>();
        //Material newMat = new Material(_sparkMat.shader);
        //newMat.CopyPropertiesFromMaterial(_sparkMat);
        //newMat.hideFlags = HideFlags.DontSave;
        //pi.spark.gameObject.GetComponentInChildren<Renderer>().sharedMaterial = newMat;
        //pi.sparkMat = newMat;
        //pi.sparkTrans = pi.spark.transform;
        //pi.sparkTrans.parent = LevelManager.Singleton.BulletRoot;

        pi.worldPos = worldPos;
        pi.Deactive();
        return(pi);
    }
 void Awake()
 {
     _pointList = new System.Collections.Generic.List <pointInfo>();
     for (int i = 0; i < _chainMaxLength; ++i)
     {
         _pointList.Add(CreatePointInfo(Vector3.zero));
     }
     _head      = 0;
     _activeCnt = 0;
     _endPoint  = null;
 }
    public void Clear()
    {
        while (_activeCnt > 1)
        {
            RemoveFirst();
        }
        pointInfo pi = _pointList[_head];

        pi.Deactive();
        pi.previous = null;
        pi.next     = null;
        _activeCnt  = 0;
        _endPoint   = null;
    }
    public void UpdatePos(int idx, Vector3 pos)
    {
        pointInfo current  = _pointList[idx];
        pointInfo previous = _pointList[idx].previous;
        pointInfo next     = _pointList[idx].next;

        current.worldPos = pos;
        if (previous != null)
        {
            previous.Update(previous.worldPos, pos);
        }
        if (next != null)
        {
            current.Update(pos, next.worldPos);
        }
    }
 public void RemoveFirst()
 {
     if (_activeCnt > 0)
     {
         pointInfo pi = _pointList[_head];
         pi.Deactive();
         if (pi.previous != null)
         {
             pi.previous.next = null;
             pi.previous      = null;
         }
         if (pi.next != null)
         {
             pi.next.previous = null;
             pi.next          = null;
         }
         _head = ((_head + 1) % _pointList.Count);
         --_activeCnt;
     }
 }
    public int AddPoint(Vector3 worldPos)
    {
        // extend
        int tail = (_head + _activeCnt) % _pointList.Count;

        if (_activeCnt == _pointList.Count)
        {
            _pointList.Insert(tail, CreatePointInfo(worldPos));
        }
        pointInfo pi = _pointList[tail];

        pi.worldPos = worldPos;
        if (_endPoint != null)
        {
            _endPoint.next = pi;
            pi.previous    = _endPoint;
            _endPoint.Active(Time.timeSinceLevelLoad);
            _endPoint.Update(_endPoint.worldPos, worldPos);
        }
        _endPoint = pi;
        ++_activeCnt;

        return(tail);
    }