"use strict";
/* Treasure-hunter and trail-worn traveler, searching the world for... wait, wrong game.  Wields a mean brush. */
var GO_Charis = function(){
	this.sprite = new Sprite(GO_Charis.spriteSheet,"Charis");
	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.leftward_move_water = [0,-1,-2];
	this.rightward_move_water = [0,1,2];
	this.horizontal_move_index=0;

	this.upward_move_water = [0,-1,-2];
	this.downward_move_water = [0,1,2];
	this.vertical_move_index = 0;

	this.anim_state = this.ANIM_STATES.bob;

	
	this.frame_timer=0;

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

	this.painting = false; // are we motivated to paint when bored?
	this.easel = null;
	this.inspiration = ""; // for painting
	this.inspiration_timer = 0;

	this.kicking=false;
	this.kick_strength=-1;
	this.kick_direction = this.direction; // because if I don't, I'll catch hell for the lag between the input and actual wave release
	this.kick_timer=0;

	this.idle_timer = 0;
};
GO_Charis.spriteImg = SDS_Load_Image("art/sheets/sprite/artist.png");
GO_Charis.spriteSheet = new TileSheet(4, 4, 2, 2, GO_Charis.spriteImg);

GO_Charis.prototype = new GameObject(
	new Sprite(
		GO_Charis.spriteSheet,
		"Charis"
	),
	"Charis"
	);

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

	GO_Charis.prototype.ANIM_STATES = {
		"bob" : 0,
		"swim" : 1,
		"paint" : 2,
		"kick" : 3,
		"pray" : 4,
	};
	GO_Charis.prototype.anim_frames = [
		[0,0,1,1], //"bob" : 
		[0,1,0,2], //"swim" :
		[4,5], //"paint"
		[0,6,7,6,0], //"kick"
		[3], //"pray"
	];
	GO_Charis.prototype.INSPIRATION_COMPLETE=300;
	GO_Charis.prototype.IDLE_BORED=450;

	GO_Charis.prototype.findEasel = function(){
		if (this.easel) return;
		if (this.manager){
			this.easel = this.manager.getNamedObject("CharisEasel");
		}
	};
	GO_Charis.prototype.paint = function(){
		this.idle_timer = this.IDLE_BORED;
		this.anim_state = this.ANIM_STATES.paint;
		this.frame_timer=0;
	};

	GO_Charis.prototype.checkEvent = function(sourceGO){
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
		
		this.idle_timer = 0;
		this.anim_state = this.ANIM_STATES.bob;
		this.frame_timer=0;

		// we see dead people
		for (this._heap.i=GLOBAL_DIARY.narrative.length-1; this.inspiration==="" && this._heap.i>=0; this._heap.i--){
			if (GLOBAL_DIARY.narrative[this._heap.i].chaptergory==="SOULS") this.inspiration=GLOBAL_DIARY.narrative[this._heap.i].eventname;
		}
		
		return Dialogue_getCurrentEvent["Charis"]();
	};

	GO_Charis.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		this.idle_timer++;

		
		this.anim_state = (this.anim_state == this.ANIM_STATES.paint) ? this.ANIM_STATES.paint : this.ANIM_STATES.bob;
		// if we're in the mood to paint, and we got bored, paint.
		if (this.painting && this.anim_state!=this.ANIM_STATES.paint && this.idle_timer>=this.IDLE_BORED){
			if (!this.easel){
				this.findEasel();
			}
			if (this.easel){
				this._heap.targety = this.easel.sprite.y;
				this._heap.targetx = this.easel.sprite.x + this.easel.facing*8;
				if (this._heap.targety<this.sprite.y){
					this.controls.unpress(this.controls,"down");
					this.controls.press(this.controls,"up");
				}
				else if (this._heap.targety>this.sprite.y){
					this.controls.press(this.controls,"down");
					this.controls.unpress(this.controls,"up");
				}
				else{
					this.controls.unpress(this.controls,"down");
					this.controls.unpress(this.controls,"up");
				}
				if (this._heap.targetx<this.sprite.x){
					this.controls.unpress(this.controls,"right");
					this.controls.press(this.controls,"left");
				}
				else if (this._heap.targetx>this.sprite.x){
					this.controls.press(this.controls,"right");
					this.controls.unpress(this.controls,"left");
				}
				else{
					this.controls.unpress(this.controls,"right");
					this.controls.unpress(this.controls,"left");
				}
				if (this.sprite.x==this._heap.targetx && this.sprite.y==this._heap.targety){
					this.direction = (this.easel.facing>0) ? -1 : 1;
					this.anim_state = this.ANIM_STATES.paint;
				}
			}
		}
		// if we're particularly inspired, paint something specific
		if (this.anim_state==this.ANIM_STATES.paint && this.inspiration!="" && this.inspiration_timer<this.INSPIRATION_COMPLETE){
			this.inspiration_timer++;
			if (this.inspiration_timer>=this.INSPIRATION_COMPLETE){
				if (!this.easel){
					this.findEasel();
				}
				if (this.easel){
					this.easel.paint(this.inspiration);
				}
			}
		}

		this.NPCSwim(this.anim_state);

		if (this.controls.pressed.A && !this.kicking){
			this.kicking=true;
			this.kick_strength = GO_KickWave.STRENGTH.STRONG;
			this.kick_direction = this.direction;
			this.kick_timer=-1;
		}
		if (this.controls.held.B && !this.kicking){
			this.anim_state = this.ANIM_STATES.pray;
		}

		this._heap.kickFrame = 0;
		if (this.kicking){
			this.kick_timer++;
			this._heap.kickFrame = this.kick_timer>>1;
			if (this.kick_timer == this.anim_frames[this.ANIM_STATES.kick].length){
				// fire ze kickwave!
				this._heap.wave = new GO_KickWave(this.kick_strength,this.kick_direction,this.sprite.constraints);
				
				this._heap.wave.spawnOnSprite(this.sprite,this.manager);
				this.playSound("FX_swish");
			}
			if (this._heap.kickFrame >= this.anim_frames[this.ANIM_STATES.kick].length){
				this.kicking=false;
			}
		}

		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;
		// special case for painting
		if (this.anim_state==this.ANIM_STATES.paint){
			this._heap.paintToggle = 
				((this.frame_timer>>3)&1) ^
				((this.frame_timer>>4)&1) ^
				((this.frame_timer>>5)&1) ^
				((this.frame_timer>>6)&1) ^
				((this.frame_timer>>7)&1) ^
				((this.frame_timer>>8)&1);
			this._heap.aniFrame = this._heap.paintToggle ? 1 : 0;
		}

		this.sprite.frame = this.anim_frames[this.anim_state][this._heap.aniFrame];
		if (this.kicking){
			this.sprite.frame = this.anim_frames[this.ANIM_STATES.kick][this._heap.kickFrame];
		}

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

