So, the idea of the code is to go around the ball in a circle and then when it lines up with a skeleton it kicks the ball. However, halfway through it just keeps kicking the ball to the right even though the skeleton is already dead! Is my peasant just drunk or something?
Screenshot
Code:
// Push the ball to knock over all the blue skeletons without hitting any red ones.
// The blue skeletons can be found as enemies.
function almostEqual(a, b){
return Math.abs(a - b) < 0.02
}
var peasant = hero.findByType("peasant")[0];
var ball = hero.findByType("ball")[0];
var enemies = hero.findEnemies();
function killSkeletons(i){
var enemy = enemies[i]
var angle = 0;
hero.command(peasant, "move", {'x': 35, 'y': 35})
hero.wait(1.5);
if (i > -1 && enemy){
var ballToSkeleton = Vector.subtract(enemy.pos, ball.pos);
var peasantToBall = Vector.subtract(ball.pos, peasant.pos);
if(ballToSkeleton.heading() != peasantToBall.heading()){
if (ball.velocity.magnitude() < 1) {
while (true){
ballToSkeleton = Vector.subtract(enemy.pos, ball.pos);
peasantToBall = Vector.subtract(ball.pos, peasant.pos);
let newX = ball.pos.x + (5 * Math.cos(angle));
let newY = ball.pos.y + (5 * Math.sin(angle));
hero.command(peasant, "move", {'x': newX, 'y': newY});
angle += 0.01
if (almostEqual(ballToSkeleton.heading(), peasantToBall.heading())){
break;
}
}
}
else {
}
}
hero.command(peasant, "move", {'x': 40, 'y': 35});
hero.wait(1);
killSkeletons(i - 1);
}
}
killSkeletons(enemies.length - 1);
The code is a little old so if you ask what something does it might take a while for me to figure it out because I kinda forgot lol.