"use strict";

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

	this._basicInit(EN_Hedgeboar.params);

	//-----

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

	this.controls = new Controls(); // ground-runner; easier to just patch in standard controls


	this.direction = 1;

	this.grounded = false;
	this.in_water_cache = false;
	this.horizontal_move_index = 0;
	this.jump_index = 0;
	this.fall_index = 0;

	this.meli = null;
	this.meliFind=false; // she probably won't be in the map when we init; hopefully when we update


	this.anim_state = this.ANIM_STATES.stand;
	this.frame_timer=0;

	this.behavior_state = this.BEHAVIOR_STATES.curious;
	this.behavior_timer = 0;
	this.leftward_move_land = this.leftward_walk_land;
	this.rightward_move_land = this.rightward_walk_land;
};
EN_Hedgeboar.spriteImg = SDS_Load_Image("art/sheets/sprite/hedgeboar.png");
EN_Hedgeboar.spriteSheet = new TileSheet(5, 4, 2, 2, EN_Hedgeboar.spriteImg);
EN_Hedgeboar.params = {
	name: "Hedgeboar",
	box: [2,2,29,15], // left, top, right, bottom
	max_step: 2,
	hp: 6,
	infection: 3,
};

EN_Hedgeboar.prototype = new GO_Enemy();

	EN_Hedgeboar.prototype.interactions = GO_Enemy.prototype.interactions |
			DEFINE_GAMEOBJECT_INTERACTIONS.CONTROL;
	
	// NPC walk controls
	EN_Hedgeboar.prototype.leftward_walk_land = [0,-1];
	EN_Hedgeboar.prototype.rightward_walk_land = [0,1];
	EN_Hedgeboar.prototype.leftward_charge_land = [0,-1,-2,-3];
	EN_Hedgeboar.prototype.rightward_charge_land = [0,1,2,3];
	EN_Hedgeboar.prototype.leftward_move_water = [0,-1];
	EN_Hedgeboar.prototype.rightward_move_water = [0,1];
	EN_Hedgeboar.prototype.jump_rise_air = [ //27px = 1.75 blocks
		-4,-4,-4,-4,
		-3,-3, 
		-2,-2,
		-1,
		0, 0];
	EN_Hedgeboar.prototype.fall_air = [ 
		1, 
		2, 2, 2, 
		3, 3, 3, 
		4, 4, 4, 4, 4, 4, 
		5, 5, 5, 5, 5, 5,
		6, 6, 6, 6, 6, 6,
		7, 7, 7, 7, 7, 7, 
		8];
	EN_Hedgeboar.prototype.jump_rise_water = [ //8px = .5 blocks
		-2, -2, -2, 
		-1, -1, 
		0]; // maybe a bird; not a duck.
	EN_Hedgeboar.prototype.fall_water = [ 
		-1]; // naturally buoyant; "fall" towards surface

	EN_Hedgeboar.prototype.ANIM_STATES = {
		"stand" : 0,
		"walk" : 1,
		"jump" : 2,
		"fall" : 3,
		"stun" : 4
	};
	EN_Hedgeboar.prototype.anim_frames = [
		[0], //"stand" : 
		[0,1,0,2], //"walk" : 
		[1], //"jump" : 
		[2], //"fall" :
		[3] // "stun" :
	];

	EN_Hedgeboar.prototype.BEHAVIOR_STATES = {
		"curious" : 0,
		"walk" : 1,
		"charge" : 2,
		"bonk" : 3,
		"stun" : 4,
		"dead" : 5 // special for Raviene's death spell
	};

	// Raviene's death spell uses the takedamage call for simplicity with Meli as the target
	// so the only other thing it gets used on also implements it
	EN_Hedgeboar.prototype.takedamage = function(damage){
		this.infection=0;
		this.hp=1;
		this.behavior_state=this.BEHAVIOR_STATES.dead;

		this._heap.drop = this.drop();
		if (this._heap.drop){
			this._heap.drop.spawnOnSprite(this.sprite,this.manager);
		}

		this._heap.poof = new GO_Poof("FX_splat");
		this._heap.poof.spawnOnSprite(this.sprite,this.manager);
		this._heap.poof = null;

		this.interactions = this.interactions | DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;
	}

	EN_Hedgeboar.prototype.checkEvent = function(sourceGO){
		if (this.behavior_state!=this.BEHAVIOR_STATES.dead) return "";// jic
		return "GD_Inspect_Hedgeboar";
	}

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

		this._heap.oldAnimState=this.anim_state;

		++this.behavior_timer;
		switch (this.behavior_state){
			case this.BEHAVIOR_STATES.curious:
				this.controls.unpress(this.controls,"left");
				this.controls.unpress(this.controls,"right");

				this._heap.seenMeli = this.meli && 
					(this.meli.sprite.y+this.meli.sprite.box_bottom>this.sprite.y+this.sprite.box_top) && 
					(this.meli.sprite.y+this.meli.sprite.box_top<this.sprite.y+this.sprite.box_bottom);
				this._heap.seenMeli &= this.meli &&
					((this.direction<0 && this.meli.sprite.x+this.meli.sprite.box_left < this.sprite.x+this.sprite.box_left) ||
					(this.direction>0 && this.meli.sprite.x+this.meli.sprite.box_right > this.sprite.x+this.sprite.box_right));	
				this._heap.seenMeli &= this.behavior_timer>=30;
				
				if (!this._heap.seenMeli && (this.behavior_timer&15)==0){
					this.direction*=-1;
				}

				if (this._heap.seenMeli || this.behavior_timer>=90){
					this.behavior_timer=0;
					this.behavior_state = (this.infection>0) ? this.BEHAVIOR_STATES.charge : this.BEHAVIOR_STATES.walk;
				}
			break;

			case this.BEHAVIOR_STATES.walk:
				this.leftward_move_land = this.leftward_walk_land;
				this.rightward_move_land = this.rightward_walk_land;
				this.controls.press(this.controls,this.direction<0 ? "left" : "right");
				this.controls.unpress(this.controls,this.direction<0 ? "right" : "left");
			break;
			case this.BEHAVIOR_STATES.charge:
				this.leftward_move_land = this.leftward_charge_land;
				this.rightward_move_land = this.rightward_charge_land;
				this.controls.press(this.controls,this.direction<0 ? "left" : "right");
				this.controls.unpress(this.controls,this.direction<0 ? "right" : "left");
			break;

			case this.BEHAVIOR_STATES.bonk:
				this.controls.unpress(this.controls,"left");
				this.controls.unpress(this.controls,"right");
				// manual override motion
				this._heap.dx = -this.direction;
				this._heap.dy = this.behavior_timer<7 ? -1 : this.behavior_timer>8 ? 1 : 0
				this.sprite.move(this._heap.dx,this._heap.dy,  this._heap.spriteMoveRes);
				if (this.behavior_timer>=15){
					this.behavior_timer=0;
					this.behavior_state = this.BEHAVIOR_STATES.stun;
				}
			break;

			case this.BEHAVIOR_STATES.stun:
				this.controls.unpress(this.controls,"left");
				this.controls.unpress(this.controls,"right");
				if (this.behavior_timer>=15){
					this.behavior_timer=0;
					this.behavior_state = this.BEHAVIOR_STATES.curious;
				}
			break;

			case this.BEHAVIOR_STATES.dead:
				this.controls.unpress(this.controls,"left");
				this.controls.unpress(this.controls,"right");
				this.behavior_timer=0;
			break;

		}

		
		if (this.behavior_state !== this.BEHAVIOR_STATES.bonk) this.NPCWalk();

		if (this._heap.spriteMoveRes.flags & DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_H){
			if (this.behavior_state===this.BEHAVIOR_STATES.charge) this.damage(this,0);
			else if (this.behavior_state===this.BEHAVIOR_STATES.walk) this.behavior_state=this.BEHAVIOR_STATES.curious;
			this._heap.spriteMoveRes.flags &= ~DEFINE_SPRITE_MOVE_RESULT_FLAGS.EJECTED_H; // because _heap is shared by all instances, and thus if another instance moves and we don't, we get its _moveRes :P
		}
		

		this.toggleInteractionsStd();

		if (this.behavior_state===this.BEHAVIOR_STATES.bonk || 
			this.behavior_state===this.BEHAVIOR_STATES.stun ||
			this.behavior_state===this.BEHAVIOR_STATES.dead){
			this.anim_state = this.ANIM_STATES.stun;
		}
		// else it will be stand/walk/fall as appropriate

		if (this._heap.oldAnimState != this.anim_state){
			this.frame_timer=0;
		}
		this.frame_timer++;
		this._heap.aniFrame = (this.frame_timer>>(this.infection ? 2 : 3)) % this.anim_frames[this.anim_state].length;

		this.sprite.frame = this.anim_frames[this.anim_state][this._heap.aniFrame];

		this.sprite.frame = this.sprite.frame & 7;

		if (this.infection>0) this.sprite.frame+=8;
		if (this.direction<0) this.sprite.frame+=4;
	};
	EN_Hedgeboar.prototype.damage = function(source,points){
		if (points>0) this.hp-=points;
		
		// bonk back the right way from attack
		if (source!=this){
			if (source.sprite.x + (source.sprite.box_left+source.sprite.box_right)>>1 < this.sprite.x + (this.sprite.box_left+this.sprite.box_right)>>1){
				this.direction = -1;
			}
			else{
				this.direction = 1;
			}
		}

		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=-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--;

		this.behavior_timer=0;
		this.behavior_state=this.BEHAVIOR_STATES.bonk;

		return (this.hp<=0);
		
	};
	EN_Hedgeboar.prototype._inflictVal = {hp:3, chi:0};
	EN_Hedgeboar.prototype.inflict = function(){
		return this._inflictVal;
	};
	EN_Hedgeboar.prototype.drop = function(){
		this._heap.dropRand = Math.random();
		if (this._heap.dropRand<0.75){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.BIG);
		}
		else if (this._heap.dropRand<0.99){
			return new GO_HeartDrop(GO_HeartDrop.SIZE.SMALL);
		}
		return null;
	};


