"use strict";
/* Melody's daughter.  Hard to keep up with. */
var GO_Grace = function(initAI){
	this.sprite = new Sprite(GO_Grace.spriteSheet,"Grace");
	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.root = null; // GameObject to orbit
	this.target = null; // GameObject to point towards
	this.root_name = null;
	this.target_name = null;

	this.hp = 5;

	this.init_ai = initAI;

	this.bossfight = false;
	this.fightpath_index=0;
	this.fightpath_timer=0;

};
GO_Grace.spriteImg = SDS_Load_Image("art/sheets/sprite/child.png");
GO_Grace.spriteSheet = new TileSheet(4, 4, 2, 2, GO_Grace.spriteImg);

GO_Grace.prototype = new GameObject(
	new Sprite(
		GO_Grace.spriteSheet,
		"Grace"
	),
	"Grace"
	);

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

	GO_Grace.prototype.leftward_move_water = [0,-1,-2];
	GO_Grace.prototype.rightward_move_water = [0,1,2];
	GO_Grace.prototype.upward_move_water = [0,-1,-2];
	GO_Grace.prototype.downward_move_water = [0,1,2];
	GO_Grace.prototype.ANIM_STATES = {
		"bob" : 0,
		"swim" : 1,
		"dead" : 2
	};
	GO_Grace.prototype.anim_frames = [
		[0,0,1,1], //"bob" : 
		[0,1,0,2], //"swim" : 
		[7]        //"dead"
	];

	GO_Grace.prototype.fight_path = [
		{up: true,  right: true,  down: false, left: false},
		{up: false, right: true,  down: true,  left: false},
		{up: false, right: false, down: true,  left: true },
		{up: true,  right: false, down: false, left: true },

		{up: false, right: false, down: true,  left: true },
		{up: true,  right: false, down: false, left: true },
		{up: true,  right: true,  down: false, left: false},
		{up: false, right: true,  down: true,  left: false},
	];

	GO_Grace.prototype.checkEvent = function(sourceGO){
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
		
		if (!this.bossfight) return Dialogue_getCurrentEvent[this.name]();
		else{
			SDS.dlgMgr.open("Grace_c2_hp_"+this.hp,function(index,retval){
				if (retval!=null){
					
				}
			},true);
			return null;
		}
	}

	GO_Grace.prototype.pointAI = function(){
		if (!this.root || !this.target){
			if (!this.root_name || !this.target_name) return;
			this.root = this.manager.getNamedObject(this.root_name);
			this.target = this.manager.getNamedObject(this.target_name);

			if (!this.root || !this.target) return;
		}

		if (this.init_ai){
			if (this.root.sprite.isTouchingTerrain(DEFINE_MAP_TERRAIN_TYPES.WATER)){
				this.sprite.x = this.root.sprite.x;
				this.sprite.y = this.root.sprite.y;
			}
			this.init_ai=false;
			return;
		}

		// calculate center-to-center direction of target
		this._heap.vectorx = this.target.sprite.x + ((this.target.sprite.box_right+this.target.sprite.box_left)>>1) - 
			this.root.sprite.x - ((this.root.sprite.box_right+this.root.sprite.box_left)>>1);
		this._heap.vectory = this.target.sprite.y + ((this.target.sprite.box_bottom+this.target.sprite.box_top)>>1) - 
			this.root.sprite.y - ((this.root.sprite.box_bottom+this.root.sprite.box_top)>>1);

		this._heap.r = Math.sqrt(this._heap.vectorx*this._heap.vectorx + this._heap.vectory*this._heap.vectory); // wibbly-wobbly floaty-woaty... I feel dirty
		if (this._heap.r==0) return;
		this._heap.scale = 32/this._heap.r;
		this._heap.vectorx = Math.round(this._heap.vectorx*this._heap.scale);
		this._heap.vectory = Math.round(this._heap.vectory*this._heap.scale);

		this._heap.posx = this.root.sprite.x + this._heap.vectorx;
		this._heap.posy = this.root.sprite.y + this._heap.vectory;

		if (this._heap.posx<this.sprite.x){
			this.controls.unpress(this.controls,"right");
			this.controls.press(this.controls,"left");
		}
		else if (this._heap.posx>this.sprite.x){
			this.controls.unpress(this.controls,"left");
			this.controls.press(this.controls,"right");
		}
		else{
			this.controls.unpress(this.controls,"right");
			this.controls.unpress(this.controls,"left");
		}

		if (this._heap.posy<this.sprite.y){
			this.controls.unpress(this.controls,"down");
			this.controls.press(this.controls,"up");
		}
		else if (this._heap.posy>this.sprite.y){
			this.controls.unpress(this.controls,"up");
			this.controls.press(this.controls,"down");
		}
		else{
			this.controls.unpress(this.controls,"down");
			this.controls.unpress(this.controls,"up");
		}
	}
	GO_Grace.prototype.panicAI = function(){
		this.controls.held.up = this.fight_path[this.fightpath_index].up;
		this.controls.held.down = this.fight_path[this.fightpath_index].down;
		this.controls.held.left = this.fight_path[this.fightpath_index].left;
		this.controls.held.right = this.fight_path[this.fightpath_index].right;

		if(this.fightpath_timer++ == 40){
			this.fightpath_timer=0;
			this.fightpath_index = (this.fightpath_index+1)&7;
		}
	}

	GO_Grace.prototype.AI = function(){
		if (!this.bossfight){
			this.pointAI();
		}
		else{
			this.panicAI();
		}
	}

	GO_Grace.prototype.Update = function(){
		if (this.hp<=0){
			this._heap.oldAnimState = this.anim_state;
			this.anim_state = this.ANIM_STATES.dead;
			this.bossfight = false; // jic
			this.interactions = 0;
		}
		else{

			this.AI();

			this._heap.oldAnimState = this.anim_state;
			
			this.NPCSwim();
		}
		

		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;

		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;
		if (this.bossfight) this.sprite.frame+=4;
	}

