コード例 #1
0
        void UpdateScreenPos()
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(unit.GetPos() + new Vector3(0, .5f, 0));

            screenPos.z         = 0;
            rectT.localPosition = screenPos * UI.GetScaleFactor();
        }
コード例 #2
0
        public void Show(Unit unit)
        {
            activeUnit = unit;

            refreshEveryFrame = activeUnit.IsTower() && activeUnit.GetTower().isPreview;

            thisT.position = unit.GetPos();
            CreatePoints(unit);
            thisObj.SetActive(true);
        }
コード例 #3
0
        void Update()
        {
            if (activeUnit == null)
            {
                Hide();
            }

            if (refreshEveryFrame)
            {
                thisT.position = activeUnit.GetPos();
                CreatePoints(activeUnit);
            }
        }
コード例 #4
0
ファイル: TowerManager.cs プロジェクト: asd540578/TD_Project
 public static List <Unit> GetUnitsWithinRange(Unit srcUnit, float range)
 {
     return(GetUnitsWithinRange(srcUnit.GetPos(), range));
 }
コード例 #5
0
 public TargetingInfo(Unit u)
 {
     tgtUnit = u; pos = tgtUnit.GetPos(); valid = true;
 }
コード例 #6
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();
                }
            }
        }
コード例 #7
0
ファイル: SpawnManager.cs プロジェクト: asd540578/TD_Project
 public static List <Unit> GetUnitsWithinRange(Unit srcUnit, float range, UnitTower._TargetGroup tgtGroup = UnitTower._TargetGroup.All)
 {
     return(GetUnitsWithinRange(srcUnit.GetPos(), range, tgtGroup));
 }