"use strict";
/* The Goddess.  Logic derived from Goddess, lore notwithstanding. */
var GO_Goddess = function(){
	this.sprite = new Sprite(GO_Goddess.spriteSheet,"Goddess");
	this.sprite.collision_flags = 
		//DEFINE_SPRITE_COLLISION_FLAGS.GROUND |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(10,0,21,31);
	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.neutral;
	
	this.frame_timer=0;

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

	this.fade_dir = true;
	this.fade_bitstring = 0;

	this.meli = null;
	this.meliFind=false; // she probably won't be in the map when we init; hopefully when we update
};
GO_Goddess.spriteImg = SDS_Load_Image("art/sheets/sprite/goddess.png");
GO_Goddess.spriteSheet = new TileSheet(5, 5, 2, 3, GO_Goddess.spriteImg);

GO_Goddess.prototype = new GameObject(
	new Sprite(
		GO_Goddess.spriteSheet,
		"Goddess"
	),
	"Goddess"
	);

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

	// SimpleMove (fly) controls
	GO_Goddess.prototype.leftward_move = [0,-1,-2];
	GO_Goddess.prototype.rightward_move = [0,1,2];
	GO_Goddess.prototype.upward_move = GO_Goddess.prototype.leftward_move;
	GO_Goddess.prototype.downward_move = GO_Goddess.prototype.rightward_move;

	//011101110101010100010001
	GO_Goddess.prototype.FADE_IN = 0x775511;
	//100010001010101011101110
	GO_Goddess.prototype.FADE_OUT = 0x775511;


	GO_Goddess.prototype.ANIM_STATES = {
		"neutral" : 0,
		"expository" : 1,
		"entreat" : 2, // or accusatory
		"focus" : 3,
	};
	GO_Goddess.prototype.anim_frames = [
		[0,1,2,3], //"neutral" : 
		[4,5,6,7], //"expository" : 
		[8,9,10,11], //"entreat" : 
		[12,13], //"focus" : 
	];

	GO_Goddess.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
	}
	GO_Goddess.prototype.clearFromRoom = function(mapRoom){
		mapRoom.removeSprite(this.sprite);
	}

	GO_Goddess.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]();

		return "Ending";
	}

	GO_Goddess.prototype.FadeIn = function(){
		this.fade_dir = true;
		this.fade_bitstring = this.FADE_IN;
	}
	GO_Goddess.prototype.FadeOut = function(){
		this.fade_dir = false;
		this.fade_bitstring = this.FADE_OUT;
	}

	GO_Goddess.prototype.Update = function(){
		// peaceful mob style target lookup
		if (!this.meli && !this.meliFind){
			this.meli = this.manager.getNamedObject("Meli");
			this.meliFind = true;
		}

		this._heap.oldAnimState = this.anim_state;

		
		if (this.controls.held.B && this.controls.held.A){
			this.anim_state = this.ANIM_STATES.focus;
		}
		else if (this.controls.held.B){
			this.anim_state = this.ANIM_STATES.entreat;
		}
		else if (this.controls.held.A){
			this.anim_state = this.ANIM_STATES.expository;
		}
		else{
			this.anim_state = this.ANIM_STATES.neutral;
		}
		
		this.SimpleMove();
		if (this.controls.held.left) this.direction=-1;
		if (this.controls.held.right) this.direction=1;
		
		if (this._heap.oldAnimState != this.anim_state){
			this.frame_timer=0;
		}
		this.frame_timer++;

		// normal case
		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];
		// special case
		if (this.anim_state == this.ANIM_STATES.focus){
			this._heap.aniFrame = this.frame_timer & 1;
			this.sprite.frame = this.anim_frames[this.anim_state][this._heap.aniFrame];
		}

		this.sprite.frame = this.sprite.frame & 15;
		if (this.direction<0) this.sprite.frame+=16;

		if (this.fade_bitstring!=0){
			this.sprite.visible = !!(this.fade_bitstring & 1);
			this.fade_bitstring = this.fade_bitstring>>1;
		}
		else this.sprite.visible = this.fade_dir;
	}

