[英]Make a code less repetitive
我在游戏中编写了与角色移动有关的一部分代码,该代码非常重复(四个方向几乎都是相同的)。
由于在所有“ 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.