package com.sarmatianHeroes.objects {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import com.sarmatianHeroes.*;
	
	public class HeroBase extends MovieClip {
		
		private var _map:Map = Map.getInstance();
		
		public var quantity:Number = 1;
		public var radius:Number = 12;
		public var vx:Number = 0;
		public var vy:Number = 0;
		public var speed:Number = 60;
		public var walked:Number = 0;
		public var collision:Ball;
		public var heroHP:Number;
		public var heroAP:Number = 2;
		public var Damage:Number = 30;
		public var attackRadius:Number = 50;
		public var target:Object;
		public var inAttack:Boolean = false;
		public var x0:Number;
		public var y0:Number;
		var _hpBar:MovieClip;
		var _heroHPMax:int = 100;
		var _evade:Number;
		
		private const HEAL:int = 30;
		
		public function HeroBase() 
		{
			heroHP = _heroHPMax;
			_evade = 0.8;
			collision = new Ball(radius,0x000000);
			addChild(collision);
			collision.alpha = 0.2;
			collision.y -= 3;
			collision.scaleX = 1.5;
			this.setChildIndex(collision,0);
			gotoAndStop(2);
			_hpBar = new HPBar();
			addChild(_hpBar);
			//trace(_hpBar.HPLine.scaleX);
		}
		
		public function attack(_target:Object):void
		{
			inAttack = true;
			target = _target;
			trace("hit_", target);
		}
		
		public function heal():void
		{
			heroHP += HEAL;
			if(heroHP > _heroHPMax)
			{
				heroHP = _heroHPMax;
			}
			_hpBar.HPLine.scaleX = heroHP/_heroHPMax;
		}
		
		public function takeDamage(damage:int):void
		{
			
			heroHP -= damage*0.20;
			if (heroHP>0)
			{
				_hpBar.HPLine.scaleX = heroHP/_heroHPMax;
				var _boom:Boom = new Boom(x,y);
				stage.addChild(_boom);
			}
			else
			{
				quantity = 0;
				if (_hpBar)
				{
					removeChild(_hpBar);
					_hpBar = null;
				}
				_map._heroAll.splice(_map._heroAll.indexOf(this),1);
				_map._obstacles.splice(_map._obstacles.indexOf(this),1);
				_map._layerMain.removeChild(this);
				var _body:MovieClip = new DeadHeroBody(_map._layerForward,_map._layerBackObjects,this);
				if(_map._heroAll.length == 0)
				{
					_map.mapDefeat();
				}
			}
		}
	}
	
}
