This looks like... a good moment for a short lesson in code refactoring
😉
First useful thing: if you're using the same value multiple times, it's generally a good idea to assign it to a descriptive variable. You may or may not make your code shorter, but you'll definitely make it easier to understand. That will bring us to:
JavaScript:
let playerX = $gameVariables.value(1);
let playerY = $gameVariables.value(2);
let eventX = $gameMap.event(this.eventId()).x;
let eventY = $gameMap.event(this.eventId()).y;
if ((((playerX == eventX+0) || (playerX == eventX-2) || (playerX == eventX-1) || (playerX == eventX+1) || (playerX == eventX+2))
&&
((playerY == eventY-1) || (playerY == eventY-2) || (playerY == eventY-3) || (playerY == eventY-4) || (playerY == eventY-5) || (playerY == eventY-6)))
&&!
(((playerX == eventX-2) && (playerY == eventY-1)) ||
((playerX == eventX-2) && (playerY == eventY-6)) ||
((playerX == eventX+2) && (playerY == eventY-1)) ||
((playerX == eventX+2) && (playerY == eventY-6)))) {
$gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], true);
}
Next, look for repetitive patterns that can be expressed with something simpler. In this case, one thing that stands out is checking if
playerX is equal to one of 5 nearby values. How would you describe a value that's equal to either x-2, x-1, x ,x+1 or x+2? It's a variable with a value
between x-2 and x+2. This can be replaced with
<= and
>= operators, like so:
JavaScript:
let playerX = $gameVariables.value(1);
let playerY = $gameVariables.value(2);
let eventX = $gameMap.event(this.eventId()).x;
let eventY = $gameMap.event(this.eventId()).y;
if (playerX >= eventX-2 && playerX <= eventX+2 && playerY <= eventY-1 && playerY >= eventY-6
&&!
(((playerX == eventX-2) && (playerY == eventY-1)) ||
((playerX == eventX-2) && (playerY == eventY-6)) ||
((playerX == eventX+2) && (playerY == eventY-1)) ||
((playerX == eventX+2) && (playerY == eventY-6)))) {
$gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], true);
}
Now, what about the rest? There's no such simple pattern here, and even if there was, we're trying to make this compatible with a wide range of shapes. So, here's another good idea when writing code: try to separate
logic and
data. That means that the logic that handles something (like checking if the roof should be displayed) should be separate from the data that it uses (like the tiles on which the roof exists). This will make it a lot easier when you want to change said data. There are multiple ways we could do that here, but let's store the exceptions in an array of two-element arrays.
JavaScript:
const exceptions = [[-2,-1], [-2,-6], [+2,-1], [+2,-6]];
let playerX = $gameVariables.value(1);
let playerY = $gameVariables.value(2);
let eventX = $gameMap.event(this.eventId()).x;
let eventY = $gameMap.event(this.eventId()).y;
if (playerX >= eventX-2 && playerX <= eventX+2 && playerY <= eventY-1 && playerY >= eventY-6){
for (let i = 0; i < exceptions.length; i++) {
if(playerX === eventX + exceptions[i][0] && playerY === eventY + exceptions[i][1]) return;
}
$gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], true);
}
Granted, this might be more difficult if you're unfamiliar with arrays or for loops (in which case, I do recommend learning those
😉 ) - but this will basically check one by one every element in the
exceptions array, and abort if it finds a match.
Small note: there's no difference between
+2 and
2 in this context - it's just decorative.
All of this isn't just about making the code shorter - you can now much more easily change the dimensions of the object and the list of exceptions
🙂
EDIT: Fixed the indexes on exceptions
EDIT2: And also fixed the directions of comparisons. Whoops.