package {
        import flash.display.MovieClip;
        import flash.text.TextField;
        import flash.display.Shape;
        import flash.events.Event;
 
        public class Avoider extends MovieClip {
                public var target:MovieClip = new MovieClip;
                public var player:MovieClip = new MovieClip;
                public var score:int = 0;
                public var mouseDiffX:int = 0;
                public var mouseDiffY:int = 0;
                public var bottomwalls:Array;
                public var topwalls:Array;
                public var leftwalls:Array;
                public var rightwalls:Array;
 
                public function Avoider() {
                        bottomwalls = new Array(wallbottom,wallbottom2);
                        topwalls = new Array(walltop,walltop2);
                        leftwalls = new Array(wallleft,wallleft2);
                        rightwalls = new Array(wallright,wallright2);
                        trace(bottomwalls[0]);
                        addEventListener(Event.ENTER_FRAME, mouseMovement);
                        drawPlayer();
                        drawNewTarget();
                        drawNewEnemy();
                }
                public function drawNewTarget() {
                        var posX:uint = randRange(30,520);
                        var posY:uint = randRange(30,370);
                        target.graphics.beginFill(0x0000FF);
                        target.graphics.drawRect(0, 0, 20, 20);
                        target.x = posX;
                        target.y = posY;
                        target.graphics.endFill();
                        addChild(target);
                        target.addEventListener(Event.ENTER_FRAME,gotToTarget);
                }
                public function drawPlayer() {
                        player.graphics.beginFill(0x000000);
                        player.graphics.drawCircle(0, 0, 10);
                        player.x = 200;
                        player.y = 200;
                        player.graphics.endFill();
                        addChild(player);
                }
                public function drawNewEnemy() {
                        var posX:uint = randRange(50,500);
                        var posY:uint = randRange(50,350);
 
                        var anenemy:Enemy = new Enemy;
                        anenemy.graphics.beginFill(0xFFFFFF);
                        anenemy.graphics.lineStyle(2,0x000000);
                        anenemy.graphics.drawCircle(10, 10, 10);
                        anenemy.x = posX;
                        anenemy.y = posY;
                        anenemy.graphics.endFill();
                        addChild(anenemy);
                        anenemy.addEventListener(Event.ENTER_FRAME,touchPlayer);
                        if (randRange(0,100) > 50) {
                                anenemy.addEventListener(Event.ENTER_FRAME,moveVertical);
                        } else {
                                anenemy.addEventListener(Event.ENTER_FRAME,moveHorizontal);
                        }
                }
                private function randRange(start:Number,end:Number):Number {
                        return Math.floor(start + Math.random() * end - start);
                }
                private function moveVertical(e:Event) {
                        if (e.target.mydirection == 0) {
                                e.target.y += 4;
                        } else if (e.target.mydirection == 1) {
                                e.target.y -=4;
                        }
                        if (e.target.hitTestObject(wallbottom)) {
                                e.target.mydirection = 1;
                        }
                        if (e.target.hitTestObject(walltop)) {
                                e.target.mydirection = 0;
                        }
                }
                private function moveHorizontal(e:Event) {
                        if (e.target.mydirection == 0) {
                                e.target.x += 4;
                        } else if (e.target.mydirection == 1) {
                                e.target.x -=4;
                        }
                        if (e.target.hitTestObject(wallright)) {
                                e.target.mydirection = 1;
                        }
                        if (e.target.hitTestObject(wallleft)) {
                                e.target.mydirection = 0;
                        }
                }
                private function touchPlayer(e:Event) {
                        if (e.target.hitTestObject(player) && !e.target.hastouched) {
                                e.target.hastouched = true;
                                for (var i = (numChildren - 1); i > 9; i--) {
                                        getChildAt(i).removeEventListener(Event.ENTER_FRAME,touchPlayer);
                                        removeChildAt(i);
                                }
                                e.target.removeEventListener(Event.ENTER_FRAME,touchPlayer);
                                score = 0;
                                scoretext.text = "Score: 0";
                                drawNewTarget();
                                drawNewEnemy();
                        }
                }
                private function gotToTarget(e:Event) {
                        if (e.target.hitTestObject(player)) {
                                e.target.x = randRange(30,520);
                                e.target.y = randRange(30,370);
                                drawNewEnemy();
                                score++;
                                scoretext.text = "Score: " + String((score * 10));
                        }
                }
                public function mouseMovement(event:Event):void {
                        var hitting:Boolean = false;
                        var i:int = 0;
                        mouseDiffY = mouseY - player.y;
                        mouseDiffX = mouseX - player.x;
                        if (mouseDiffY < 0) {
                          hitting = false;
                          for(i = 0;i < topwalls.length;i++){
                                   if (player.hitTestObject(topwalls[i])) {
                                           hitting = true;
                                   }
                          }
                          if(!hitting){
                                 player.y += mouseDiffY / 6;
                          }
                        }
                        if (mouseDiffY > 0) {
                          hitting = false;
                          for(i = 0;i < bottomwalls.length;i++){
                                   if (player.hitTestObject(bottomwalls[i])) {
                                           hitting = true;
                                   }
                          }
                          if(!hitting){
                                 player.y += mouseDiffY / 6;
                          }
                        }
                        if (mouseDiffX < 0) {
                          hitting = false;
                          for(i = 0;i < leftwalls.length;i++){
                                   if (player.hitTestObject(leftwalls[i])) {
                                           hitting = true;
                                   }
                          }
                          if(!hitting){
                                 player.x += mouseDiffX / 6;
                          }
                        } 
                        if (mouseDiffX > 0) {
                          hitting = false;
                          for(i = 0;i < rightwalls.length;i++){
                                   if (player.hitTestObject(rightwalls[i])) {
                                           hitting = true;
                                   }
                          }
                          if(!hitting){
                                 player.x += mouseDiffX / 6;
                          }
                        }                 
                }
        }
}