"use strict";

var EN_MetalJerk = function(){
	this.sprite = new Sprite(EN_MetalJerk.spriteSheet,"MetalJerk");
	this.sprite.game_object = this; // no longer a generic GO

	this._basicInit(EN_MetalJerk.params);

	//-----

	this.sprite.collision_flags |= (DEFINE_SPRITE_COLLISION_FLAGS.GROUND | DEFINE_SPRITE_COLLISION_FLAGS.WATER);

	this.direction = 1;
	this.elevation = 0;
	this.range = 16;

	this.dy = 0;
	this.ay = 0;

	this.meli = null;
	this.meliFind=false; // she probably won't be in the map when we init; hopefully when we update

	this.frame_timer=0;
};
EN_MetalJerk.spriteImg = SDS_Load_Image("art/sheets/sprite/metaljerk.png");
EN_MetalJerk.spriteSheet = new TileSheet(4, 3, 0, 2, EN_MetalJerk.spriteImg);
EN_MetalJerk.params = {
	name: "MetalJerk",
	box: [0,0,15,7], // left, top, right, bottom
	max_step: 0, // otherwise we get turnaround false-positives going into slopes on y and being ejected out on x
	hp: 3,
	infection: 1, // N/A; not infected per se, just murderous
};

EN_MetalJerk.prototype = new GO_Enemy();

	EN_MetalJerk.prototype.swoop = [0,1,1,1,2,2,2,3];

	EN_MetalJerk.prototype.ai = function(){
		if (!this.meli && !this.meliFind){
			this.meli = this.manager.getNamedObject("Meli");
			this.meliFind = true;
		}

		if (!this.elevation){
			this.elevation = this.sprite.y + this.sprite.center.y;
		}

		// V1 - manip raw dy, ay according to implied target
		/*if (this.meli && this.infection>0){
			if ((this.direction=== 1 && this.sprite.x+this.sprite.box_right < this.meli.sprite.x+this.meli.sprite.box_left) ||
				(this.direction===-1 && this.sprite.x+this.sprite.box_left > this.meli.sprite.x+this.meli.sprite.box_right)){
				
				// seek if in range
				if (this.meli.sprite.y+this.meli.sprite.box_bottom<this.sprite.y+this.sprite.box_top && this.meli.sprite.y+this.meli.sprite.box_bottom>this.elevation-this.range){
					this.ay-=1;
				}
				else if (this.meli.sprite.y+this.meli.sprite.box_top>this.sprite.y+this.sprite.box_bottom && this.meli.sprite.y+this.meli.sprite.box_top<this.elevation+this.range){
					this.ay+=1;
				}
				// center if out of range
				else if(this.sprite.y+this.sprite.center.y<this.elevation){
					this.ay+=1;
					this.ay = Math.min(this.ay, this.elevation - (this.sprite.y+this.sprite.center.y));
				}
				else if(this.sprite.y+this.sprite.center.y>this.elevation){
					this.ay-=1;
					this.ay = Math.max(this.ay, this.elevation - (this.sprite.y+this.sprite.center.y));
				}
				// dampen if on center
				else if (this.ay!=0){
					this.ay+=(this.ay>0) ? -1 : 1;
				}
			}
			// center if not seeing meli
			else if(this.sprite.y+this.sprite.center.y<this.elevation){
				this.ay+=1;
				this.ay = Math.min(this.ay, this.elevation - (this.sprite.y+this.sprite.center.y));
			}
			else if(this.sprite.y+this.sprite.center.y>this.elevation){
				this.ay-=1;
				this.ay = Math.max(this.ay, this.elevation - (this.sprite.y+this.sprite.center.y));
			}
			// dampen if on center
			else if (this.ay!=0){
				this.ay+=(this.ay>0) ? -1 : 1;
			}
		}

		this.ay = Math.min(3,Math.max(this.ay,-3));
		this.dy += this.ay;
		this.dy = Math.min(3,Math.max(this.dy,-3));*/


		// V2- set a target y; track to it, capping according to distance
		this._heap.targety = this.elevation - this.sprite.center.y; // default to targeting usual elevation
		if (this.meli && this.infection>0){
			if ((this.direction=== 1 && this.sprite.x+this.sprite.box_right < this.meli.sprite.x+this.meli.sprite.box_left) ||
				(this.direction===-1 && this.sprite.x+this.sprite.box_left > this.meli.sprite.x+this.meli.sprite.box_right)){
				
				// seek if inoutof range - unintentionally erroneous but evil
				//if ((this.meli.sprite.y+this.meli.sprite.box_bottom<this.sprite.y+this.sprite.box_top && this.meli.sprite.y+this.meli.sprite.box_bottom>this.elevation-this.range) ||
				//	(this.meli.sprite.y+this.meli.sprite.box_top>this.sprite.y+this.sprite.box_bottom && this.meli.sprite.y+this.meli.sprite.box_top<this.elevation+this.range)){
				//	this._heap.targety = this.meli.sprite.y + this.meli.sprite.center.y;
				//}

				// seek if in range & view - corrected
				/*if (
					((this.meli.sprite.y+this.meli.sprite.box_bottom>this.elevation-this.range) &&
					(this.meli.sprite.y+this.meli.sprite.box_top<this.elevation+this.range))
					&&
					((this.meli.sprite.y+this.meli.sprite.box_bottom>this.sprite.y+this.sprite.box_top) &&
					(this.meli.sprite.y+this.meli.sprite.box_top<this.sprite.y+this.sprite.box_bottom))

					){
					this._heap.targety = this.meli.sprite.y + this.meli.sprite.center.y;
				}*/

				// less chaotic
				if ((this.meli.sprite.y+this.meli.sprite.box_bottom>this.elevation-this.range) &&
					(this.meli.sprite.y+this.meli.sprite.box_top<this.elevation+this.range)){
					this._heap.targety = this.meli.sprite.y + this.meli.sprite.center.y;
				}
			}
		}
		if(this.sprite.y+this.sprite.center.y<this._heap.targety){
			this.ay+=1;
		}
		else if(this.sprite.y+this.sprite.center.y>this._heap.targety){
			this.ay-=1;
		}
		this.ay = Math.min(3,Math.max(this.ay,-3));
		this.dy += this.ay;
		this._heap.cap = Math.abs(this.sprite.y+this.sprite.center.y-this._heap.targety);
		this._heap.cap = Math.min(this._heap.cap,this.swoop.length-1);
		this._heap.cap = this.swoop[this._heap.cap];
		this.dy = Math.min(this._heap.cap,Math.max(this.dy,-this._heap.cap));
		//this.ay = Math.min(this._heap.cap,Math.max(this.dy,-this._heap.cap)); // unintentional but even more jerky
		this.ay = Math.min(this._heap.cap,Math.max(this.ay,-this._heap.cap));


		this._heap.dx = ((this.direction>0 && this.sprite.frame==0) || (this.direction<0 && this.sprite.frame==3)) ? this.direction : 0;
		
		// faster on X if within x-range
		if ((this.meli.sprite.x+this.meli.sprite.box_right>this.sprite.x+this.sprite.box_left-this.range) &&
			(this.meli.sprite.x+this.meli.sprite.box_left<this.sprite.x+this.sprite.box_right+this.range)){
			this._heap.dx*=2;
		}

		this.sprite.move(this._heap.dx<<1,this.dy, this._heap.spriteMoveRes);

		if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_V){
			this.dy = 0;
			this.ay = 0;
		}
		if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_H){
			this.direction*=-1;
			this.frame_timer = 0;
		}
		

		this.toggleInteractionsStd();

		
		if (!(this.frame_timer&3)){
			if (this.direction>0){
				--this.sprite.frame;
				if(this.sprite.frame<0) this.sprite.frame=0;
			}
			if (this.direction<0){
				++this.sprite.frame;
				if (this.sprite.frame>3) this.sprite.frame=3;
			}
		}
		++this.frame_timer;
		
		
	};
	EN_MetalJerk.prototype.damage = function(source,points){
		this.direction*=-1;
		this.frame_timer = 0;
		if (points>0){
			this.hp-=points;
			return (this.hp<=0);
		}
		else{
			return false;
		}
	};
	EN_MetalJerk.prototype._inflictVal = {hp:2, chi:0};
	EN_MetalJerk.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_MetalJerk.prototype.drop = function(){
		//var dropRand = Math.random();
		this._heap.dropRand = Math.random();
		if (this._heap.dropRand<0.05){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.BIG);
		}
		else if (this._heap.dropRand<0.25){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.SMALL);
		}
		return null;
	};


