コード例 #1
0
ファイル: BUnit.cs プロジェクト: BlankFactor/Simulator-02
    /// <summary>
    /// 收集可攻击目标坐标
    /// </summary>
    virtual public void ExtendAttackableGridUnit(int _index, int _count = 0)
    {
        // 不受理状态 当前位置不为空闲状态且非初始位置 超出拓展限制
        if (_index != currentPos && bfm.GetObstacleStatuByIndex(_index) || _count > attackDistance)
        {
            return;
        }

        // 加入到有效网格队列(忽略当前位置)
        if (bfm.GetUnitStatuByIndex(_index) && _index != currentPos && !IncludeGridUnit(_index) && campNumber != bfm.GetUnitCampNumber(_index))
        {
            AddGridUnit(_index);
        }

        Vector2 xy = bfm.GetRowColumnByIndex(_index);

        // 6向延展
        for (int dir = 0; dir < 6; dir++)
        {
            // 生成延展方向 计算下一延展坐标 从左上角开始顺时针 0->5
            Vector2 nextIndex = Vector2.zero;
            int     factor    = (int)xy.x % 2;
            switch (dir)
            {
            case 0:
            {
                nextIndex.x = xy.x + 1;
                nextIndex.y = xy.y + (1 - Mathf.Pow(2, factor));
                break;
            }

            case 1:
            {
                nextIndex.x = xy.x + 1;
                nextIndex.y = xy.y + (2 - Mathf.Pow(2, factor));
                break;
            }

            case 2:
            {
                nextIndex.x = xy.x;
                nextIndex.y = xy.y + 1;
                break;
            }

            case 3:
            {
                nextIndex.x = xy.x - 1;
                nextIndex.y = xy.y + (2 - Mathf.Pow(2, factor));
                break;
            }

            case 4:
            {
                nextIndex.x = xy.x - 1;
                nextIndex.y = xy.y + (1 - Mathf.Pow(2, factor));
                break;
            }

            case 5:
            {
                nextIndex.x = xy.x;
                nextIndex.y = xy.y - 1;
                break;
            }

            default: break;
            }

            // 检测范围合法性
            if (nextIndex.x < 0 || nextIndex.x >= bfm.GetMapHeight() || nextIndex.y < 0 || nextIndex.y >= bfm.GetMapWidth())
            {
                return;
            }
            else
            {
                ExtendAttackableGridUnit(bfm.GetIndexByRowColumn(nextIndex), _count + 1);
            }
        }
    }