"use strict";
/* buried in his work, usually literally */
var GO_Ernest = function(){
	this.sprite = new Sprite(GO_Ernest.spriteSheet,"Ernest");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND | 
		DEFINE_SPRITE_COLLISION_FLAGS.AIR |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(2,0,13,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.anim_state = this.ANIM_STATES.bob;
	this.frame_timer=0;

	this.direction = 1; // +/-1 for facing

	this.investigating = false; // are we looking at something?

	this.idle_timer = 0;
	this._heap.investigateCounter = 1;
};
GO_Ernest.spriteImg = SDS_Load_Image("art/sheets/sprite/archaeologist.png");
GO_Ernest.spriteSheet = new TileSheet(4, 4, 2, 2, GO_Ernest.spriteImg);

GO_Ernest.prototype = new GameObject(
	new Sprite(
		GO_Ernest.spriteSheet,
		"Ernest"
	),
	"Ernest"
	);

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

	GO_Ernest.prototype.leftward_move_water = [0,-1,-2];
	GO_Ernest.prototype.rightward_move_water = [0,1,2];
	GO_Ernest.prototype.upward_move_water = [0,-1,-2];
	GO_Ernest.prototype.downward_move_water = [0,1,2];
	GO_Ernest.prototype.ANIM_STATES = {
		"bob" : 0,
		"swim" : 1,
		"investigate" : 2
	};
	GO_Ernest.prototype.anim_frames = [
		[0,0,1,1], //"bob" : 
		[0,1,0,2], //"swim" : 
		[6,4,5], //"investigate"
	];
	GO_Ernest.prototype.IDLE_BORED=1800;

	GO_Ernest.prototype.investigate = function(){
		this.idle_timer = this.IDLE_BORED;
		this.anim_state = this.ANIM_STATES.investigate;
		this.frame_timer=0;
	};

	GO_Ernest.prototype.checkEvent = function(sourceGO){
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
		
		this.idle_timer = Math.min(this.idle_timer,0);
		this.anim_state = this.ANIM_STATES.bob;
		this.frame_timer=0;

		return Dialogue_getCurrentEvent["Ernest"]();
	};

	GO_Ernest.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		if (this.idle_timer>=0) this.idle_timer++;

		
		this.anim_state = (this.anim_state == this.ANIM_STATES.investigate) ? this.ANIM_STATES.investigate : this.ANIM_STATES.bob;
		// if we're in the mood to investigate, and we got bored, investigate.
		if (this.investigating && this.anim_state!=this.ANIM_STATES.investigate && this.idle_timer>=this.IDLE_BORED){
			this.anim_state = this.ANIM_STATES.investigate;
		}

		this.NPCSwim(this.anim_state);
		

		if (this._heap.oldAnimState != this.anim_state){
			this.frame_timer=0;
		}
		this.frame_timer++;
		this._heap.aniFrame = (this.frame_timer>>2) % this.anim_frames[this.anim_state].length;
		// special case for investigating
		if (this.anim_state==this.ANIM_STATES.investigate){
			this._heap.investigateCounter += 
				((this.frame_timer>>2)&1) ^
				((this.frame_timer>>3)&1) ^
				((this.frame_timer>>5)&1);
			this._heap.investigateCounter -= 
				((this.frame_timer>>2)&1) ^
				((this.frame_timer>>4)&1) ^
				((this.frame_timer>>7)&1);
			this._heap.investigateCounter = Math.min(Math.max(this._heap.investigateCounter,0),2);
			this._heap.aniFrame = this._heap.investigateCounter;
		}

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

		this.sprite.frame = this.sprite.frame & 7;
		if (this.direction<0) this.sprite.frame+=8;
	};
