如何将其写得更干净?

时间:2018-07-16 05:58:58

标签: c# unity3d

我知道,这可能是一个非常愚蠢的问题。

我有一个很长的代码,该代码会重复自身并使用大量的copy + paste,我知道复制粘贴相同的代码是非常糟糕的做法,但我想不出一种缩短它的方法!我正在努力编写更简洁的代码是检查其他向量周围的向量的代码。我通过添加+1和-1来更改x,然后更改y +1和-1以及在z + 1和z-1的末尾来完成此操作,对于每个轻微的更改,我为稍有变化的向量调用相同的函数。这将创建一个非常丑陋的代码:

 void checkAirRadius(Voxel temp, Vector3 fireposition)
{
    Vector3 original = fireposition;
    if (temp.type != null)
    {
        if (temp.type.isFire != true && temp.type.burnable == true)
        {
            fireposition.x -= 1;
            Voxel temp2 = env.GetVoxel(fireposition);
            if (temp2.type == null)
                if (temp2.hasContent != 1)
                {
                    env.VoxelDestroy(original);
                    env.VoxelPlace(fireposition, firevoxel);
                    coroutine = WaitAndPrint(1.0f, fireposition, 1);
                    StartCoroutine(coroutine);

                }
            fireposition.x += 1;
            fireposition.x += 1;
            temp2 = env.GetVoxel(fireposition);
            if (temp2.type == null)
                if (temp2.hasContent != 1)
                {
                    env.VoxelDestroy(original);
                    env.VoxelPlace(fireposition, firevoxel);
                    coroutine = WaitAndPrint(1.0f, fireposition, 1);
                    StartCoroutine(coroutine);

                }
            fireposition.x -= 1;
            fireposition.y += 1;
            temp2 = env.GetVoxel(fireposition);
            if (temp2.type == null)
                if (temp2.hasContent != 1)
                {
                    env.VoxelDestroy(original);
                    env.VoxelPlace(fireposition, firevoxel);
                    coroutine = WaitAndPrint(1.0f, fireposition, 0);
                    StartCoroutine(coroutine);

                }
            fireposition.y -= 1;
            fireposition.y -= 1;
            temp2 = env.GetVoxel(fireposition);
            if (temp2.type == null)
                if (temp2.hasContent != 1)
                {
                    env.VoxelDestroy(original);
                    env.VoxelPlace(fireposition, firevoxel);
                    coroutine = WaitAndPrint(1.0f, fireposition, 2);
                    StartCoroutine(coroutine);

                }
            fireposition.y += 1;
            fireposition.z -= 1;
            temp2 = env.GetVoxel(fireposition);
            if (temp2.type == null)
                if (temp2.hasContent != 1)
                {
                    env.VoxelDestroy(original);
                    env.VoxelPlace(fireposition, firevoxel);
                    coroutine = WaitAndPrint(1.0f, fireposition, 1);
                    StartCoroutine(coroutine);

                }
            fireposition.z += 1;
            fireposition.z += 1;
            temp2 = env.GetVoxel(fireposition);
            if (temp2.type == null)
                if (temp2.hasContent != 1)
                {
                    env.VoxelDestroy(original);
                    env.VoxelPlace(fireposition, firevoxel);
                    coroutine = WaitAndPrint(1.0f, fireposition, 1);
                    StartCoroutine(coroutine);

                }
        }
    }
}
void FireUpdate()
{
    for (int i = poolFire.Count - 1; i >= 0; i--)
    {
        if (Random.Range(1, 10) == 5 || VoxelPlayEnvironment.instance.GetVoxel(new Vector3(poolFire[i].x, poolFire[i].y - 1, poolFire[i].z)).type == null)
        {
            poolFire.RemoveAt(i);
            return;
        }
        else
        {

            Vector3 fireposition = poolFire[i];
            fireposition.x -= 1;
            Voxel temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
            checkAirRadius(temp, fireposition);
            fireposition.x += 1;
            fireposition.x += 1;
             temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
            checkAirRadius(temp, fireposition);
            fireposition.x -= 1;
            fireposition.y += 1;
             temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
            checkAirRadius(temp, fireposition);
            fireposition.y -= 1;
             temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
            checkAirRadius(temp, fireposition);
            fireposition.y -= 1;
             temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
            checkAirRadius(temp, fireposition);
            fireposition.z += 1;
             temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
            checkAirRadius(temp, fireposition);
            fireposition.z -= 2;
             temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
            checkAirRadius(temp, fireposition);



        }
    }

我想知道是否可以创建一个简单的函数,该函数将采用一个向量并针对该块调用函数,而不必每次手动将x,y,z更改2点。

1 个答案:

答案 0 :(得分:5)

遵循DRY(不要重复自己)的原则,每次看到被重用的代码时,您都可以并且应该对其进行重构。

对于您的代码,我要做的第一件事是创建一个接收新fireposition的函数。举例来说:

void ChangeFirePositionAndRecalculate (int newValue) {
    fireposition.z += newValue;
    temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
    checkAirRadius(temp, fireposition);
}

代替呼叫:

fireposition.z -= 2;
temp = VoxelPlayEnvironment.instance.GetVoxel(fireposition);
checkAirRadius(temp, fireposition);

您可以致电:

ChangeFirePositionAndRecalculate (-2);
ChangeFirePositionAndRecalculate (1);

但是,再说一次,干。例如,您可以创建一个具有所有这些位置的数组:

public int [] positions;

这甚至可以让您直接从编辑器更改fireposition。考虑到这一点,您现在可以运行:

for (int i = 0; i < position.lenght; i++) {
    ChangeFirePositionAndRecalculate (positions [i]);
}

其余部分您都可以搞定。这只是我要做的事情。

[EDIT]我忘了提及创建方法时使用CamelCase的情况,我注意到您的某些方法使用了,但checkAirRadius却没有。