"use strict";

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

	this._basicInit(EN_Jerk.params);

	//-----

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

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

	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 = 0;
	this.frame_timer=0;
};
EN_Jerk.spriteImg = SDS_Load_Image("art/sheets/sprite/jerk.png");
EN_Jerk.spriteSheet = new TileSheet(4, 4, 2, 2, EN_Jerk.spriteImg);
EN_Jerk.params = {
	name: "Jerk",
	box: [1,3,14,12], // 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: 2,
	infection: 3, 
};

EN_Jerk.prototype = new GO_Enemy();

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

	EN_Jerk.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;
		}

		this._heap.seek=false;
		this._heap.still=false;

		// 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;
					this._heap.seek=true;
				}

				// 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;
					this._heap.seek=true;
				}*/

				// 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;
				//	this._heap.seek=true;
				//}
			}
		}
		else if (this.infection<=0){
			// land if not infected
			this._heap.targety = this.sprite.y+this.sprite.center.y+8;
		}

		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.infection>0) ? 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.infection){
				// perch if hit ground
				this._heap.still = true;
			}
		}
		if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_H){
			this.direction*=-1;
			this.frame_timer = 0;
		}
		

		this.toggleInteractionsStd();

		this._heap.anicycle = this._heap.seek ? 3 : 15;
		if (!(this.frame_timer&this._heap.anicycle)){
			++this.frame;
			this.frame = this.frame & 1;
		}
		++this.frame_timer;

		this.sprite.frame = this.frame;

		if (this.infection>0){
			this.sprite.frame += 8;
			if (this._heap.seek) this.sprite.frame += 2;
		}
		else{
			if (!this._heap.still) this.sprite.frame += 2;
		}
		if (this.direction<0) this.sprite.frame += 4;
		
		
	};
	EN_Jerk.prototype.damage = function(source,points){
		this.direction*=-1;
		this.frame_timer = 0;
		if (points>0){
			this.hp-=points;
			return (this.hp<=0);
		}
		else{
			this._heap.cough = new GO_Smoke();
			this._heap.cough.sprite.x = this.sprite.x + ((this.direction>0) ? 16 : 0);
			this._heap.cough.sprite.y = this.sprite.y;
			this._heap.cough.launch_dir.x=0;
			this._heap.cough.launch_dir.y=0;
			this._heap.cough.host_obj = this;

			this.sprite.map_room.addSprite(this._heap.cough.sprite);
			this.manager.add(this._heap.cough);

			this._heap.cough = null;

			this.infection--;
			return false;
		}
	};
	EN_Jerk.prototype._inflictVal = {hp:2, chi:0};
	EN_Jerk.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_Jerk.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;
	};


