실패 이동

실패 시 이동 (왔다 갔다) 구현

- 실패 시 이동은 Cos을 이용하여 구현하였다.

- Cos의 분자의 경우에는 ㅠ만큼 지나면 다시 0이 되는 것이니 경과 시간에 ㅠ를 곱해줬다.

- Cos의 분모의 경우에는  목표 시간을 넣어줬는데 목표 시간이 클수록 경과 시간도 커지기 때문이다.

- 경과 시간이 ㅠ부터 시작하기 위해 (ratio가 0부터 시작하기 위해) ㅠ를 더해줬다.

 

public IEnumerator GoRoundTrip(Vector2 targetPos, float moveTime)
{
    Vector2 offsetVec = Transform.position;
    float elapsedTime = 0;
    float ratio = 0;
    while (elapsedTime < moveTime)
    {
        elapsedTime += Time.deltaTime;
        ratio = (MathF.Cos(elapsedTime * PI / (moveTime * HALF) + PI) + 1) * HALF;

        if (elapsedTime >= moveTime)
            ratio = 0f;

        Transform.position = Vector2.Lerp(offsetVec, targetPos, ratio);
        yield return null;
    }
}

 

 

파괴 로직

이동 후 파괴

- 이동 후 파괴시켜야 하므로 코루틴 이후 타겟의 이동이 끝나면 호출 하는 방식

- 이동이 끝나면 콜백으로 호출하게 만들었다.

private void Explode()
{
    foreach (var index in _explosionSet)
    {
        _boardPosArr[index.x, index.y] = _board[index].Transform.position;
        Destroy(_board[index].gameObject);
        _board[index] = null;
    }

    ArrangeBoard();
}
public IEnumerator Move(Vector2 targetPos, float moveTime)
{
    // 이동 로직

    if (_destroyOrder)
        _destroyCallback.Invoke();
}

 

 

다음 타일 내려오게 하기

파괴가 완료 되었다면 파괴된 타일 기준으로 위의 타일이 내려와야 한다.

- 선입 선출이 필요해서 Queue를 사용 (계속 다음 타일과 바꿔 기존의 타일을 null로 해줘야 함)

for (int j = 0; j < boardSize; j++)
{
    tempIndex.x = j;
    tempIndex.y = i;
    if (!_board[tempIndex])
    {
        _nullQueue.Enqueue(tempIndex);
    }
    else if (_nullQueue.Count > 0)
    {
        Vector2Int nullIndex = _nullQueue.Dequeue();
        _board[tempIndex].Transform.position = _boardPosArr[nullIndex.x, nullIndex.y];
        _board[nullIndex] = _board[tempIndex];
        _board[tempIndex] = null;
    }
}

 

 

결과

- 실패시 다시 타일이 돌아옴

- 성공 시 타일이 파괴되고 위의 타일들이 자리를 채워줌

 

+ Recent posts