代码之家  ›  专栏  ›  技术社区  ›  SpoocyCrep

我怎么能写得更干净?

  •  0
  • SpoocyCrep  · 技术社区  · 8 年前

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

    我有一个很长的代码,重复自己,并使用了大量的复制+粘贴,我知道这是一个非常糟糕的做法,复制粘贴相同的代码,但我无法找出一个方法来缩短它!我正在努力编写的代码cleaner是一个检查向量周围其他向量的代码。我通过加上+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两点。

    1 回复  |  直到 8 年前
        1
  •  4
  •   Vitor Figueredo    8 年前

    遵循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;
    

    这甚至可以让你改变 火势 直接从编辑那里。考虑到这一点,现在可以运行:

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

    你可以算出其余的。这只是我想做的一些事情。

    [编辑]我忘了提到在创建方法时,使用camelcase,我注意到您的一些方法使用 checkAirRadius 不。