"use strict";
/* keeper of the second shrine; may have written the book on laissez-faire parenting */
var GO_Melody = function(){
	this.sprite = new Sprite(GO_Melody.spriteSheet,"Melody");
	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._heap.collisions = [];
};
GO_Melody.spriteImg = SDS_Load_Image("art/sheets/sprite/keeper2.png");
GO_Melody.spriteSheet = new TileSheet(4, 4, 2, 2, GO_Melody.spriteImg);

GO_Melody.prototype = new GameObject(
	new Sprite(
		GO_Melody.spriteSheet,
		"Melody"
	),
	"Melody"
	);

	GO_Melody.prototype.interactions = 0;
		//DEFINE_GAMEOBJECT_INTERACTIONS.CONTROL |
		//DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;

	GO_Melody.prototype.leftward_move_water = [0,-1,-2];
	GO_Melody.prototype.rightward_move_water = [0,1,2];
	GO_Melody.prototype.upward_move_water = [0,-1,-2];
	GO_Melody.prototype.downward_move_water = [0,1,2];
	GO_Melody.prototype.ANIM_STATES = {
		"bob" : 0,
		"swim" : 1,
		"out" : 2,
		"land" : 3
	};
	GO_Melody.prototype.anim_frames = [
		[0,0,1,1], //"bob" : 
		[0,1,0,2], //"swim" : 
		[4], //"out" :
		[5,6] //"land" :  
	];

	GO_Melody.prototype.checkEvent = function(sourceGO){
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
		
		return Dialogue_getCurrentEvent[this.name]();
	}

	GO_Melody.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		this._heap.i=0;
		
		this.NPCSwim();


		this._heap.inwater = this.sprite.isTouchingTerrain(DEFINE_MAP_TERRAIN_TYPES.WATER);
		if (!this._heap.inwater){
			this.sprite.getCollisions(this._heap.collisions);

			this._heap.foundLog=false;
			for (this._heap.i=0; !this._heap.foundLog && this._heap.i<this._heap.collisions.length; this._heap.i++){
				if (this._heap.collisions[this._heap.i].name=="Log"){
					this.anim_state=this.ANIM_STATES.out;
					this._heap.foundLog = true;
				}
			}
			if (!this._heap.foundLog){
				this.interactions = DEFINE_GAMEOBJECT_INTERACTIONS.CONTROL |
									DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;

				this.anim_state = this.controls.held.A ? this.ANIM_STATES.land : this.ANIM_STATES.out;
			}
		}
		else{
			this.interactions = DEFINE_GAMEOBJECT_INTERACTIONS.CONTROL |
									DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;
		}
		

		if (this._heap.oldAnimState != this.anim_state){
			this.frame_timer=0;
		}

		this._heap.aniFrame = 0;
		if (this.anim_state==this.ANIM_STATES.bob || this.anim_state==this.ANIM_STATES.swim){
			this.frame_timer++;
			this._heap.aniFrame = (this.frame_timer>>2) % this.anim_frames[this.anim_state].length;
		}
		else if(this.anim_state==this.ANIM_STATES.land){
			this.frame_timer++;
			this._heap.aniFrame = (this.frame_timer & 40) && !(this.frame_timer & 84);
			this._heap.aniFrame = this._heap.aniFrame ? 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;
	}

