예제 #1
0
    IEnumerator CoWaitDrag(Vector3 initPos)
    {
        isReady = false;
        while (true)
        {
            if (Input.GetMouseButtonUp(0))
            {
                break;
            }
            var   mousePos = InputEx.GetMouseWorld();
            float distance = Vector3.Distance(initPos, mousePos);
            if (distance > Global.DRAG_DISTANCE)
            {
                //드래그 완료
                Direction dir         = BoardUtil.GetDirection(initPos, mousePos);
                Block     changeBlock = BlockManager.instance.GetNeighbor(selectBlock, dir);
                if (changeBlock == null)
                {
                    break;
                }
                //블록 스왑
                yield return(StartCoroutine(BlockManager.instance.CoSwapBlock(selectBlock, changeBlock)));

                var selectInfos = MatchManager.instance.Check(selectBlock);
                var targetInfos = MatchManager.instance.Check(changeBlock);
                curMatchInfos = MatchUtil.Distinct(selectInfos.Union(targetInfos).ToList());
                //스왑 실패 (Undo)
                if (curMatchInfos.Count == 0)
                {
                    Debug.Log("Swap Fail");
                    yield return(StartCoroutine(BlockManager.instance.CoUndoSwap()));

                    break;
                }
                //중력 적용 후 맵 생성 (추가 Match가 없을때까지 반복)
                while (true)
                {
                    if (curMatchInfos.Count == 0)
                    {
                        break;
                    }
                    BlockManager.instance.DestoryBlocks(MatchUtil.GetCoordsAll(curMatchInfos));
                    yield return(StartCoroutine(BlockManager.instance.CoApplyGravityAndGenerateMap()));

                    curMatchInfos = MatchManager.instance.CheckAll();
                }
                break;
            }
            yield return(null);
        }
        if (BlockManager.instance.totalTopCount == 0)
        {
            Debug.Log("Win!");
        }

        //초기화
        selectBlock = null;
        isReady     = true;
    }
    /// <summary>
    /// 해당 맵의 모든 블록의 매치를 검사합니다.
    /// </summary>
    /// <param name="block">기준 블록</param>
    /// <returns>매치결과 </returns>
    public List <MatchInfo> CheckAll()
    {
        var result = new List <MatchInfo>();

        foreach (var it in BlockManager.instance.blocks)
        {
            var blockResult = Check(it);
            if (blockResult != null)
            {
                result = result.Union(blockResult).ToList();
            }
        }
        result = MatchUtil.Distinct(result);
        return(result);
    }