"use strict";

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

	this._basicInit(EN_Seahorse.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.bob_timer=0;

	this.bullet_timer=0;

	this.frame_timer=0;
};
EN_Seahorse.spriteImg = SDS_Load_Image("art/sheets/sprite/seahorse.png");
EN_Seahorse.spriteSheet = new TileSheet(4, 4, 2, 1, EN_Seahorse.spriteImg);
EN_Seahorse.params = {
	name: "Seahorse",
	box: [3,0,13,15], // left, top, right, bottom
	max_step: 2,
	hp: 3,
	infection: 1,
};

EN_Seahorse.prototype = new GO_Enemy();

	EN_Seahorse.prototype.bob = [0,0,1,2,3,2,1,0,0,-1,-2,-3,-2,-1];

	EN_Seahorse.prototype.ai = function(){
		if (!this.meli && !this.meliFind){
			this.meli = this.manager.getNamedObject("Meli");
			this.meliFind = true;
		}
		if (this.meli){
			this.direction = (this.meli.sprite.x>this.sprite.x) ? 1 : -1;
		}

		this.bob_timer++;
		//var dyi = this.bob_timer>>2;
		this._heap.dyi = this.bob_timer>>2;
		if (this._heap.dyi>=this.bob.length){
			this.bob_timer=0;
			this._heap.dyi=0;
		}

		this.sprite.move(0,this.bob[this._heap.dyi]);

		this.bullet_timer++;
		this.bullet_timer&=31;
		if (this.infection>0 && this.bullet_timer==0){
			// spit
			//var spit = new GO_Bullet((this.direction*2)<<3,0);
			this._heap.spit = new GO_Bullet((this.direction*2)<<3,0);
			this._heap.spit.sprite.x = (this.direction>0) ?
				this.sprite.x+15 :
				this.sprite.x-7;
			this._heap.spit.sprite.y = this.sprite.y+1;

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

			this._heap.spit = null;
		}

		this.toggleInteractionsStd();

		this.frame_timer++;
		this.frame_timer&=1;
		//var frame = this.frame_timer;
		this._heap.frame = this.frame_timer;
		if (this.infection>0) this._heap.frame+=2;
		if (this.direction<0) this._heap.frame+=4;
		this.sprite.frame=this._heap.frame;
	};
	EN_Seahorse.prototype.damage = function(source,points){
		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._heap.cough.sprite.y = this.sprite.y;
			this._heap.cough.launch_dir.x=this.direction;
			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_Seahorse.prototype._inflictVal = {hp:1, chi:0};
	EN_Seahorse.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_Seahorse.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;
	};


