I'm building a Tetris game and I've been stuck for days now.
I have a function to rotate the tetraminos rotateRight().
I built in a way to check if the tetramino is on either edge of the playfield area, and when rotating block goes over the edge (appearing on the other side) it will then move the block one space to left(if its by the right edge and vice versa if its by the left one).
Its done via conditionals code here below:
if (checkEdgeL) { // checks if its by the left wall
if (checkEdgeR) { // check if it goes over the wall to the other side
if (current === tetraminosArray[6][currentRotation]) {
if (currentRotation === 2) {
currentPosition = currentPosition + 2;
} else if (currentRotation === 0) {
currentPosition = currentPosition + 1;
}
} else if (
current === tetraminosArray[random][currentRotation] &&
random !== 6
) {
if (currentRotation === 2) {
currentPosition = currentPosition + 1;
}
}
}
}
if (checkEdgeR) { // checks if its by the right wall
if (checkEdgeL) { // check if it goes over the wall to the other side
if (current === tetraminosArray[6][currentRotation]) {
if (currentRotation === 2) {
currentPosition = currentPosition - 1;
}
if (currentRotation === 0) {
currentPosition = currentPosition - 2;
}
} else if (
current === tetraminosArray[random][currentRotation] &&
random !== 6
) {
if (currentRotation === 0) {
currentPosition = currentPosition - 1;
}
}
}
}
draw();
console.log(currentRotation);
}
I have few problems here. When only half of the above is enabled (so only checks for either left or right side) all works fine.
In current state I Tetramino is pulled from one side to the other due to conditionals and how code moves blocks when they go over the edge.
If I change to else if statement in the middle whole thing is messed up:
if (checkEdgeL) {
if (checkEdgeR) {
if (current === tetraminosArray[6][currentRotation]) {
console.log("conditional 1");
if (currentRotation === 2) {
console.log("conditional 2a on the left");
currentPosition = currentPosition + 2;
} else if (currentRotation === 0) {
console.log("conditional 2b on the left");
currentPosition = currentPosition + 1;
}
} else if (
current === tetraminosArray[random][currentRotation] &&
random !== 6
) {
if (currentRotation === 2) {
currentPosition = currentPosition + 1;
}
}
}
} else if (checkEdgeR) { // CHANGED HERE TO ELSE IF CONDITIONAL instead of two separate IF's
if (checkEdgeL) {
if (current === tetraminosArray[6][currentRotation]) {
console.log("conditional 1");
if (currentRotation === 2) {
console.log("conditional 2a on the right");
currentPosition = currentPosition - 1;
}
if (currentRotation === 0) {
console.log("conditional 2b on the right");
currentPosition = currentPosition - 2;
}
} else if (
current === tetraminosArray[random][currentRotation] &&
random !== 6
) {
if (currentRotation === 0) {
currentPosition = currentPosition - 1;
}
}
}
}
draw();
console.log(currentRotation);
}
So question is: Is there a way of making it work as if one half of the code was commented out, and vice versa?
My whole tetris is available in this codepen.
It's set up to only spawn I Tetraminos now, and timer is off so blocks don't slide down like in the proper game.
Any help greatly appreciated, before I pull all my hair out.
I've tried changing conditionals from if to else if but that would just mess my game even more.