uberpong/dev/lib/game/entities/paddle-enemy.js
2012-07-06 19:15:30 +02:00

56 lines
1.4 KiB
JavaScript

ig.module(
'game.entities.paddle-enemy'
)
.requires(
'game.entities.ball',
'game.entities.paddle'
)
.defines(function(){
EntityPaddleEnemy = EntityPaddle.extend({
move_delay: 35,
update: function() {
var ball = ig.game.getEntitiesByType(EntityBall)[0];
if (this.move_delay > 0) this.move_delay -= 1;
// TODO: Let the AI use the force feature
// Check if the ball is moving towards the enemy
if (ball.vel.x < 0) {
// Do we need to move the panel enyway?
if (this.move_delay == 0 && ((ball.pos.y > this.pos.y-5) && (ball.pos.y+ball.size.y < this.pos.y+this.size.y+5))) {
this.move_delay = 3;
if (this.vel.y < 0) {
this.vel.y = -200+((3-this.move_delay)*50)
}
if (this.vel.y > 0) {
this.vel.y = 200-((3-this.move_delay)*50)
}
} else if (this.move_delay == 0) {
// The paddle is too low
if ((ball.pos.y+ball.size.y/2) < (this.pos.y+30)) {
this.vel.y = -400;
// The paddle is too high
} else {
this.vel.y = 400;
}
}
// Move to the middle
} else {
if (this.pos.y >= 130 && this.pos.y <= 180) {
this.vel.y = 0;
}
if (this.pos.y < 130) {
this.vel.y = 150;
} else if (this.pos.y > 180) {
this.vel.y = -150;
}
}
this.parent();
}
});
});