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 function Avoider() {
                        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 > 5; 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 {
                        mouseDiffY = mouseY - player.y;
                        mouseDiffX = mouseX - player.x;
 
                        if (mouseDiffY < 0) {
                                if (!player.hitTestObject(walltop)) {
                                        player.y += mouseDiffY / 6;
                                }
                        }
                        if (mouseDiffY > 0) {
                                if (!player.hitTestObject(wallbottom)) {
                                        player.y += mouseDiffY / 6;
                                }
                        }
                        if (mouseDiffX < 0) {
                                if (!player.hitTestObject(wallleft)) {
                                        player.x += mouseDiffX / 6;
                                }
                        }
                        if (mouseDiffX > 0) {
                                if (!player.hitTestObject(wallright)) {
                                        player.x += mouseDiffX / 6;
                                }
                        }
                }
        }
}