"use strict";

// "Stalagmites MIGHT hang from the ceiling... but they don't!" - Brossentia
var EN_ShadowmiteV = function(){
	this.sprite = new Sprite(EN_ShadowmiteV.spriteSheet,"ShadowmiteV");
	this.sprite.game_object = this; // no longer a generic GO

	this._basicInit(EN_ShadowmiteV.params);

	//-----

	this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.GROUND;

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

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

EN_ShadowmiteV.prototype = new GO_Enemy();

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

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

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

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

		
		

		this.toggleInteractionsStd();

		++this.frame_timer;
		
		switch(this.behavior){
			case EN_ShadowmiteV.behaviors.IDLE:
				this.sprite.frame = (this.direction>0) ? 0 : 4;
			break;
			case EN_ShadowmiteV.behaviors.PRIME:
				this.sprite.frame = (this.direction>0) ? 1 : 5;
			break;
			case EN_ShadowmiteV.behaviors.FLY:
				if (this.frame_timer<2) this.sprite.frame = (this.direction>0) ? 2 : 6;
				else this.sprite.frame = (this.direction>0) ? 3 : 7;
			break;
			case EN_ShadowmiteV.behaviors.LAND:
				this.sprite.frame = ((this.direction>0) ? 0 : 4) + (2 - (this.frame_timer>>1));
			break;
		}
	};
	EN_ShadowmiteV.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.y = this.sprite.y + ((this.direction>0) ? 16 : 0);
			this._heap.cough.sprite.x = this.sprite.x;
			this._heap.cough.launch_dir.y=0;
			this._heap.cough.launch_dir.x=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_ShadowmiteV.prototype._inflictVal = {hp:1, chi:0};
	EN_ShadowmiteV.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_ShadowmiteV.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;
	};


