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

같은 모양의 블럭 삭제하기

by pudding81 2024. 5. 13.

 

 

 

-  가로 세로  연속된 같은 이름의  블럭을 수를 센다.

-  가로 세로 3개 이상의 연속된 블럭을 삭제한다.

 

 private void FindsameBlock()
 {
    
    
     // 각 열과 행을 검색해서 같은 이름의 블록을 찾는다
     for (int row = 0; row < rowCount; row++)
     {
         for (int col = 0; col < colCount; col++)
         {
             GameObject currentBlock = blocks[row, col];
             // 클릭된 셀의 스프라이트 이름 가져오기

             if (currentBlock != null)
             {
               string spriteName = currentBlock.GetComponent<SpriteRenderer>().sprite.name;
                 // 가로 방향으로 연속된 같은 블록 수를 센다
                 int horizontalCount = CountSameBlocks(row, col, spriteName, true,false);
                 // 세로 방향으로 연속된 같은 블록 수를 센다
                 int verticalCount = CountSameBlocks(row, col, spriteName, false,true);

                                                 
                

                 // 가로 또는 세로로 3개 이상의 연속된 블록이 있는 경우 파괴한다
                 if (horizontalCount >= 3)
                 {
                    

                     //Debug.Log($"{currentBlock.name}: 가로 {horizontalCount}개");
                     DestroySameBlocks(row, col, spriteName, true, false);
                 }

                 if (verticalCount >= 3)
                 {
                    


                     // Debug.Log($"{currentBlock.name}: 세로 {verticalCount}개");
                   DestroySameBlocks(row, col, spriteName, false, true);
                 }



             }
         }

     }

    



 }




 private int CountSameBlocks(int startRow, int startCol, string blockName, bool checkHorizontal, bool checkVertical)
 {
     int count = 0;

     if (checkHorizontal)
     {
         // 가로 방향 검사
         for (int col = startCol; col < colCount; col++)
         {
             GameObject nextBlock = blocks[startRow, col];
             if (nextBlock != null)
             {
                 string spriteName = nextBlock.GetComponent<SpriteRenderer>().sprite.name;
                 if (spriteName == blockName)
                 {
                     count++;
                 }
                 else
                 {
                     break;
                 }
             }
             else
             {
                 break;
             }
         }
     }

     if (checkVertical)
     {
         // 세로 방향 검사
         for (int row = startRow; row < rowCount; row++)
         {
             GameObject nextBlock = blocks[row, startCol];
             if (nextBlock != null)
             {
                 string spriteName = nextBlock.GetComponent<SpriteRenderer>().sprite.name;
                 if (spriteName == blockName)
                 {
                     count++;
                 }
                 else
                 {
                     break;
                 }
             }
             else
             {
                 break;
             }
         }
     }

     // 최소 3개 이상의 연속된 블록인지 확인
     if (count >= 3)
     {
       
         return count;
     }


    
    
     return 0; // 최소 3개 이상의 연속된 블록이 아닌 경우 0을 반환
 }




 private void DestroySameBlocks(int startRow, int startCol, string blockName, bool destroyHorizontal, bool destroyVertical)
 {

    


     // 가로 방향 삭제
     if (destroyHorizontal)
     {
         int horizontalCount = CountSameBlocks(startRow, startCol, blockName, true, false);
         for (int col = startCol; col < startCol + horizontalCount; col++)
         {
             GameObject block = blocks[startRow, col];

             if (block != null)
             {
                 string spriteName = block.GetComponent<SpriteRenderer>().sprite.name;

                 if (spriteName == blockName)
                 {
                     //
                     //Debug.Log($"삭제 스프라이트 {spriteName} 블럭네임 {blockName}");

                   
                     Destroy(block);

                     // Instantiate(effect, block.transform.position, Quaternion.identity);
                     //  Debug.Log("가로 블럭삭제");



                     blocks[startRow, col] = null; // 제거된 블록의 자리를 null로 설정
                                                   //  Debug.Log($"{col},{startRow}, {block.name} 블록제거");





                    
                 }
             }


         }

     }

     // 세로 방향 삭제
     if (destroyVertical)
     {
         int verticalCount = CountSameBlocks(startRow, startCol, blockName, false, true);
         for (int row = startRow; row < startRow + verticalCount; row++)
         {
             GameObject block = blocks[row, startCol];
             if (block != null)
             {
                 string spriteName = block.GetComponent<SpriteRenderer>().sprite.name;
                 if (spriteName == blockName)
                 {

                   
                     Destroy(block);

                     // Instantiate(effect, block.transform.position, Quaternion.identity);
                     // Debug.Log("세로 블럭삭제");


                     blocks[row, startCol] = null; // 제거된 블록의 자리를 null로 설정


                   

                     //  Debug.Log($"{startCol} ,{row},{block.name} 블록제거");
                 }
             }



         }
     }




 }