コード例 #1
0
        // Update is called once per frame
        void LateUpdate()
        {
            if (unit == null)
            {
                if (thisObj.activeInHierarchy)
                {
                    thisObj.SetActive(false);
                }
                return;
            }


            if (unit.IsDestroyed() || (unit.HP >= unit.GetFullHP() && unit.shield >= unit.GetFullShield()))
            {
                UIUnitOverlayManager.RemoveUnit(unit);
                unit = null;
                thisObj.SetActive(false);
                return;
            }


            if (!thisObj.activeInHierarchy)
            {
                return;
            }

            Vector3 screenPos = Camera.main.WorldToScreenPoint(unit.thisT.position + new Vector3(0, 1, 0));

            screenPos.z         = 0;
            rectT.localPosition = screenPos * UIMainControl.GetScaleFactor();

            sliderHP.value     = (unit.HP / unit.GetFullHP());
            sliderShield.value = (unit.shield / unit.GetFullShield());
        }
コード例 #2
0
ファイル: Unit.cs プロジェクト: ledotrong/tower-defense
        protected IEnumerator SupportRoutine()
        {
            supportRoutineRunning = true;

            StartCoroutine(SupportEffectRoutine());

            while (true)
            {
                yield return(new WaitForSeconds(0.1f));

                if (!destroyed)
                {
                    List <Unit> tgtList = new List <Unit>();
                    Collider[]  cols    = Physics.OverlapSphere(thisT.position, GetRange(), maskTarget);
                    if (cols.Length > 0)
                    {
                        for (int i = 0; i < cols.Length; i++)
                        {
                            Unit unit = cols[i].gameObject.GetComponent <Unit>();
                            if (!unit.IsDestroyed())
                            {
                                tgtList.Add(unit);
                            }
                        }
                    }

                    for (int i = 0; i < buffedUnit.Count; i++)
                    {
                        Unit unit = buffedUnit[i];
                        if (unit == null || unit.IsDestroyed())
                        {
                            buffedUnit.RemoveAt(i); i -= 1;
                        }
                        else if (!tgtList.Contains(unit))
                        {
                            unit.UnBuff(GetBuff());
                            buffedUnit.RemoveAt(i); i -= 1;
                        }
                    }

                    for (int i = 0; i < tgtList.Count; i++)
                    {
                        Unit unit = tgtList[i];
                        if (!buffedUnit.Contains(unit))
                        {
                            unit.Buff(GetBuff());
                            buffedUnit.Add(unit);
                        }
                    }
                }
            }
        }
コード例 #3
0
        void FPSHit(Unit hitUnit, Vector3 hitPoint)
        {
            if (attInstance.srcWeapon.GetAOERange() > 0)
            {
                LayerMask mask1 = 1 << TDTK.GetLayerCreep();
                LayerMask mask2 = 1 << TDTK.GetLayerCreepF();
                LayerMask mask  = mask1 | mask2;

                Collider[] cols = Physics.OverlapSphere(hitPoint, attInstance.srcWeapon.GetAOERange(), mask);
                if (cols.Length > 0)
                {
                    List <Unit> tgtList = new List <Unit>();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        Unit unit = cols[i].gameObject.GetComponent <Unit>();
                        if (!unit.IsDestroyed())
                        {
                            tgtList.Add(unit);
                        }
                    }
                    if (tgtList.Count > 0)
                    {
                        for (int i = 0; i < tgtList.Count; i++)
                        {
                            AttackInstance attInst = new AttackInstance();
                            attInst.srcWeapon = attInstance.srcWeapon;
                            attInst.tgtUnit   = tgtList[i];
                            attInst.Process();
                            tgtList[i].ApplyEffect(attInst);
                        }
                    }
                }
            }
            else
            {
                if (hitUnit != null && hitUnit.IsCreep())
                {
                    attInstance.tgtUnit = hitUnit;
                    attInstance.Process();
                    hitUnit.ApplyEffect(attInstance);
                }
            }
        }
