0

подскажите, пожалуйста, как избавиться от дубляжа.

        private float GetXPositionForShapeSquare(ShapeData shapeData, int column, Vector2 moveDistance)
        {
            var shiftOnX = 0f;
            
            if (shapeData.columns > 1)
            {
                float startXPos;
                if (shapeData.columns % 2 != 0)
                    startXPos = (shapeData.columns / 2) * moveDistance.x * -1;
                else
                    startXPos = ((shapeData.columns / 2) - 1) * moveDistance.x * -1 - moveDistance.x / 2;
                shiftOnX = startXPos + column * moveDistance.x;
                return shiftOnX;
            }

            return shiftOnX;
        }

        private float GetYPositionForShapeSquare(ShapeData shapeData, int row, Vector2 moveDistance)
        {
            var shiftOnY = 0f;
            
            if (shapeData.rows > 1)
            {
                float startYPos;
                if (shapeData.rows % 2 != 0)
                    startYPos = (shapeData.rows / 2) * moveDistance.y;
                else
                    startYPos = ((shapeData.rows / 2) - 1) * moveDistance.y + moveDistance.y / 2;
                shiftOnY = startYPos - row * moveDistance.y;
            }
            return shiftOnY;
        } 
1
  • сделайте метод, который будет возвращать точку, где будут и х, и y. Т.е. объедините эти 2 метода в один
    – ArchDemon
    17 дек 2022 в 6:22

1 ответ 1

1

Кажется, можно обойтись простым рефакторингом:

private float GetPositionGeneric(int size, int pos, float distance)
{
    var shift = 0f;
    
    if (size > 1)
    {
        float start;
        if (size % 2 != 0)
            start = (size / 2) * distance;
        else
            start = ((size / 2) - 1) * distance + distance / 2;
        shift = start - pos * distance;
        return shift;
    }

    return shift;
}

private float GetXPositionForShapeSquare(ShapeData shapeData, int column, Vector2 moveDistance)
{
    return GetPositionGeneric(shapeData.columns, column, -moveDistance.x);
}

private float GetYPositionForShapeSquare(ShapeData shapeData, int row, Vector2 moveDistance)
{
    return GetPositionGeneric(shapeData.rows, row, moveDistance.y);
}

Ещё один рефакторинг для GetPositionGeneric. Избавимся от start:

...
if (size % 2 != 0) {
    shift = (size / 2) * distance - pos * distance;
} else {
    shift = ((size / 2) - 1) * distance + distance / 2 - pos * distance;
}
...

Вынесем distance за скобки:

...
if (size % 2 != 0) {
    shift = (size / 2 - pos) * distance;
} else {
    shift = (size / 2 - 0.5f - pos) * distance;
}
...

Заменим деление с целочисленного на вещественное:

...
if (size % 2 != 0) {
    shift = (size / 2.f - 0.5f - pos) * distance;
} else {
    shift = (size / 2.f - 0.5f - pos) * distance;
}
...

Уберём ненужный теперь if:

private float GetPositionGeneric(int size, int pos, float distance)
{
    if (size > 1)
    {
        return (size / 2.f - 0.5f - pos) * distance;
    }
    return 0;
}

Не знаю нужен ли оставшийся if. Возможно его тоже следует убрать.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.