"use strict";
/* Zeke, keeper of the mountain shrine.  Chill old dude. */
var GO_Zeke = function(){
	this.sprite = new Sprite(GO_Zeke.spriteSheet,"Zeke");
	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;

	// very rudimentary. 0=none, 1=fakeblocks
	this.spell = 0;
	this.spell_timer = 0;
};
GO_Zeke.spriteImg = SDS_Load_Image("art/sheets/sprite/keeper4.png");
GO_Zeke.spriteSheet = new TileSheet(4, 4, 2, 2, GO_Zeke.spriteImg);

GO_Zeke.prototype = new GameObject(
	new Sprite(
		GO_Zeke.spriteSheet,
		"Zeke"
	),
	"Zeke"
	);

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

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

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


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

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

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

		return Dialogue_getCurrentEvent[this.name]();
	}

	GO_Zeke.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;

			if (this.spell==1){
				this._heap.block = new GO_FakeBullet(
					COS[this.spell_timer<<3]>>2,
					SIN[this.spell_timer<<3]>>2,
					GO_HolyBlock.spriteSheet,
					3,
					60,
					0
				);
				this._heap.block.spawnOnSprite(this.sprite,this.manager,0,0);

				++this.spell_timer;
				this.spell_timer &= 31;
			}
		}
		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;
			}
		}

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

