예제 #1
0
 public void AddToList(GameObject unit)
 {
     if (unit.GetComponent <HumanControl>())
     {
         HumanList.Add(unit);
     }
     else if (unit.GetComponent <BuilderControl>())
     {
         BuilderList.Add(unit);
     }
     //switch (typeUnit)
     //{
     //    case 0:
     //        HumanList.Add(unit.gameObject);
     //        break;
     //    case 1:
     //        //Debug.Log(unit.name);
     //        BuilderList.Add(unit);
     //        break;
     //}
 }
예제 #2
0
    public bool ChangeTargetUnit(Transform target, int typeTarget)
    {
        float shortestDistance = Mathf.Infinity;
        int   numberHuman      = -1;

        switch (typeTarget)
        {
        case 0:
            if (HumanList.Count > 0)
            {
                for (int i = 0; i < HumanList.Count; i++)
                {
                    if (HumanList[i] == null)
                    {
                        HumanList.RemoveAt(i); break;
                    }
                    float distanceToTarget = Vector3.Distance(HumanList[i].transform.position, target.transform.position);
                    if (shortestDistance >= distanceToTarget
                        &&
                        HumanList[i].GetComponent <HumanControl>().ChangeTartget == null)
                    {
                        shortestDistance = distanceToTarget;
                        numberHuman      = i;
                    }
                }
                if (numberHuman > -1)
                {
                    HumanList[numberHuman].GetComponent <HumanControl>().ChangeTartget = target;
                    return(true);
                }
            }
            return(false);

        case 1:
            if (BuilderList.Count > 0)
            {
                for (int i = 0; i < BuilderList.Count; i++)
                {
                    if (BuilderList[i] == null)
                    {
                        BuilderList.RemoveAt(i); break;
                    }
                    float distanceToTarget = Vector3.Distance(BuilderList[i].transform.position, target.transform.position);
                    if (shortestDistance >= distanceToTarget
                        &&
                        BuilderList[i].GetComponent <BuilderControl>().ChangeTartget == null)
                    {
                        shortestDistance = distanceToTarget;
                        numberHuman      = i;
                    }
                }
                if (numberHuman > -1)
                {
                    BuilderList[numberHuman].GetComponent <BuilderControl>().ChangeTartget = target;
                    return(true);
                }
            }
            return(false);
        }
        return(false);
    }