From 3879dbef65bb57b886e6b3f5c2f35f37841a939c Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Fri, 6 Jul 2012 19:14:07 +0200 Subject: [PATCH] Make the AI unbreakable ... almost. see #9 --- dev/lib/game/entities/paddle-enemy.js | 53 +++++++++++++++++++++------ 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/dev/lib/game/entities/paddle-enemy.js b/dev/lib/game/entities/paddle-enemy.js index c0b3efe..c9ce104 100644 --- a/dev/lib/game/entities/paddle-enemy.js +++ b/dev/lib/game/entities/paddle-enemy.js @@ -6,21 +6,52 @@ ig.module( 'game.entities.paddle' ) .defines(function(){ + EntityPaddleEnemy = EntityPaddle.extend({ + move_delay: 35, + update: function() { + var ball = ig.game.getEntitiesByType(EntityBall)[0]; -EntityPaddleEnemy = EntityPaddle.extend({ + if (this.move_delay > 0) this.move_delay -= 1; - update: function(){ - var ball = ig.game.getEntitiesByType( EntityBall )[0]; + // TODO: Let the AI use the force feature - if( ball.pos.y + ball.size.y / 2 > this.pos.y + this.size.y / 2 ) { - this.vel.y = 100; - } else { - this.vel.y = -100; - } + // 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))) { + console.log("delay" + ball.pos.y); + 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; - this.parent(); - } + // 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(); + } + }); }); -});