Esempio n. 1
0
    IEnumerator ScanForTarget()
    {
        LayerMask maskTarget = 1 << LayerManager.LayerTower();

        while (true)
        {
            if (target == null)
            {
                if (unit.HasStoppedMoving())
                {
                    unit.ResumeAnimation();
                    unit.ResumeMoving();
                }

                if (attackStrategicPointOnly)
                {
                    List <Unit> list = GameControl.GetLifeUnit();

                    foreach (Unit targetUnit in list)
                    {
                        if (targetUnit != null && targetUnit.HPAttribute.HP > 0)
                        {
                            if (Vector3.Distance(unit.thisT.position, targetUnit.GetTargetT().position) <= range)
                            {
                                target = targetUnit;

                                if (attackMode == _AttackMode.StopNAttack)
                                {
                                    if (attackMethod != _AttackMethod.Melee)
                                    {
                                        unit.StopAnimation();
                                    }
                                    unit.StopMoving();
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (targetArea == _TargetingA.AllAround)
                    {
                        Collider[] cols = Physics.OverlapSphere(unit.thisT.position, range, maskTarget);
                        //if(cols!=null && cols.Length>0) Debug.Log(cols[0]);

                        if (cols.Length > 0)
                        {
                            Collider currentCollider = cols[Random.Range(0, cols.Length)];
                            Unit     targetTemp      = currentCollider.gameObject.GetComponent <Unit>();
                            if (targetTemp != null && targetTemp.HPAttribute.HP > 0)
                            {
                                target = targetTemp;

                                if (attackMode == _AttackMode.StopNAttack)
                                {
                                    if (attackMethod != _AttackMethod.Melee)
                                    {
                                        unit.StopAnimation();
                                    }
                                    unit.StopMoving();
                                    //if(dash){
                                    //	if(Vector3.Distance(unit.thisT.position, target.thisT.position)>8){
                                    //		//unit.PlayDash();
                                    //	}
                                    //}
                                }
                            }
                        }
                    }
                    else if (targetArea == _TargetingA.FrontalCone)
                    {
                        Collider[] cols = Physics.OverlapSphere(unit.thisT.position, range, maskTarget);
                        //if(cols!=null && cols.Length>0) Debug.Log(cols[0]);

                        if (cols.Length > 0)
                        {
                            Collider currentCollider = cols[0];
                            foreach (Collider col in cols)
                            {
                                Quaternion targetRot = Quaternion.LookRotation(col.transform.position - unit.thisT.position);
                                if (Quaternion.Angle(targetRot, unit.thisT.rotation) < frontalConeAngle)
                                {
                                    Unit targetTemp = currentCollider.gameObject.GetComponent <Unit>();
                                    if (targetTemp != null && targetTemp.HPAttribute.HP > 0)
                                    {
                                        target = targetTemp;
                                        if (attackMode == _AttackMode.StopNAttack)
                                        {
                                            if (attackMethod != _AttackMethod.Melee)
                                            {
                                                unit.StopAnimation();
                                            }
                                            unit.StopMoving();
                                            //if(dash){
                                            //	if(Vector3.Distance(unit.thisT.position, target.thisT.position)>8){
                                            //		//unit.PlayDash();
                                            //	}
                                            //}
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if (targetArea == _TargetingA.Obstacle)
                    {
                        RaycastHit hit;
                        if (Physics.Raycast(unit.thisT.position, unit.thisT.forward, out hit, range, maskTarget))
                        {
                            Unit targetTemp = hit.collider.gameObject.GetComponent <Unit>();
                            if (targetTemp != null && targetTemp.HPAttribute.HP > 0)
                            {
                                target = targetTemp;
                                if (attackMode == _AttackMode.StopNAttack)
                                {
                                    if (attackMethod != _AttackMethod.Melee)
                                    {
                                        unit.StopAnimation();
                                    }
                                    unit.StopMoving();
                                    //if(dash){
                                    //	if(Vector3.Distance(unit.thisT.position, target.thisT.position)>8){
                                    //		//unit.PlayDash();
                                    //	}
                                    //}
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                //if target is out of range or dead or inactive, clear target
                currentTargetDist = Vector3.Distance(unit.thisT.position, target.GetTargetT().position);
                                #if UNITY_4_0
                if (currentTargetDist > range * 1.25f || target.HPAttribute.HP <= 0 || !target.thisObj.activeInHierarchy)
                {
                                #else
                if (currentTargetDist > range * 1.25f || target.HPAttribute.HP <= 0 || !target.thisObj.active)
                {
                                #endif
                    target = null;
                    if (attackMode == _AttackMode.StopNAttack)
                    {
                        unit.ResumeAnimation();
                        unit.ResumeMoving();
                        //Debug.Log("target cleared");
                    }
                    if (meleeState == _MeleeState.Attacking)
                    {
                        meleeState = _MeleeState.OutOfRange;
                    }
                }
            }
            yield return(null);
        }
    }