Пример #1
0
        public override UCLI_Path GetRandomPath(int seed)
        {
            if (m_RecordSeeds == null || m_RecordSeeds.Count == 0)
            {
                return(GenRandomPath(seed));
            }

            var rnd = new UCL_Random(seed);

            return(GenRandomPath(m_RecordSeeds[rnd.Next(m_RecordSeeds.Count)]));
        }
Пример #2
0
        protected UCLI_Path GenRandomPath(int seed)
        {
            if (m_StartPosMin == null || m_StartPosMax == null)
            {
                return(null);
            }
            List <Vector3> points = new List <Vector3>();

            points.Clear();
            float path_len = 0;
            var   rnd      = new UCL_Random(seed);
            var   size     = m_Size;
            bool  inv_x    = false;
            bool  inv_y    = false;

            if (size.x < 0)
            {
                inv_x  = true;
                size.x = -size.x;
            }
            if (size.y < 0)
            {
                inv_y  = true;
                size.y = -size.y;
            }
            float hx = 0.5f * size.x;
            float hy = 0.5f * size.y;
            var   sp = rnd.OnRect(size.x, size.y);

            Vector2 vel = Vector2.zero;

            if (sp.x == 0)
            {
                if (sp.y > hy)
                {
                    vel = rnd.OnUnitCircle(-0.2f * Mathf.PI, 0);
                }
                else
                {
                    vel = rnd.OnUnitCircle(0, 0.2f * Mathf.PI);
                }
            }
            else if (sp.x == size.x)
            {
                if (sp.y > hy)
                {
                    vel = rnd.OnUnitCircle(1f * Mathf.PI, 1.2f * Mathf.PI);
                }
                else
                {
                    vel = rnd.OnUnitCircle(0.8f * Mathf.PI, 1f * Mathf.PI);
                }
            }
            else if (sp.y == 0)
            {
                if (sp.x > hx)
                {
                    vel = rnd.OnUnitCircle(0.5f * Mathf.PI, 0.7f * Mathf.PI);
                }
                else
                {
                    vel = rnd.OnUnitCircle(0.3f * Mathf.PI, 0.5f * Mathf.PI);
                }
            }
            else
            {
                if (sp.x > hx)
                {
                    vel = rnd.OnUnitCircle(1.3f * Mathf.PI, 1.5f * Mathf.PI);
                }
                else
                {
                    vel = rnd.OnUnitCircle(1.5f * Mathf.PI, 1.7f * Mathf.PI);
                }
            }
            Vector2 pos = sp;

            System.Func <Vector2, Vector3> ToWorldSpace = null;
            if (inv_x && inv_y)
            {
                ToWorldSpace =
                    (o) => {
                    o.x = -o.x;
                    o.y = -o.y;
                    return(m_StartPosMin.TransformPoint(o));
                };
            }
            else if (inv_x)
            {
                ToWorldSpace =
                    (o) => {
                    o.x = -o.x;
                    return(m_StartPosMin.TransformPoint(o));
                };
            }
            else if (inv_y)
            {
                ToWorldSpace =
                    (o) => {
                    o.y = -o.y;
                    return(m_StartPosMin.TransformPoint(o));
                };
            }
            else
            {
                ToWorldSpace =
                    (o) => {
                    return(m_StartPosMin.TransformPoint(o));
                };
            }

            float angle = m_StartPosMin.rotation.eulerAngles.z;

            Vector3 PrevPos = ToWorldSpace(pos);
            float   acc     = m_MoveData.m_Acc;// * (1f/m_StartPosMin.lossyScale.x);

            vel *= acc;
#if PathDebug
            Debug.LogWarning("m_Size:" + this.m_Size + ",sp:" + sp + ",vel:" + vel);
#endif
            points.Add(PrevPos);
            int   swing_loop      = m_MoveData.m_SwingLoop;
            int   loop            = swing_loop * 2 + 1;
            float rr              = ((m_MoveData.m_SwingAngle * Mathf.Deg2Rad) / swing_loop);
            float start_r         = vel.Radius();
            int   start_random_at = m_MoveData.m_StartRandomAt;
            int   record_interval = m_MoveData.m_RecordInterval;
            var   vel_dec         = m_MoveData.m_VelDec;
            for (int i = 0; i < m_MaxMoveTimes; i++)
            {
                if (i > start_random_at)
                {
                    vel *= vel_dec;
                    float r  = vel.Radius();
                    float dr = r - start_r;
                    r += (i % loop - swing_loop) * rr;
                    Vector2 acc_vec = Vector2.zero;
                    if (dr > 0 && dr < Mathf.PI)
                    {
                        acc_vec = rnd.OnUnitCircle(r - m_MoveData.m_AngelDel * Mathf.Deg2Rad,
                                                   r + m_MoveData.m_Inertia * m_MoveData.m_AngelDel * Mathf.Deg2Rad);
                    }
                    else
                    {
                        acc_vec = rnd.OnUnitCircle(r - m_MoveData.m_Inertia * m_MoveData.m_AngelDel * Mathf.Deg2Rad,
                                                   r + m_MoveData.m_AngelDel * Mathf.Deg2Rad);
                    }
                    vel += acc_vec * acc;
                }
                pos += vel;

                if (pos.x < 0 || pos.x > size.x ||
                    pos.y < 0 || pos.y > size.y)
                {
                    break;
                }
                if (i % record_interval == 0)
                {
                    var   cur_pos = ToWorldSpace(pos);
                    float len     = (cur_pos - PrevPos).magnitude;
                    path_len += len;
                    points.Add(cur_pos);
                    PrevPos = cur_pos;
                }
            }
            {
                var cur_pos = ToWorldSpace(pos);
                path_len += (cur_pos - PrevPos).magnitude;
                points.Add(cur_pos);
            }
#if PathDebug
            for (int i = 0; i < points.Count; i++)
            {
                Debug.LogWarning("" + i + ":" + points[i]);
            }
#endif
            var path = new CurvePath(points, path_len);
            return(path);
        }