"use strict";

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

	this._basicInit(EN_Mosquito.params);

	//-----

	this.sprite.collision_flags |= (DEFINE_SPRITE_COLLISION_FLAGS.WATER);

	// full compliment of delta vars in case we ever want to attach flocking;
	// mostly we'll just use x and y
	this.dx = 0;
	this.dy = 0;
	this.dz = 0;
	this.z = 0;

	this.moveRes = new SpriteMoveResult();

	this.behavior = EN_Mosquito.behaviors.PATH;
	this.itinerary = []; // of {x:n,y:n}s
	this.itindex = 0;
	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;

	this._heap.targetx = this.sprite.x;
	this._heap.targety = this.sprite.y;
};
EN_Mosquito.spriteImg = SDS_Load_Image("art/sheets/sprite/fly.png");
EN_Mosquito.spriteSheet = new TileSheet(3, 3, 1, 2, EN_Mosquito.spriteImg);
EN_Mosquito.params = {
	name: "Mosquito",
	box: [0,3,7,5], // left, top, right, bottom
	max_step: 2,
	hp: 1,
	infection: 1,
};
EN_Mosquito.behaviors = {
		PATH: 0, // navigate waypoints
		TARGET: 1, // home to player
		FLOCK: 2 // do nothing; let flocking object control externally
	};

EN_Mosquito.prototype = new GO_Enemy();
	EN_Mosquito.prototype.ai = function(){
		if (!this.meli && !this.meliFind){
			this.meli = this.manager.getNamedObject("Meli");
			this.meliFind = true;
		}
		this._heap.targetx = this.sprite.x;
		this._heap.targety = this.sprite.y;
		switch (this.behavior){
			case EN_Mosquito.behaviors.PATH:
				if (this.itindex < this.itinerary.length){
					if (Math.abs(this.itinerary[this.itindex].x - (this.sprite.x + this.sprite.center.x)) + Math.abs(this.itinerary[this.itindex].y - (this.sprite.y + this.sprite.center.y)) < 9){
						this.itindex = (this.itindex+1)%this.itinerary.length;
					}
					this._heap.targetx = this.itinerary[this.itindex].x - this.sprite.center.x;
					this._heap.targety = this.itinerary[this.itindex].y - this.sprite.center.y;

					if (this.meli!=null && this.infection>0){
						if (Math.abs(this.meli.sprite.x + this.meli.sprite.center.x - (this.sprite.x + this.sprite.center.x)) + 
							Math.abs(this.meli.sprite.y + this.meli.sprite.center.y - (this.sprite.y + this.sprite.center.y)) < 16){
							this.behavior = EN_Mosquito.behaviors.TARGET;
						}
					}
				}
			break;
			case EN_Mosquito.behaviors.TARGET:
				if (this.infection>0 && this.meli!=null){
					this._heap.targetx = this.meli.sprite.x + this.meli.sprite.center.x - this.sprite.center.x;
					this._heap.targety = this.meli.sprite.y + this.meli.sprite.box_top - this.sprite.center.y;
				}
				else{
					this.behavior = EN_Mosquito.behaviors.PATH;
				}
			break;
			// FLOCK or other value is intentional nop
		}

		if (this.behavior != EN_Mosquito.behaviors.FLOCK && this.frame_timer===0){
			if (this._heap.targetx < this.sprite.x) --this.dx;
			if (this._heap.targetx > this.sprite.x) ++this.dx;
			if (this._heap.targety < this.sprite.y) --this.dy;
			if (this._heap.targety > this.sprite.y) ++this.dy;
			this.dx = Math.max(-3,Math.min(this.dx,3));
			this.dy = Math.max(-3,Math.min(this.dy,3));
		}

		this.sprite.move(this.dx,this.dy,this.moveRes);
		this.z+=this.dz;

		if (this.moveRes.flags & DEFINE_SPRITE_MOVE_RESULT_EJECTED_H){
			this.dx = -this.dx;
		}
		if (this.moveRes.flags & DEFINE_SPRITE_MOVE_RESULT_EJECTED_V){
			this.dy = -this.dy;
		}

		this.toggleInteractionsStd();

		++this.frame_timer;
		this.frame_timer&=1;
		this.sprite.frame = 0;
		if (this.infection>0) this.sprite.frame+=4;
		if (this.dx<0) this.sprite.frame+=2;
		this.sprite.frame += this.frame_timer;
	};
	EN_Mosquito.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=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_Mosquito.prototype._inflictVal = {hp:0, chi:1};
	EN_Mosquito.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_Mosquito.prototype.drop = function(){
		this._heap.dropRand = Math.random();
		if (this._heap.dropRand<0.03){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.SMALL);
		}
		return null;
	};


