Ok, I'm struggling a little bit, as you can tell from my game. Basically, right now I am trying to make the AI perfect so it hits the ball with almost every shot. This is the code I am using
Does anyone know where I am going wrong, or know of a different method to achieve the perfect AI? Thanks for any help, it is greatly appreciated.Code:TurretAI = function () {
var turret = _root.turret2;
var ball = _root.ball;
var unsolved = true;
var counter = 1;
var turret_x = turret.x;
var turret_y = turret.y;
var turret_xSpeed = turret.xSpeed;
var ball_x = ball.x;
var ball_y = ball.y;
var ball_xSpeed = ball.xSpeed;
var ball_ySpeed = ball.ySpeed;
//bullet_speed = 15*15;
var bullet_speed = 15;
while(unsolved) {
counter++;
turret_x += turret_xSpeed;
if(turret_xSpeed > 0) {
if(turret_x > 500) {
turret_xSpeed *= -1;
}
} else {
if(turret_x < 50) {
turret_xSpeed *= -1;
}
}
ball_x += ball_xSpeed;
ball_y += ball_ySpeed;
if(ball_x > 540) {
ball_x = 540;
ball_xSpeed *= -1;
} else if(ball_x < 10) {
ball_x = 10;
ball_xSpeed *= -1;
}
var bullet_distance = (bullet_speed*counter)*(bullet_speed*counter);
var xDist = turret_x-ball_x;
var yDist = turret_y-ball_y;
var dist = (xDist*xDist) + (yDist*yDist);
if(dist < bullet_distance) {
//Find the correct rotation and how long it will take to rotate there
var rot = Math.atan2(yDist, xDist);
var rotation = rot*57.2957795130823;
rotation -= 90;
if(rotation < 0) {
rotation += 360;
} else if(rotation > 360) {
rotation -= 360;
}
var rotation_time = Math.ceil((rotation-turret.rotation)/turret.rotation_speed);
if(rotation_time > counter) {
counter = rotation_time;
}
turret.needed_rotation = rotation;
turret.counter = counter;
unsolved = false;
}
}
}
