Exemple #1
0
        public void MoveTo(UnitCtl fromCtl, UnitCtl toCtl)
        {
            fromUnitData = fromCtl.unitData;
            toUnitData   = toCtl.unitData;
            this.from    = fromCtl.transform;
            this.to      = toCtl.transform;
            navMeshAgent.SetDestination(to.position);
            Vector3 fromPoint = from.position;
            Vector3 toPoint   = to.position;


            Vector3 direction = (toPoint - fromPoint).normalized * beginDistance;

            float radian = HMath.radian(0, 0, direction.x, direction.z);

            radian += Mathf.PI * 0.5F;


            direction += fromPoint;
            float x = HMath.radianPointX(radian, gap * index, direction.x);
            float z = HMath.radianPointY(radian, gap * index, direction.z);

            begionTarget.x = x;
            begionTarget.z = z;

            direction = (toPoint - fromPoint).normalized * endDistance;
            radian    = HMath.radian(0, 0, direction.x, direction.z);
            radian   += Mathf.PI * 0.5F;

            direction = toPoint - direction;
            x         = HMath.radianPointX(radian, gap * index, direction.x);
            z         = HMath.radianPointY(radian, gap * index, direction.z);

            endTarget.x = x;
            endTarget.z = z;

            Out();
        }
        public void SetPath(List <Vector3> paths)
        {
            List <Vector3> list = new List <Vector3>();

            if (paths.Count >= 2)
            {
                Vector3 p1       = paths[0];
                Vector3 p2       = paths[1];
                float   distance = Vector3.Distance(p1, p2);
                Vector3 p3       = Vector3.zero;
                int     add      = 0;
                bool    begin    = false;
                bool    end      = false;
                if (distance > 1F)
                {
                    p3.x  = HMath.directionPointX(p1.x, p1.z, p2.x, p2.z, lengthRadius);
                    p3.z  = HMath.directionPointY(p1.x, p1.z, p2.x, p2.z, lengthRadius);
                    add  += 1;
                    begin = true;
                }

                p1       = paths[paths.Count - 1];
                p2       = paths[paths.Count - 2];
                distance = Vector3.Distance(p1, p2);
                Vector3 p4 = Vector3.zero;
                if (distance > 1F)
                {
                    p4.x = HMath.directionPointX(p1.x, p1.z, p2.x, p2.z, lengthRadius);
                    p4.z = HMath.directionPointY(p1.x, p1.z, p2.x, p2.z, lengthRadius);
                    add += 1;
                    end  = true;
                }


                for (int i = 0; i < paths.Count; i++)
                {
                    list.Add(paths[i]);
                    if (add > 0)
                    {
                        if (i == 0 && begin)
                        {
                            list.Add(p3);
                        }

                        if (i == paths.Count - 2 && end)
                        {
                            list.Add(p4);
                        }
                    }
                }
            }


            for (int i = 0; i < list.Count; i++)
            {
                Vector3 source = list[i];
                Vector3 pos    = new Vector3(source.x, source.y, source.z);

                if (i != 0 && i != list.Count - 1)
                {
                    float myGap = gap;
//					if(i > 1 && i < list.Count - 2)
//					{
//						myGap = turnGap;
//					}

                    if (list.Count > 4 && i < list.Count - 1)
                    {
                        myGap = turnGap;
                    }

                    Vector3 d1     = pos - list[i - 1];
                    Vector3 d2     = list[i + 1] - pos;
                    Vector3 d      = d1.normalized + d2.normalized;
                    float   radian = HMath.radian(0, 0, d.x, d.z);
                    radian += Mathf.PI * 0.5F;

                    float x = HMath.radianPointX(radian, myGap * index, pos.x);
                    float z = HMath.radianPointY(radian, myGap * index, pos.z);
                    pos.x = x;
                    pos.z = z;
                }
                this.list.Add(pos);
            }
        }
        /** 检测建筑障碍 */
        public void CheckBuild()
        {
            if (paths.Count == 0)
            {
                return;
            }

            int listCount = paths[0].list.Count;

            for (int i = 0; i < listCount; i++)
            {
                bool       hasHit         = false;
                float      minHitDistance = 9999;
                RaycastHit hitInfo        = new RaycastHit();

                float   radius = 1f;
                Vector3 pos    = Vector3.zero;

                // 查找最近的碰撞体
                foreach (Path path in paths)
                {
                    List <Vector3> list   = path.list;
                    List <Vector3> points = path.points;
                    path.hit = null;

                    points.Add(list[i]);
                    if (i == 0)
                    {
                        continue;
                    }



                    if (i < listCount - 2)
                    {
                        Vector3 p1 = list[i];
                        Vector3 p2 = list[i + 1];


                        RaycastHit _hitInfo;
                        if (Physics.Linecast(p1, p2, out _hitInfo, buildLayer.value))
                        {
                            path.hit = _hitInfo.collider.gameObject;
                            pos      = _hitInfo.collider.transform.position;
                            float hitDistance = Vector3.Distance(paths[0].list[i], pos);

                            radius = (_hitInfo.collider as SphereCollider).radius * _hitInfo.collider.transform.lossyScale.x;
                            if (hitDistance <= radius * hitRadiusMultiply || Vector3.Distance(paths[0].list[i + 1], pos) <= radius * hitRadiusMultiply)
                            {
                                continue;
                            }

                            if (hasHit == false)
                            {
                                hasHit         = true;
                                hitInfo        = _hitInfo;
                                minHitDistance = hitDistance;
                            }
                            else if (hitDistance <= minHitDistance)
                            {
                                hitInfo        = _hitInfo;
                                minHitDistance = hitDistance;
                            }
                        }
                    }
                }


                int hL = -100;
                int hR = 100;

                //查找hL、hR
                if (hasHit)
                {
                    radius = (hitInfo.collider as SphereCollider).radius * hitInfo.collider.transform.lossyScale.x;
                    pos    = hitInfo.collider.transform.position;

                    foreach (Path path in paths)
                    {
                        if (path.hit == null)
                        {
                            continue;
                        }
                        if (path.hit != hitInfo.collider.gameObject)
                        {
                            continue;
                        }

                        int index   = path.index;
                        int myIndex = index;


                        List <Vector3> list   = path.list;
                        List <Vector3> points = path.points;


                        Vector3 p1 = list[i];
                        Vector3 p2 = list[i + 1];

                        Vector3 d0    = p2 - p1;
                        Vector3 d1    = pos - p1;
                        Vector3 cross = Vector3.Cross(d1, d0);

                        if (cross.y < 0)
                        {
                            if (myIndex <= hR)
                            {
                                hR = myIndex;
                            }
                        }
                        else
                        {
                            if (myIndex >= hL)
                            {
                                hL = myIndex;
                            }
                        }
                    }
                }

                if (hasHit)
                {
                    if (hR == 100)
                    {
                        hR = hL + 1;
                    }
                    if (hL == -100)
                    {
                        hL = hR - 1;
                    }

//					Debug.Log("hL=" + hL + "  hR=" + hR);
                    foreach (Path path in paths)
                    {
                        int index   = path.index;
                        int myIndex = index;

                        List <Vector3> list   = path.list;
                        List <Vector3> points = path.points;


                        Vector3 p1 = list[i];
                        Vector3 p2 = list[i + 1];

                        int symbol   = 1;
                        int gapIndex = 0;
                        if (myIndex <= hL)
                        {
                            gapIndex = myIndex - hL - 1;
                            symbol   = -1;
                        }
                        else
                        {
                            gapIndex = myIndex - hR + 1;
                            symbol   = 1;
                        }


                        Vector3 direction = (p2 - p1).normalized;
                        float   radian    = HMath.radian(0, 0, direction.x, direction.z);
                        radian += Mathf.PI * 0.5F;

                        Vector3 ip = HMath.IntersectionPoint(pos, p1, p2);
                        float   r  = radius * symbol + gap * gapIndex;


//						Debug.Log("index="+index + "  gapIndex=" + gapIndex + " r=" + r);


                        Vector3 p;

                        if (Vector3.Distance(ip, p1) > radius * hitRadiusMultiply * hitRadiusMultiplyVertical)
                        {
                            p = ip - direction * radius * hitRadiusMultiply * hitRadiusMultiplyVertical;
                            points.Add(p);
                        }

                        p   = Vector3.zero;
                        p.x = HMath.radianPointX(radian, r, pos.x);
                        p.z = HMath.radianPointY(radian, r, pos.z);
                        points.Add(p);

                        if (Vector3.Distance(ip, p2) > radius * hitRadiusMultiply * hitRadiusMultiplyVertical)
                        {
                            p = ip + direction * radius * hitRadiusMultiply * hitRadiusMultiplyVertical;
                            points.Add(p);
                            list.Insert(i + 1, p);
                        }
                        else
                        {
                            list.Insert(i + 1, p2);
                        }
                    }


                    listCount = paths[0].list.Count;
                }
            }
        }