랜덤 타일 세팅

첫 세팅 시 타일 랜덤 배치

- ScriptableObject를 사용하여 타일 스프라이트 사용 -> 오늘 작업

- 3개 이상 안 겹치게 확인하여 다른 타일로 바꾸는 로직 필요 -> 추후 작업

public class MainGame : MonoBehaviour
{
	
    [SerializeField] private CharacterSO characterSO;

    private void InitBoard()
    {
        int characterSOIndex = UnityEngine.Random.Range(0, boardSize);
        characterImage.sprite = characterSO.Characters[characterSOIndex];
    }
}

 

 

 

여러 개일 때 움직이는 로직 구현 (1차 구현)

맞았을 때 테스트용으로 움직이는 로직을 구현하기로 했다.

- 아래처럼 구현하였는데 조건에 따라 총 6개의 경우의 수를 검사하기로 했다.

 

- enum배열 요소를 사용하여 bool배열을 체크하려 했지만 인덱스 형변환이 가비지를 발생시키므로 딕셔너리로 체크하였다.

- 위의 내용처럼 가운데가 먼저 체크 되었을 때 연산을 줄이려고 했다.

Dictionary<Direction, bool> _checkDict = new Dictionary<Direction, bool>();

private bool CheckExplosion(Vector2Int index)
{
    int targetSOIndex = _board[index].CharacterSOIndex;

    _checkDict[Direction.Horizontal] = CheckHorizontalIndex(index, targetSOIndex);
    _checkDict[Direction.Vertical] = CheckVerticalIndex(index, targetSOIndex);

    if (!_checkDict[Direction.Horizontal])
    {
        _checkDict[Direction.Right] = CheckRightIndex(index, targetSOIndex);
        _checkDict[Direction.Left] = CheckLeftIndex(index, targetSOIndex);
    }

    if (!_checkDict[Direction.Vertical])
    {
        _checkDict[Direction.Up] = CheckUpIndex(index, targetSOIndex);
        _checkDict[Direction.Down] = CheckDownIndex(index, targetSOIndex);
    }

    return _checkDict[Direction.Horizontal] || _checkDict[Direction.Vertical] || 
        _checkDict[Direction.Left] || _checkDict[Direction.Right] ||
        _checkDict[Direction.Up] || _checkDict[Direction.Down] || _checkDict[Direction.Up];
}

 

 

파괴할 오브젝트 추가

- 위의 조건을 돌면서 파괴할 오브젝트를 추가하는데 중복을 제거해야하므로 HashSet 자료구조를 사용했다.

- 이미지가 3개 이상이 같은지 확인했다.

private bool CheckHorizontalIndex(Vector2Int index, int targetSOIndex)
{
    // 가로
	// ... 로직 ...
    
    if (isHorizontalExploded)
    {
        for (int i = tempLeftIndex.y; i <= tempRightIndex.y; i++)
            _explosionSet.Add(new Vector2Int(index.x, i));
    }

    return isHorizontalExploded;
}

 

 

결과

- 타일 랜덤 배치

- 연속적인 타일이 3개 이상일 때 타일이 움직이도록 구현

* 다음 목표 : 처음 세팅 시 3개 붙어있는 것을 제거하도록 구현

+ Recent posts