减少代码重复

Make a code less repetitive

我在一个游戏中写了一段代码,涉及角色的移动,这段代码非常重复(4个方向几乎都一样)。

我看不出如何让它更简单,因为在所有 'if' 条件下都进行了测试,因为在另一种情况下,我会制作一个带有正/负变量的系统,我可以乘以位置变化。

感谢您的帮助!

for (var i = 0; i < boxArray.length; i++) {
    var box = boxArray[i];

    if (controller.left && currentLevel[this.id - this.moveHorizontal] != 1) {
        // Enregistrement de la position dans le tableau d'undo/redo
        this.index++;
        var array = [this.x, this.y, this.id];
        this.undoArray.push(array);
        // Si box à côté et pas de collision possible 
        if (box.id == this.id - this.moveHorizontal && currentLevel[this.id - this.moveHorizontal * 2] != 1 && currentLevel[this.id - this.moveHorizontal * 2] != 2) {
            // Décalage de la position du player 
            this.x -= this.boxWidth;
            this.id -= this.moveHorizontal;
            currentLevel[this.id] = 8;
            currentLevel[this.id + this.moveHorizontal] = 0;
            // Décalage de la position de la box
            box.x -= this.boxWidth; 
            box.id -= this.moveHorizontal;
            currentLevel[this.id - this.moveHorizontal] = 2;
            controller.left = false;
        }
        // Sinon si aucun objet à côté  
        else if (currentLevel[this.id - this.moveHorizontal] == 0 || currentLevel[this.id - this.moveHorizontal] == 3) {
            // Décalage de la position du player
            this.x -= this.boxWidth;
            this.id -= this.moveHorizontal;
            currentLevel[this.id] = 8;
            // Décalage de la position du sol
            currentLevel[this.id + this.moveHorizontal] = 0;
            controller.left = false;
        }
    }
    else if (controller.right && currentLevel[this.id + this.moveHorizontal] != 1) {
        this.index++;
        var array = [this.x, this.y, this.id];
        this.undoArray.push(array);
        if (box.id == this.id + this.moveHorizontal && currentLevel[this.id + this.moveHorizontal * 2] != 1 && currentLevel[this.id + this.moveHorizontal * 2] != 2) {
            this.x += this.boxWidth;
            this.id += this.moveHorizontal;
            currentLevel[this.id] = 8;
            currentLevel[this.id - this.moveHorizontal] = 0;
            box.x += this.boxWidth; 
            box.id += this.moveHorizontal;
            currentLevel[this.id + this.moveHorizontal] = 2;
            controller.right = false;
        }
        else if (currentLevel[this.id + this.moveHorizontal] == 0 || currentLevel[this.id + this.moveHorizontal] == 3) {
            this.x += this.boxWidth;
            this.id += this.moveHorizontal;
            currentLevel[this.id] = 8;
            currentLevel[this.id - this.moveHorizontal] = 0;
            controller.right = false;
        }
    }

类似的东西

for (var i = 0; i < boxArray.length; i++) {
    var box = boxArray[i];

    var direction = {
        'left': -1,
        'right': 1
    }; 

    for (var d in direction) {
        var mh = this.moveHorizontal * direction[d];

        if (controller[d] && currentLevel[this.id + mh])] {
            this.index++;
            var array = [this.x, this.y, this.id];
            this.undoArray.push(array);

            if (box.id == this.id + mh && currentLevel[this.id + mh * 2] != 1 && currentLevel[this.id + mh * 2] != 2) {
                this.x += this.boxWidth * direction[d];
                this.id += mh; 
                currentLevel[this.id] = 8;
                currentLevel[this.id + this.moveHorizontal] = 0;
                // Décalage de la position de la box
                box.x += this.boxWidth * direction[d]; 
                box.id += mh; 
                currentLevel[mh] = 2;
                controller[d] = false;
            } else if (currentLevel[this.id + mh] == 0 || currentLevel[this.id + mh] == 3) {
                // Décalage de la position du player
                this.x += this.boxWidth * direction[d];
                this.id += mh; 
                currentLevel[this.id] = 8;
                // Décalage de la position du sol
                currentLevel[this.id + (mh * -1)] = 0;
                controller[d] = false;
            }
        }
    }
} 

我认为你绝对可以简化这段代码。 例如,创建一个像这样的对象:

var moves = {
    moveLeft: function() { ... },
    moveRight: function() { ... }
};

然后你可以说

moves[side](...);

其中 side 是表示 moveLeft 或 moveRight 的字符串。

除此之外,您还可以对重复的功能进行分组,例如左右重复的功能:

function initMovement() {
   var array = [this.x, this.y, this.id];
   this.index++;
   this.undoArray.push(array);
}