"use strict";
/* Gabe, acolyte of the sky shrine.  Well-intentioned, but a bit brainwashed. */
var GO_Gabe = function(){
	this.sprite = new Sprite(GO_Gabe.spriteSheet,"Gabe");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.GROUND |
		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.jump_index = 0;
	this.fall_index = 0;
	this.grounded = false;
	this.in_water_cache = false;
	this.anim_state = this.ANIM_STATES.fall;
	this.fall_air = this.fall_air_fast;
	this.frame_timer=0;

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

	this.flying = false;
	this.flight_timer = 0;
};
GO_Gabe.spriteImg = SDS_Load_Image("art/sheets/sprite/keeper4jr.png");
GO_Gabe.spriteSheet = new TileSheet(4, 4, 2, 2, GO_Gabe.spriteImg);

GO_Gabe.prototype = new GameObject(
	new Sprite(
		GO_Gabe.spriteSheet,
		"Gabe"
	),
	"Gabe"
	);

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

	// NPC walk controls
	GO_Gabe.prototype.leftward_move_land = [0,-1,-2];
	GO_Gabe.prototype.rightward_move_land = [0,1,2];
	GO_Gabe.prototype.leftward_move_water = [0,-1];
	GO_Gabe.prototype.rightward_move_water = [0,1];
	GO_Gabe.prototype.jump_rise_air = [ //40px = 2.5 blocks
		-4,-4,-4,-4,-4,-4,
		-3,-3,-3, 
		-2,-2,-2,
		-1,
		0, 0];
	GO_Gabe.prototype.fall_air_slow = [ 
		1, 
		2, 2, 2, 
		3, 3, 3]; // truncated at 3 because wings
	GO_Gabe.prototype.fall_air_fast = [ 
		1, 
		2, 2, 2, 
		3, 3, 3, 
		4, 4, 4, 4, 4, 4, 
		5, 5, 5, 5, 5, 5,
		6, 6, 6, 6, 6, 6,
		7, 7, 7, 7, 7, 7, 
		8];
	GO_Gabe.prototype.jump_rise_water = [ //8px = .5 blocks
		-2, -2, -2, 
		-1, -1, 
		0]; // maybe a bird; not a duck.
	GO_Gabe.prototype.fall_water = [ 
		-1]; // naturally buoyant; "fall" towards surface

	// SimpleMove (fly) controls
	GO_Gabe.prototype.leftward_move = GO_Gabe.prototype.leftward_move_land;
	GO_Gabe.prototype.rightward_move = GO_Gabe.prototype.rightward_move_land;
	GO_Gabe.prototype.upward_move = GO_Gabe.prototype.leftward_move_land;
	GO_Gabe.prototype.downward_move = GO_Gabe.prototype.rightward_move_land;


	GO_Gabe.prototype.ANIM_STATES = {
		"stand" : 0,
		"walk" : 1,
		"jump" : 2,
		"fall" : 3,
		"cast" : 4
	};
	GO_Gabe.prototype.anim_frames = [
		[0], //"stand" : 
		[4,5], //"walk" : 
		[6], //"jump" : 
		[7], //"fall" : 
		[2], //"cast" : 
	];

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

	GO_Gabe.prototype.checkEvent = function(sourceGO){
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;

		return Dialogue_getCurrentEvent[this.name]();
	}

	GO_Gabe.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		// check controls, set flying
		// to fly, "jump" in midair & hold
		if ((this.flying && this.controls.held.A) || (!this.grounded && !this.in_water_cache && this.controls.pressed.A)){
			this.flying = true;
			this.fall_index = 0;
		}
		else{
			this.flying=false;
		}

		
		if (this.controls.held.B){// yeah, casting & discharge will hold position
			this.fall_index = 0;
			this.anim_state = this.ANIM_STATES.cast;
		}
		else if (this.flying){
			this.SimpleMove();
			this.anim_state=this.ANIM_STATES.stand; // not because we're standing but because it's the base for the flight frame
			if (this.controls.held.left) this.direction=-1;
			if (this.controls.held.right) this.direction=1;
		}
		else{
			this.fall_air = this.controls.held.A ? this.fall_air_slow : this.fall_air_fast;
			this.NPCWalk();
		}
		

		if (this._heap.oldAnimState !== this.anim_state){
			this.frame_timer=0;
		}
		this.frame_timer++;
		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];

		if (this.flying){
			this.sprite.frame += 4;
			++this.flight_timer;
			this.flight_timer = this.flight_timer & 15;
			if (this.flight_timer & 8){
				++this.sprite.frame;
			}
		}
		else if (this.anim_state===this.ANIM_STATES.stand){
			if (this.controls.held.down){
				// look down custom
				++this.sprite.frame;
			}
			if (this.controls.held.up){
				// praise custom
				this.sprite.frame += 3;
			}
		}

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

