package com.Tutorial
{
    import com.adamatomic.flixel.*;
    
    public class Player extends FlxSprite
    {
        [Embed(source = '../../data/gfx/Player.png')] public static var ImgPlayer:Class;
        
        private var _move_speed:int = 2000;
        private var _jump_power:int = 800;   
        private var _max_health:int = 10;
        private var _hurt_counter:Number = 0;
		public var direction:int = 0;
		private var _moving:Boolean = false;
		private var _canmove:Boolean = true;
		
        public function Player(X:Number,Y:Number,Dir:String):void
        {
            super(ImgPlayer, X, Y, true, true,11,23);//Max speeds
						
            maxVelocity.x = 90;
            maxVelocity.y = 90;
            //Set the player health
            health = 10;
            //Gravity
            acceleration.y = 0;            
            //Friction
            drag.x = 800;
            drag.y = 800;
            //bounding box tweaks
            width = 11;
            height = 23;
            offset.x = 0;
            offset.y = 0;
			addAnimation("none",[0,1,2,3]);
			addAnimation("standingup",[1]);
			addAnimation("standingdown",[0]);
			addAnimation("standingleft",[2]);
			addAnimation("standingright",[3]);
			addAnimation("walkingup",[1]);
			addAnimation("walkingdown",[0]);
			addAnimation("walkingleft",[2]);
			addAnimation("walkingright",[3]);
			
			play("none");
			
			this.update();
        }
    
		public function stop():void
		{
			_canmove = false;
			play("none");
			switch(direction)
			{
				case 0:
				this.specificFrame(0);
				this._curFrame = 0;
				break;
				case 1:
				this.specificFrame(1);
				this._curFrame = 1;
				break;
				case 2:
				this.specificFrame(3);
				this._curFrame = 3;
				break;
				case 3:
				this.specificFrame(2);
				this._curFrame = 2;
				break;
			}
			this.update();
		}
		
		public function start():void
		{
			_canmove = true;
		}
        override public function update():void
        {
            super.update();
			
			if (!_canmove)
				return;

			var oldmoving:Boolean;
			oldmoving = _moving;
			var olddir:int;
			_moving = false;
			

			if (FlxG.kDown)
			{
				velocity.y += _move_speed * FlxG.elapsed;
				if (oldmoving && this._curAnim.name!="walkingdown"&&!(FlxG.kLeft||FlxG.kRight))
					this.play("walkingdown");
				_moving = true;
				direction = 1;
			}
            if(FlxG.kLeft)
            {
                velocity.x -= _move_speed * FlxG.elapsed;
				if (oldmoving && this._curAnim.name != "walkingleft"&&!FlxG.kUp)
				{
					this.play("walkingleft");
				}
				_moving = true;
				direction = 2;
            }
            if (FlxG.kRight)
            {
                velocity.x += _move_speed * FlxG.elapsed;  
				if (oldmoving && this._curAnim.name!="walkingright"&&!FlxG.kUp)
					this.play("walkingright");
				_moving = true;
				direction = 3;
            }
			if (FlxG.kUp)
			{
				velocity.y -= _move_speed * FlxG.elapsed;
				if (oldmoving && this._curAnim.name!="walkingup")
					this.play("walkingup");
				_moving = true;
				direction = 0;
			}
			
			if (!_moving)
			{
				switch(direction)
				{
					case 0:
					this.play("standingup");
					break;
					case 1:
					this.play("standingdown");
					break;
					case 2:
					this.play("standingleft");
					break;
					case 3:
					this.play("standingright");
					break;
				}
			}
			
			if (x < 8)
			{
				if (FlxG.level < 9)
				{
					FlxG.oldlevel = FlxG.level;
					FlxG.level=9
				}
				else
				{
					if (y < 3 * 16)
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 1;
					}
					else
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 7;
					}
				}		
				FlxG.switchState(WalkState);		
			}
			else if (x > FlxG.width - 16- 8)
			{
				if (FlxG.level < 9)
				{
					FlxG.oldlevel = FlxG.level;
					FlxG.level=9
				}
				else
				{
					if (y < 3 * 16)
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 6;
					}
					else
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 0;
					}
				}			
				FlxG.switchState(WalkState);	
			}
			else if (y < 8)
			{
				if (FlxG.level < 9)
				{
					FlxG.oldlevel = FlxG.level;
					FlxG.level=9
				}
				else
				{
					if (x < 3 * 16)
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 8;
					}
					else if (x < 5 * 16)
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 5;
					}
					else
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 3;
					}
				}			
				FlxG.switchState(WalkState);	
			}
			else if (y > FlxG.height - 16 - 16 - 8)
			{
				if (FlxG.level < 9)
				{
					FlxG.oldlevel = FlxG.level;
					FlxG.level=9
				}
				else
				{
					if (x < 3 * 16)
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 4;
					}
					else
					{
						FlxG.oldlevel = FlxG.level;
						FlxG.level = 2;
					}
				}
				FlxG.switchState(WalkState);				
			}
			
        }

        override public function hurt(Damage:Number):void
        {
            _hurt_counter = 1;
            return super.hurt(Damage);
        }       
    }
}