본문 바로가기
게임프로젝트/애니팡 모작

블럭 클릭해서 드래그하면 블럭의 좌표, 이동방향 표시하기

by pudding81 2024. 5. 13.

    private void ClickBlock()
    {

        // 클릭한 위치의 셀 인덱스를 디버그로 출력 / 블록이름 보여주기
        if (Input.GetMouseButtonDown(0))
        {
            isdragging = true;
            this.StartDrag = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            int col = Mathf.RoundToInt(StartDrag.x);
            int row = Mathf.RoundToInt(StartDrag.y);

            if (col >= 0 && col < colCount && row >= 0 && row < rowCount)
            {


                // 클릭된 셀의 블록 객체 가져오기
                this.clickedBlock = blocks[row, col];
                if (clickedBlock != null)
                {
                    BlockController blockController = clickedBlock.GetComponent<BlockController>();
                    // 블록 컨트롤러에서 블록네임 이넘타입 가져오기
                    BlockName blockName = blockController.GetSpriteName();
                    // 블록 스프라이트 이름 출력
                    Debug.Log($" 드래그 시작 위치: {col},{row} /스프라이트이름:{blockName}");

                   

                }
            }

        }

        // 클릭이 끝난 위치의 셀 인덱스를 디버그로 출력 / 블록이름 보여주기 / 누르면 블록삭제
        if (Input.GetMouseButtonUp(0))
        {
            // 드래그가 끝나는 위치를 가져옵니다.
            this.EndDrag = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            int col = Mathf.RoundToInt(EndDrag.x);
            int row = Mathf.RoundToInt(EndDrag.y);

            if (col >= 0 && col < colCount && row >= 0 && row < rowCount)
            {


                // 클릭된 셀의 블록 객체 가져오기
                GameObject clickedBlock = blocks[row, col];
                if (clickedBlock != null)
                {
                    BlockController blockController = clickedBlock.GetComponent<BlockController>();
                    // 블록 컨트롤러에서 블록네임 이넘타입 가져오기
                    BlockName blockName = blockController.GetSpriteName();
                    // 블록 이름 출력
                    Debug.Log($" 드래그 종료 위치: {col},{row} /이름:{blockName}");
                }




            }

            //2.2 스와이프 방향을 구한다.
            float distance = Vector2.Distance(StartDrag, EndDrag);

            // 일정 거리 이상의 스와이프인지 확인 (예: 100 픽셀)
            if (distance > mindistance && distance <maxdistance)
            {

                Vector2 dragDirection = EndDrag - StartDrag;



                if (Mathf.Abs(dragDirection.x) > Mathf.Abs(dragDirection.y))
                {
                    if (dragDirection.x > 0) // 오른쪽으로 드래그
                    {
                        Debug.Log("오른쪽으로 드래그");
                      
                    }
                    else // 왼쪽으로 드래그
                    {
                        Debug.Log("왼쪽으로 드래그");
                      
                    }
                }
                else
                {
                    if (dragDirection.y > 0) // 위로 드래그
                    {
                         Debug.Log("위로 드래그");
                     
                    }
                    else
                    {
                        Debug.Log("아래로 드래그");
                       
                    }
                }



            }
                // 드래그 상태를 변경합니다.
                isdragging = false;


            

        }

          


    }

'게임프로젝트 > 애니팡 모작' 카테고리의 다른 글

요요  (0) 2024.05.20
같은 모양의 블럭 삭제하기  (0) 2024.05.13
블럭 드래그 해서 이동하기  (0) 2024.05.13
블럭 랜덤 생성하기  (1) 2024.05.13