Crux of the desert


#1
while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Left: enemy.pos.x is less than hero.pos.x
        
var isLeft = hero.pos.x  > enemy.pos.x;
        // Above: enemy.pos.y is greater than hero.pos.y
        
var isAbove = hero.pos.y < enemy.pos.y;
        // Right: enemy.pos.x is greater than hero.pos.x
        
var isRight = enemy.pos.x > hero.pos.x;
        // Below: enemy.pos.y is less than hero.pos.y
        
var isBelow = enemy.pos.y > hero.pos.y; 
        // If enemy isAbove and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        
if(isLeft && isAbove) {
            hero.buildXY("fire-trap", 40 - 20, 34 + 17);
        }
        // If enemy isAbove and isRight:
        // buildXY() a "fire-trap" at the X mark.
        
if (isAbove && isRight) {
            hero.buildXY("fire-trap", 60, 51);
        }
        // If enemy isBelow and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        
if (isBelow && isLeft) {
            hero.buildXY("fire-trap", 20, 17);
        }
        // If enemy isBelow and isRight:
        // buildXY() a "fire-trap" at the X mark.
        
if (isBelow && isRight) {
            hero.buildXY("fire-trap", 60, 17);
        }else{
            hero.moveXY(40, 34);
        }
        
    
       
    }
}

That’s my code. I’m not sure if it’s right or wrong. plz help


#2

When deciding where the enemy is “left” is negative on the x axis, and “right” is positive on the x axis. Same for “up” being positive on the y axis, and “down” being negative on the y axis. So if enemy were left of the hero, then it’d be “hero.pos.x > enemy.pos.x” or “enemy.pos.x < hero.pos.x”. You have the “isBelow” asking if the enemy has a higher position on the y axis than you.

Also, not sure if it matters but you could have done the adding/subtracting for the x,y instead of direct movement. “60, 51” could of been “40 + 20, 34 + 17”, but not sure if that matters.

I’m not used to JavaScript, so I could be wrong about the else statement, but it looks out of line with everything. Not sure if that matters or works the same, but if it does then that could be causing issues too. I really should learn more on JavaScript. Anyways, hope this helped some. ^^