"use strict";
/* land village innkeeper.  Odd sense of humor. */
var GO_Brill = function(){

	this.sprite = new Sprite(GO_Brill.spriteSheet,"Brill");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(1,1,14,15);
	this.sprite.game_object = this; // no longer a generic GO

	this.controls = new Controls(); // just to init

	this.horizontal_move_index=0;
	this.vertical_move_index=0;
	this.jump_index = 0;
	this.fall_index = 0;
	this.grounded = false;
	this.in_water_cache = false;
	this.anim_state = this.ANIM_STATES.fall;
	this.frame_timer=0;

	this.direction = 1; // +/-1 for facing
};
GO_Brill.spriteImg = SDS_Load_Image("art/sheets/sprite/innkeeper_f.png");
GO_Brill.spriteSheet = new TileSheet(4, 4, 2, 1, GO_Brill.spriteImg);

GO_Brill.prototype = new GameObject(
	new Sprite(
		GO_Brill.spriteSheet,
		"Brill"
	),
	"Brill"
	);

	GO_Brill.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.CONTROL |
		DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;

	// NPC walk controls
	GO_Brill.prototype.leftward_move_land = [0,-1,-2,-3];
	GO_Brill.prototype.rightward_move_land = [0,1,2,3];
	GO_Brill.prototype.leftward_move_water = [0,-1];
	GO_Brill.prototype.rightward_move_water = [0,1];
	GO_Brill.prototype.jump_rise_air = [ //40px = 2.5 blocks
		-4,-4,-4,-4,-4,-4,
		-3,-3,-3, 
		-2,-2,-2,
		-1,
		0, 0];
	GO_Brill.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];
	GO_Brill.prototype.jump_rise_water = [ //80px = 5 blocks
		-4,-4,-4,-4,-4,-4,
		-4,-4,-4,-4,-4,-4,
		-3,-3,-3, 
		-3,-3,-3, 
		-2,-2,-2,
		-2,-2,-2,
		-1,
		-1,
		0, 0,
		0, 0];
	GO_Brill.prototype.fall_water = [ 
		1, 
		2, 2, 2]; 


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

	GO_Brill.prototype.checkEvent = function(sourceGO){
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
		
		// just for fun & debugging
		//if (sourceGO.controls){
		//	this.controls = sourceGO.controls;
		//}

		return Dialogue_getCurrentEvent[this.name]();
	}

	GO_Brill.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		this.NPCWalk();

		if (this._heap.oldAnimState != this.anim_state){
			this.frame_timer=0;
		}
		this.frame_timer++;
		if (this.anim_state==this.ANIM_STATES.walk){
			this._heap.aniFrame = (this.frame_timer>>2) % this.anim_frames[this.anim_state].length;
		}
		else{
			this._heap.aniFrame = (this.frame_timer>>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 & 3;
		if (this.direction<0) this.sprite.frame+=4;
	}

