"use strict";

// Waits on a wall, and launches across when player draws near.
// some shameless logic recycle from EN_Stingray and EN_HappyShadow
var EN_ShadowmiteH = function(){
	this.sprite = new Sprite(EN_ShadowmiteH.spriteSheet,"ShadowmiteH");
	this.sprite.game_object = this; // no longer a generic GO

	this._basicInit(EN_ShadowmiteH.params);

	//-----

	this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.GROUND;

	this.direction = 1;
	this.behavior = EN_ShadowmiteH.behaviors.IDLE;

	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_ShadowmiteH.spriteImg = SDS_Load_Image("art/sheets/sprite/shadowmite.png");
EN_ShadowmiteH.spriteSheet = new TileSheet(4, 3, 1, 3, EN_ShadowmiteH.spriteImg);
EN_ShadowmiteH.params = {
	name: "ShadowmiteH",
	box: [1,1,14,6], // left, top, right, bottom
	max_step: 2,
	hp: 1,
	infection: 6,
};

EN_ShadowmiteH.behaviors = {
		IDLE: 0, // do nothing
		PRIME: 1, // reveal
		FLY: 2, // leap
		LAND: 3
	};

EN_ShadowmiteH.prototype = new GO_Enemy();

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

		switch(this.behavior){
			case EN_ShadowmiteH.behaviors.IDLE:
				if (this.meli){
					if (Math.abs(this.meli.sprite.y + this.meli.sprite.center.y - this.sprite.y - this.sprite.center.y)<32){
						this.behavior = EN_ShadowmiteH.behaviors.PRIME;
						this.frame_timer=-1;
					}
				}
			break;
			case EN_ShadowmiteH.behaviors.PRIME:
				if (this.meli){
					if (Math.abs(this.meli.sprite.y + this.meli.sprite.center.y - this.sprite.y - this.sprite.center.y)>=32){
						this.behavior = EN_ShadowmiteH.behaviors.IDLE;
						this.frame_timer=-1;
					}
					else if (Math.abs(this.meli.sprite.y + this.meli.sprite.center.y - this.sprite.y - this.sprite.center.y)<16 &&
						((this.meli.sprite.x + this.meli.sprite.center.x - this.sprite.x - this.sprite.center.x)*this.direction >= 0)){
						this.behavior = EN_ShadowmiteH.behaviors.FLY;
						this.frame_timer=-1;
					}
				}
			break;
			case EN_ShadowmiteH.behaviors.FLY:
				if (this.frame_timer>=2){
					this._heap.dy = 0;
					if (this.meli && this.infection>0){
							
						if (this.meli.sprite.y+this.meli.sprite.box_bottom<this.sprite.y+this.sprite.box_top){
							this._heap.dy=-(this.frame_timer&1); // only every other frame
						}
						else if (this.meli.sprite.y+this.meli.sprite.box_top>this.sprite.y+this.sprite.box_bottom){
							this._heap.dy=(this.frame_timer&1); // only every other frame
						}
						
					}

					this.sprite.move(this.direction<<2,this._heap.dy, this._heap.spriteMoveRes);

					if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_H){
						this.direction*=-1;
						this.behavior = EN_ShadowmiteH.behaviors.LAND;
						this.frame_timer=-1;
					}
				}
			break;
			case EN_ShadowmiteH.behaviors.LAND:
				if (this.frame_timer>=5){
					this.behavior = EN_ShadowmiteH.behaviors.IDLE;
					this.frame_timer=-1;
				}
			break;
		}

		
		

		this.toggleInteractionsStd();

		++this.frame_timer;
		
		switch(this.behavior){
			case EN_ShadowmiteH.behaviors.IDLE:
				this.sprite.frame = (this.direction>0) ? 8 : 12;
			break;
			case EN_ShadowmiteH.behaviors.PRIME:
				this.sprite.frame = (this.direction>0) ? 9 : 13;
			break;
			case EN_ShadowmiteH.behaviors.FLY:
				if (this.frame_timer<2) this.sprite.frame = (this.direction>0) ? 10 : 14;
				else this.sprite.frame = (this.direction>0) ? 11 : 15;
			break;
			case EN_ShadowmiteH.behaviors.LAND:
				this.sprite.frame = ((this.direction>0) ? 8 : 12) + (2 - (this.frame_timer>>1));
			break;
		}
	};
	EN_ShadowmiteH.prototype.damage = function(source,points){
		this.direction*=-1;
		if (points>0){
			this.hp-=points;
			return (this.hp<=0);
		}
		else{
			//var cough = new GO_Smoke();
			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_ShadowmiteH.prototype._inflictVal = {hp:1, chi:0};
	EN_ShadowmiteH.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_ShadowmiteH.prototype.drop = function(){
		//var dropRand = Math.random();
		this._heap.dropRand = Math.random();
		if (this._heap.dropRand<0.25){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.SMALL);
		}
		return null;
	};


