/// <summary>
    /// 得到指定距离可用攻击位
    /// </summary>
    /// <param name="attackDist_">攻击距离</param>
    private Vector3 OnGetAttackSlot(attackRoleInfo attackInfo_)
    {
        // 填充攻击者
        if (attackInfoNumDict.ContainsKey(attackInfo_.attackDist))
        {
            attackInfoNumDict[attackInfo_.attackDist] += 1;
        }
        else
        {
            attackInfoNumDict.Add(attackInfo_.attackDist, 1);
        }

        // 找到有没有当前攻击距离的攻击槽信息
        List <slotInfo> nowSlotList = OnGetAttckSlots(attackInfo_.attackDist);
        //if (slotDict.ContainsKey(attackInfo_.attackDist))
        //    nowSlotList = slotDict[attackInfo_.attackDist];  // 有就直接得到
        //else
        //    nowSlotList = OnGetAttckSlots(attackInfo_.attackDist); // 没有就获取

        // 得到攻击槽位
        slotInfo slotInfo = null;

        if (nowSlotList.Count > 0)
        {
            for (int j = 0; j < nowSlotList.Count; ++j)
            {
                slotInfo info = nowSlotList[j];
                if (!info.isOccupy)
                {
                    if (slotInfo == null)
                    {
                        slotInfo = info;
                    }
                    else
                    {
                        // 目标槽位距离
                        float targetDis = Vector3.Distance(attackInfo_.attackRole.position, slotInfo.attackPos);
                        // 当前槽位距离
                        float currDis = Vector3.Distance(attackInfo_.attackRole.position, info.attackPos);

                        // 找到和自己距离最短的攻击位
                        if (currDis < targetDis)
                        {
                            slotInfo = info;
                        }
                    }
                }
            }
        }

        // 设置占用
        if (slotInfo != null)
        {
            slotInfo.isOccupy = true;
        }

        return(slotInfo != null ? slotInfo.attackPos : Vector3.zero);
    }
    // <summary>
    /// 得到指定攻击距离的全部槽(攻击位)
    /// </summary>
    private List <slotInfo> OnGetAttckSlots(float attackDist_)
    {
        // 首先判断(攻击者数量)是否大于(攻击距离*默认数量)
        // 大于则生成攻击者数量的攻击位数量,小于则根据攻击距离来计算默认的攻击位数量
        float newSlotNum = attackInfoNumDict[attackDist_] > (attackDist_ * 7) ? attackInfoNumDict[attackDist_] : attackDist_ * 7;

        float existSlotNum = 0;

        if (slotDict.ContainsKey(attackDist_))
        {
            existSlotNum = slotDict[attackDist_].Count;
        }

        // 生成攻击位
        List <slotInfo> nowSlotList;
        float           slotNum = newSlotNum - existSlotNum;
        float           degrees = 360 / slotNum;

        //for (int i = 0; i < slotNum; ++i)
        //{
        //    Vector3 rolePos = targetObj.transform.position;   // 这里的targetObj在我的设计中就是攻击者或者被攻击者本身
        //    Vector3 attackDist = new Vector3(0f, 0f, attackDist_);

        //    // 得到攻击位
        //    Vector3 slotPos = rolePos + (Quaternion.Euler(new Vector3(0f, degrees * i, 0f)) * attackDist);

        //    // 创建新的攻击槽
        //    slotInfo slotItem = new slotInfo(slotPos);
        //    nowSlotList.Add(slotItem);
        //}

        Vector3 rolePos    = targetObj.transform.position; // 这里的targetObj在我的设计中就是攻击者或者被攻击者本身
        Vector3 attackDist = new Vector3(0f, 0f, attackDist_);

        // 得到攻击位
        Vector3  slotPos  = rolePos + (Quaternion.Euler(new Vector3(0f, degrees * (existSlotNum + 1), 0f)) * attackDist);
        slotInfo slotItem = new slotInfo(slotPos);

        if (slotDict.ContainsKey(attackDist_))
        {
            nowSlotList = slotDict[attackDist_];
            nowSlotList.Add(slotItem);
        }
        else
        {
            nowSlotList = new List <slotInfo>();
            nowSlotList.Add(slotItem);
            slotDict.Add(attackDist_, nowSlotList);
        }

        return(nowSlotList);
    }
Example #3
0
    /// <summary>
    /// 更新角色攻击槽(攻击位)
    /// </summary>
    /// <param name="attackCount_">攻击槽数量</param>
    /// <param name="attackDist_">攻击距离</param>
    private void OnGetAttckSlots(int attackCount_, int attackDist_)
    {
        float degrees = 180 / attackCount_;

        for (int i = 0; i < attackCount_; ++i)
        {
            Vector3 rolePos    = roleObj.transform.position;
            Vector3 attackDist = new Vector3(0f, 0f, attackDist_);

            // 得到攻击位
            Vector3 slotPos = rolePos + (Quaternion.Euler(new Vector3(0f, degrees * i, 0f)) * attackDist);

            slotInfo slotItem = null;
            if (slotDict.ContainsKey(i))
            {
                // 更新攻击槽信息
                slotItem           = slotDict[i];
                slotItem.attackPos = slotPos;
            }
            else
            {
                // 创建新的攻击槽
                slotItem    = new slotInfo(slotPos);
                slotDict[i] = slotItem;

                // 测试
                GameObject obj = Instantiate <GameObject>(debugModel);
                obj.name = "slot_" + i;
                obj.transform.SetParent(this.transform);
                obj.transform.localScale = new Vector3(1f, 1f, 1f);
                obj.SetActive(true);
                debugModelList.Add(obj);
            }
        }

        // 测试
        for (int i = 0; i < slotDict.Count; ++i)
        {
            slotInfo   info = slotDict[i];
            GameObject obj  = debugModelList[i];
            obj.transform.position = info.attackPos;
        }
    }