"use strict";

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

	this._basicInit(EN_Clam.params);

	//-----

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

	this.direction=-1;

	this.close_timer=0;
	this.closed=true;

	this.hunger_timer=0;

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

EN_Clam.prototype = new GO_Enemy();

	EN_Clam.prototype.ai = function(){
		if (this.infection>0){
			if (this.close_timer>0) this.close_timer--;
			if (this.hunger_timer>0) this.hunger_timer--;
			this.closed = (this.close_timer!=0);
		}
		else{
			this.closed=true;
		}

		if (this.closed){
			this.sprite.setBoundingBox(2,18,27,31);
		}
		else{
			this.sprite.setBoundingBox(2,5,27,31);
		}

		this.toggleInteractionsStd();
		if (this.infection>0){
			this.sprite.collision_flags &= ~DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
		}
		else{
			this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
		}

		// ABSTRACT:  A method for using one or more clams
		// to activate one or more switches placed in contact
		// with the one or more clams, wherein the state
		// of the one or more switches is made to correspond
		// to the open/closed state of the one or more clams
		// by purely artifical means.
		if (this.infection>0){
			//var collateralDamage = 0;
			this._heap.collateralDamage = 0;
			this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
			//var collateral = this.sprite.getCollisions();
			//this._heap.collateral = this.sprite.getCollisions();
			this.sprite.getCollisions(this._heap.collateral);
			// rely for the moment on there only being one Meli at a time
			//if (this._heap.collateral.length>0){
			//	if (this._heap.collateral[0].name=="Meli"){
			//		this._heap.collateral.shift();
			//	}
			//}
			this.sprite.collision_flags &= ~DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
			if (!this.closed && this.hunger_timer==0 && this._heap.collateral.length>0){
				this.close_timer = 60;
				this.hunger_timer = 120;
				this._heap.collateralDamage = 10;
			}
			if (!this.closed || this._heap.collateralDamage>0){
				for (this._heap.i=0; this._heap.i<this._heap.collateral.length; this._heap.i++){
					if (this._heap.collateral[this._heap.i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE){
						this._heap.collateral[this._heap.i].game_object.damage(this,this._heap.collateralDamage);
					}
				}
			}
		}

		this.sprite.frame = this.closed ? 0 : 1;
		if (this.infection>0) this.sprite.frame+=2;
		if (this.direction<0) this.sprite.frame+=4;
	};
	EN_Clam.prototype.damage = function(source,points){
		// if the attack is from behind, no damage.
		if (this.closed ||
			source.sprite.x + source.sprite.box_left < this.sprite.x+16 && this.direction>0 ||
			source.sprite.x + source.sprite.box_right > this.sprite.x+16 && this.direction<0){

			// TODO: deflection sound
			return false;
		}

		this.closed=true;
		this.close_timer = 60;
		if (points>0){
			this.hp-=points;
			return (this.hp<=0);
		}
		else{
			//var cough=null;
			this._heap.cough=null;

			if (this.infection>=2){
				this._heap.cough = new GO_Smoke();
				this._heap.cough.sprite.x = (this.direction<0) ? this.sprite.x : this.sprite.x+16;
				this._heap.cough.sprite.y = this.sprite.y+8;
				this._heap.cough.launch_dir.x=this.direction;
				this._heap.cough.launch_dir.y=-1;
				this._heap.cough.host_obj = this;

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

				this._heap.cough = new GO_Smoke();
				this._heap.cough.sprite.x = (this.direction<0) ? this.sprite.x : this.sprite.x+16;
				this._heap.cough.sprite.y = this.sprite.y+8;
				this._heap.cough.launch_dir.x=this.direction;
				this._heap.cough.launch_dir.y=1;
				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-=2;
			}
			if (this.infection>0){
				this._heap.cough = new GO_Smoke();
				this._heap.cough.sprite.x = (this.direction<0) ? this.sprite.x-8 : this.sprite.x+24;
				this._heap.cough.sprite.y = this.sprite.y+8;
				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_Clam.prototype._inflictVal = {hp:2, chi:0};
	EN_Clam.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_Clam.prototype.drop = function(){
		//var dropRand = Math.random();
		this._heap.dropRand = Math.random();
		if (this._heap.dropRand<0.4){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.BIG);
		}
		else if (this._heap.dropRand<0.9){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.SMALL);
		}
		return null;
	};


	