"use strict";
/* why write 7 monkeys when I can write 1?  And I suppose they're more giant tamarins than e.g. chimps... */
var GO_Monkey = function(whichMonkey){
	if (whichMonkey===null) whichMonkey="Bobo";

	if (GO_Monkey.names[whichMonkey]===null) whichMonkey="Bobo";
	this.name = whichMonkey;

	var monkeyIdx = GO_Monkey.names[whichMonkey];

	this.sprite = new Sprite(GO_Monkey.spriteSheets[monkeyIdx],whichMonkey);
	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.frame_timer=0;

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

	this.hanging = false;

};

GO_Monkey.spriteImgs = [
	SDS_Load_Image("art/sheets/sprite/monkey1.png"),
	SDS_Load_Image("art/sheets/sprite/monkey2.png"),
	SDS_Load_Image("art/sheets/sprite/monkey3.png"),
	SDS_Load_Image("art/sheets/sprite/monkey4.png"),
	SDS_Load_Image("art/sheets/sprite/monkey5.png"),
	SDS_Load_Image("art/sheets/sprite/monkey6.png"),
	SDS_Load_Image("art/sheets/sprite/monkey7.png")
];
GO_Monkey.spriteSheets = [
	new TileSheet(4, 4, 2, 2, GO_Monkey.spriteImgs[0]),
	new TileSheet(4, 4, 2, 2, GO_Monkey.spriteImgs[1]),
	new TileSheet(4, 4, 2, 2, GO_Monkey.spriteImgs[2]),
	new TileSheet(4, 4, 2, 2, GO_Monkey.spriteImgs[3]),
	new TileSheet(4, 4, 2, 2, GO_Monkey.spriteImgs[4]),
	new TileSheet(4, 4, 2, 2, GO_Monkey.spriteImgs[5]),
	new TileSheet(4, 4, 2, 2, GO_Monkey.spriteImgs[6])
];
GO_Monkey.names = {
	"Bobo" : 0,
	"Coco" : 1,
	"Momo" : 2,
	"Lolo" : 3,
	"Yoyo" : 4,
	"Toto" : 5,
	"Dodo" : 6
};

// for tradition
GO_Monkey.spriteImg = GO_Monkey.spriteImgs[0];
GO_Monkey.spriteSheet = GO_Monkey.spriteSheets[0];

GO_Monkey.prototype = new GameObject(
	new Sprite(
		GO_Monkey.spriteSheet,
		"Monkey"
	),
	"Monkey"
	);

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

	// NPC walk controls
	GO_Monkey.prototype.leftward_move_land = [0,-1,-2,-3];
	GO_Monkey.prototype.rightward_move_land = [0,1,2,3];
	GO_Monkey.prototype.leftward_move_water = [0,-1];
	GO_Monkey.prototype.rightward_move_water = [0,1];
	GO_Monkey.prototype.jump_rise_air = [ //40px = 2.5 blocks
		-4,-4,-4,-4,-4,-4,
		-3,-3,-3, 
		-2,-2,-2,
		-1,
		0, 0];
	GO_Monkey.prototype.fall_air = [ 
		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_Monkey.prototype.jump_rise_water = [ //80px = 5 blocks
		-4,-4,-4,-4,-4,-4,
		-4,-4,-4,-4,-4,-4,
		-3,-3,-3, 
		-3,-3,-3, 
		-2,-2,-2,
		-2,-2,-2,
		-1,
		-1,
		0, 0,
		0, 0];
	GO_Monkey.prototype.fall_water = [ 
		1, 
		2, 2, 2]; 


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

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

	GO_Monkey.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		if (this.controls.held.B && this.name=="Yoyo"){ // TODO if needed: check for touching a thing to hang from
			this.anim_state = this.ANIM_STATES.hang;
		}
		else{
			this.NPCWalk();
		}
		

		if (this._heap.oldAnimState != this.anim_state){
			this.frame_timer=0;
		}
		this.frame_timer++;
		if (this.anim_state==this.ANIM_STATES.walk){
			this._heap.aniFrame = (this.frame_timer>>2) % this.anim_frames[this.anim_state].length;
		}
		else{
			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];

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

