"use strict";
/* land village innkeeper.  Possibly dyslexic. */
var GO_Gar = function(){

	this.sprite = new Sprite(GO_Gar.spriteSheet,"Gar");
	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_Gar.spriteImg = SDS_Load_Image("art/sheets/sprite/innkeeper_m.png");
GO_Gar.spriteSheet = new TileSheet(4, 4, 2, 1, GO_Gar.spriteImg);

GO_Gar.prototype = new GameObject(
	new Sprite(
		GO_Gar.spriteSheet,
		"Gar"
	),
	"Gar"
	);

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

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


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

	GO_Gar.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_Gar.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;
	}

