"use strict";

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

	this._basicInit(EN_Stingray.params);

	//-----

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

	this.direction = 1;

	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_Stingray.spriteImg = SDS_Load_Image("art/sheets/sprite/stingray.png");
EN_Stingray.spriteSheet = new TileSheet(5, 4, 2, 3, EN_Stingray.spriteImg);
EN_Stingray.params = {
	name: "Stingray",
	box: [5,5,26,9], // left, top, right, bottom
	max_step: 2,
	hp: 3,
	infection: 2,
};

EN_Stingray.prototype = new GO_Enemy();

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

		this._heap.dy = 0;
		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)){
				
				if (this.meli.sprite.y+this.meli.sprite.box_bottom<this.sprite.y+this.sprite.box_top){
					this._heap.dy=-1;
				}
				else if (this.meli.sprite.y+this.meli.sprite.box_top>this.sprite.y+this.sprite.box_bottom){
					this._heap.dy=1;
				}
			}
		}

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

		if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_H){
			this.direction*=-1;
		}
		

		this.toggleInteractionsStd();

		this.frame_timer++;
		this.frame_timer&=15;
		//var frame = this.frame_timer;
		this._heap.frame = this.frame_timer>>1;
		if (this.infection>0) this._heap.frame+=16;
		if (this.direction<0) this._heap.frame+=8;
		this.sprite.frame=this._heap.frame;
	};
	EN_Stingray.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_Stingray.prototype._inflictVal = {hp:2, chi:0};
	EN_Stingray.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_Stingray.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;
	};


