"use strict";
/* Prefers to learn about the world through books and stories, and always busy writing more */
var GO_Lela = function(){
	this.sprite = new Sprite(GO_Lela.spriteSheet,"Lela");
	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.writing = false; // are we motivated to write when bored?

	this.idle_timer = 0;
};
GO_Lela.spriteImg = SDS_Load_Image("art/sheets/sprite/writer.png");
GO_Lela.spriteSheet = new TileSheet(4, 4, 2, 2, GO_Lela.spriteImg);

GO_Lela.prototype = new GameObject(
	new Sprite(
		GO_Lela.spriteSheet,
		"Lela"
	),
	"Lela"
	);

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

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

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

	GO_Lela.prototype.checkEvent = function(sourceGO){
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
		
		this.idle_timer = 0;
		this.anim_state = this.ANIM_STATES.bob;
		this.frame_timer=0;
		
		return Dialogue_getCurrentEvent["Lela"]();
	};

	GO_Lela.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		this.idle_timer++;

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

		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 writing
		if (this.anim_state==this.ANIM_STATES.write){
			this._heap.writeToggle = 
				((this.frame_timer>>2)&1) ^
				((this.frame_timer>>3)&1) ^
				((this.frame_timer>>4)&1) ^
				((this.frame_timer>>5)&1) ^
				((this.frame_timer>>6)&1) ^
				((this.frame_timer>>7)&1);
			this._heap.aniFrame = this._heap.writeToggle ? 1 : 0;
		}

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

