Exemplo n.º 1
0
    /* 当box处于孤立状态时(预下落状态),计算下落坐标 */
    private Vector2 calcDropPosition()
    {
        var currentPositionInArray = new Vector2(transform.position.x, transform.position.y);

        var currentX = (int)currentPositionInArray.x;
        var currentY = (int)currentPositionInArray.y;

        var hitX = new int[] { currentX - 1, currentX, currentX + 1 };
        //从当前行倒序遍历地图数组(世界方向向下)

        /* Beta: 待解决某些情况源物体以外下落坐标不正确的问题 */
        //使用List收集所有的停靠点,取y值最大的坐标点即为最终的停靠点
        List <Vector2> dropPositionList = new List <Vector2>();

        foreach (var box in _map.GetMap().Reverse())
        {
            //print(gameObject.name + " check " + box.Key + " " + box.Value.gameObject.name);
            if (box.Key.y < currentY && IntArrayContainElement(hitX, (int)box.Key.x))
            {
                //Debug.LogWarning(box.Value.gameObject.name+" checkInRange " + box.Value.State + " org:" + box.Key + " linked:" + box.Value._linkedPosition);
                if (box.Value.State == BoxState.Isolated)
                {
                    //Debug.LogWarning(gameObject.name + " use " + box.Value.gameObject.name + " _linkedPosition.y:" + box.Value._linkedPosition.y);
                    //return new Vector2(currentX, box.Value._linkedPosition.y + 1);
                    dropPositionList.Add(new Vector2(currentX, box.Value._linkedPosition.y + 1));
                }
                else
                {
                    //Debug.LogWarning(gameObject.name + " use " + box.Value.gameObject.name + " .y:" + box.Key.y);
                    //return new Vector2(currentX, box.Key.y + 1);
                    dropPositionList.Add(new Vector2(currentX, box.Key.y + 1));
                }
            }
        }
        /* 查找最上方的停靠点 */
        if (dropPositionList.Count != 0)
        {
            return((from pos in dropPositionList
                    orderby pos.y descending
                    select pos).ToArray()[0]);
        }
        Debug.LogWarning("dropPosition not found");
        //如果没有找到停靠点,则返回地图外最下方的坐标
        return(_nilPosition);
    }