コード例 #4
0
        public static List <Unit> GetUnitInRange(Vector3 pos, float range, float minRange, LayerMask mask)
        {
            List <Unit> unitList = new List <Unit>();

            Collider[] cols = Physics.OverlapSphere(pos, range, mask);
            for (int i = 0; i < cols.Length; i++)
            {
                Unit unit = cols[i].GetComponent <Unit>();
                if (unit == null)
                {
                    continue;
                }
                if (unit.IsDestroyed())
                {
                    continue;
                }
                if (Vector3.Distance(pos, unit.thisT.position) > range)
                {
                    continue;
                }

                unitList.Add(unit);
            }

            if (minRange > 0)
            {
                for (int i = 0; i < unitList.Count; i++)
                {
                    if (Vector3.Distance(pos, unitList[i].thisT.position) > minRange)
                    {
                        continue;
                    }
                    unitList.RemoveAt(i);   i -= 1;
                }
            }

            return(unitList);
        }
コード例 #5
0
        void Hit()
        {
            hit = true;

            HitEffect(targetPos);

            //thisT.position=targetPos;
            attInstance.impactPoint = targetPos;

            if (attInstance.srcUnit.GetAOERadius() > 0)
            {
                LayerMask mask = attInstance.srcUnit.GetTargetMask();

                Collider[] cols = Physics.OverlapSphere(targetPos, attInstance.srcUnit.GetAOERadius(), mask);
                if (cols.Length > 0)
                {
                    List <Unit> tgtList = new List <Unit>();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        Unit unit = cols[i].gameObject.GetComponent <Unit>();
                        if (!unit.IsDestroyed())
                        {
                            tgtList.Add(unit);
                        }
                    }
                    if (tgtList.Count > 0)
                    {
                        for (int i = 0; i < tgtList.Count; i++)
                        {
                            if (tgtList[i] == target)
                            {
                                target.ApplyEffect(attInstance);
                                if (attInstance.destroy)
                                {
                                    DestroyEffect(targetPos);
                                }
                            }
                            else
                            {
                                AttackInstance attInst = new AttackInstance();
                                attInst.srcUnit = attInstance.srcUnit;
                                attInst.tgtUnit = tgtList[i];
                                attInst.Process();
                                tgtList[i].ApplyEffect(attInst);

                                if (attInst.destroy)
                                {
                                    DestroyEffect(tgtList[i].thisT.position);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (target != null)
                {
                    target.ApplyEffect(attInstance);
                }

                if (attInstance.destroy)
                {
                    DestroyEffect(targetPos);
                }
            }

            ObjectPoolManager.Unspawn(thisObj);
        }
コード例 #6
0
ファイル: Unit.cs プロジェクト: ledotrong/tower-defense
        void ScanForTarget()
        {
            if (destroyed || IsInConstruction() || stunned)
            {
                return;
            }

            //creeps changes direction so the scan direction for creep needs to be update
            if (directionalTargeting)
            {
                if (IsCreep())
                {
                    dirScanRot = thisT.rotation;
                }
                else
                {
                    dirScanRot = thisT.rotation * Quaternion.Euler(0f, dirScanAngle, 0f);
                }
            }

            if (directionalTargeting && scanDirT != null)
            {
                scanDirT.rotation = dirScanRot;
            }

            if (target == null)
            {
                List <Unit> tgtList = TDTK.GetUnitInRange(thisT.position, GetRange(), GetRangeMin(), maskTarget);

                if (tgtList.Count > 0 && directionalTargeting)
                {
                    List <Unit> filtered = new List <Unit>();
                    for (int i = 0; i < tgtList.Count; i++)
                    {
                        Quaternion currentRot = Quaternion.LookRotation(tgtList[i].thisT.position - thisT.position);
                        if (Quaternion.Angle(dirScanRot, currentRot) <= dirScanFOV * 0.5f)
                        {
                            filtered.Add(tgtList[i]);
                        }
                    }
                    tgtList = filtered;
                }

                //to prevent unit target through wall/obstacle
                //for(int i=0; i<tgtList.Count; i++){
                //	LayerMask maskUnit=1<<LayerManager.LayerCreep() | 1<<LayerManager.LayerCreepF() | 1<<LayerManager.LayerTower();
                //	if(Physics.Linecast(thisT.position, tgtList[i].thisT.position, ~maskUnit)){
                //		tgtList.RemoveAt (i);
                //		i-=1;
                //	}
                //}

                if (tgtList.Count > 0)
                {
                    if (targetPriority == _TargetPriority.Random)
                    {
                        target = tgtList[Random.Range(0, tgtList.Count - 1)];
                    }
                    else if (targetPriority == _TargetPriority.Nearest)
                    {
                        float nearest = Mathf.Infinity;
                        for (int i = 0; i < tgtList.Count; i++)
                        {
                            float dist = Vector3.Distance(thisT.position, tgtList[i].thisT.position);
                            if (dist < nearest)
                            {
                                nearest = dist;
                                target  = tgtList[i];
                            }
                        }
                    }
                    else if (targetPriority == _TargetPriority.Weakest)
                    {
                        float lowest = Mathf.Infinity;
                        for (int i = 0; i < tgtList.Count; i++)
                        {
                            if (tgtList[i].HP < lowest)
                            {
                                lowest = tgtList[i].HP;
                                target = tgtList[i];
                            }
                        }
                    }
                    else if (targetPriority == _TargetPriority.Toughest)
                    {
                        float highest = 0;
                        for (int i = 0; i < tgtList.Count; i++)
                        {
                            if (tgtList[i].HP > highest)
                            {
                                highest = tgtList[i].HP;
                                target  = tgtList[i];
                            }
                        }
                    }
                    else if (targetPriority == _TargetPriority.First)
                    {
                        target = tgtList[Random.Range(0, tgtList.Count - 1)];
                        float lowest = Mathf.Infinity;
                        for (int i = 0; i < tgtList.Count; i++)
                        {
                            if (tgtList[i].GetDistFromDestination() < lowest)
                            {
                                lowest = tgtList[i].GetDistFromDestination();
                                target = tgtList[i];
                            }
                        }
                    }
                }

                targetInLOS = false;
            }
            else
            {
                float dist = Vector3.Distance(thisT.position, target.thisT.position);
                if (target.IsDestroyed() || dist > GetRange())
                {
                    target = null;
                }

                if (target != null && directionalTargeting)
                {
                    Quaternion tgtRotation = Quaternion.LookRotation(target.thisT.position - thisT.position);
                    if (Quaternion.Angle(dirScanRot, tgtRotation) >= dirScanFOV * 0.6f)
                    {
                        target = null;
                    }
                }
            }
        }
コード例 #7
0
ファイル: Unit.cs プロジェクト: asd540578/TD_Project
        public void ScanForTarget()
        {
            if (attackTarget != null)
            {
                if (attackTarget.IsDestroyed())
                {
                    attackTarget = null;
                }
                else
                {
                    float dist = Vector3.Distance(GetPos(), attackTarget.GetPos());
                    if (dist > GetDetectionRange(attackTarget))
                    {
                        attackTarget = null;
                    }
                    else
                    {
                        return;
                    }
                }
            }

            if (CreepIsOnAttackCD())
            {
                return;                                 //for creep only
            }
            //if(cooldownAttack>0) return;

            List <Unit> unitList = null;

            if (IsTower())
            {
                unitList = SpawnManager.GetUnitsWithinRange(this, GetAttackRange(), GetTargetGroup());
            }
            else
            {
                unitList = TowerManager.GetUnitsWithinRange(this, GetAttackRange());
            }

            if (targetingFov > 0 && targetingFov < 360)
            {
                Quaternion curDir = thisT.rotation * Quaternion.Euler(0, targetingDir, 0);
                for (int i = 0; i < unitList.Count; i++)
                {
                    Quaternion dirToTarget = Quaternion.LookRotation(unitList[i].GetPos() - GetPos());
                    if (Quaternion.Angle(curDir, dirToTarget) > targetingFov * 0.5f)
                    {
                        unitList.RemoveAt(i);      i -= 1;
                    }
                }
            }

            if (unitList.Count <= 0)
            {
                return;
            }

            if (IsCreep() && targetMode == _TargetMode.NearestToDestination)
            {
                targetMode = _TargetMode.Random;
            }

            int newTargetIdx = -1;

            if (unitList.Count == 1)
            {
                newTargetIdx = 0;
            }
            else if (targetMode == _TargetMode.Random)
            {
                newTargetIdx = Random.Range(0, unitList.Count);
            }
            else if (targetMode == _TargetMode.NearestToSelf)
            {
                float nearest = Mathf.Infinity;
                for (int i = 0; i < unitList.Count; i++)
                {
                    float dist = Vector3.Distance(GetPos(), unitList[i].GetPos());
                    if (dist < nearest)
                    {
                        newTargetIdx = i; nearest = dist;
                    }
                }
            }
            else if (targetMode == _TargetMode.MostHP)
            {
                float mostHP = 0;
                for (int i = 0; i < unitList.Count; i++)
                {
                    if (unitList[i].hp + unitList[i].sh > mostHP)
                    {
                        newTargetIdx = i; mostHP = unitList[i].hp + unitList[i].sh;
                    }
                }
            }
            else if (targetMode == _TargetMode.LeastHP)
            {
                float leastHP = Mathf.Infinity;
                for (int i = 0; i < unitList.Count; i++)
                {
                    if (unitList[i].hp + unitList[i].sh < leastHP)
                    {
                        newTargetIdx = i; leastHP = unitList[i].hp + unitList[i].sh;
                    }
                }
            }
            else if (targetMode == _TargetMode.NearestToDestination)
            {
                float pathDist = Mathf.Infinity; int furthestWP = 0; int furthestSubWP = 0; float distToDest = Mathf.Infinity;
                for (int i = 0; i < unitList.Count; i++)
                {
                    float pDist         = unitList[i].GetPathDist();
                    int   wpIdx         = unitList[i].GetWPIdx();
                    int   subWpIdx      = unitList[i].GetSubWPIdx();
                    float tgtDistToDest = unitList[i].GetDistToTargetPos();

                    if (pDist < pathDist)
                    {
                        newTargetIdx = i; pathDist = pDist; furthestWP = wpIdx; furthestSubWP = subWpIdx; distToDest = tgtDistToDest;
                    }
                    else if (pDist == pathDist)
                    {
                        if (furthestWP < wpIdx)
                        {
                            newTargetIdx = i; pathDist = pDist; furthestWP = wpIdx; furthestSubWP = subWpIdx; distToDest = tgtDistToDest;
                        }
                        else if (furthestWP == wpIdx)
                        {
                            if (furthestSubWP < subWpIdx)
                            {
                                newTargetIdx = i; pathDist = pDist; furthestWP = wpIdx; furthestSubWP = subWpIdx; distToDest = tgtDistToDest;
                            }
                            else if (furthestSubWP == subWpIdx && tgtDistToDest < distToDest)
                            {
                                newTargetIdx = i; pathDist = pDist; furthestWP = wpIdx; furthestSubWP = subWpIdx; distToDest = tgtDistToDest;
                            }
                        }
                    }
                }
            }


            if (newTargetIdx >= 0)
            {
                attackTarget = unitList[newTargetIdx];
                if (snapAiming)
                {
                    Aim();
                }
            }
        }