#include "Projectile.h"




Projectile::Projectile()
{
		 age=0;
		 age_of_death=3;
		
	damagetaken = -1;
	attributess = Statss();
	msglife=0;
	dieolog="";
	race = "none";
	projectile = "none";
	sex = none;
	state = 1;
scriptId = -1;
triggered = false;
type=3;//projectile type
}
void Projectile::Set_scriptId(int s)
{
	scriptId=s; 
}




void Projectile::fromString()
{	
	int j= 0;
	int i = 0;
	//maybey i acn make the conver for sring to char* right in here
	string temps[22]; //arry to hold data
	
	while (projectile[i]!= '`') //stay in untl you find the ` at the end
	{
		if (projectile[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += projectile[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}
	type = StringToNumber(temps[0]);
	name = temps[1];
	//race = temps[2];
	//sex = temps[3];
	//npcs.insert("ghgh,dfhg,hjh,2060,1594,32,32,3,32,32,0,1,6,4,1,5`");
	x = StringToNumber(temps[4]);  //move data from temp holder 

	y = StringToNumber(temps[5]);
	
	w = StringToNumber(temps[6]);
	
	h = StringToNumber(temps[7]);
	
	layer = StringToNumber(temps[8]);
	
	source_X = StringToNumber(temps[9]);

	source_Y = StringToNumber(temps[10]);
	 //bool collision= StringToNumber(temps[11]);
	
	frame = StringToNumber(temps[12]);
	direction= StringToNumber(temps[13]);
	
	source = StringToNumber(temps[14]);
	
	//dest= StringToNumber(temps[15]);
	id = StringToNumber(temps[16]);
	
	//	textprintf_centre(screen, font, 40, 200, makecol(255, 255, 255),"test source:%d", id);
	inarea = StringToNumber(temps[17]);
	//Sleep(5000);
	socketid = StringToNumber(temps[18]);// scoketId
	scriptId = StringToNumber(temps[19]);// 
}



string Projectile::toString()
{
	
		projectile = NumberToString(type) +","+ name + "," + race + "," + "sex" +
	"," + NumberToString(x) + 
	"," + NumberToString(y) + "," + NumberToString(w) +"," +NumberToString(h) +
	"," + NumberToString(layer) + "," + NumberToString(source_X) + 
	"," + NumberToString(source_Y) + "," + NumberToString(collision) + 
	"," + NumberToString(frame) + "," + NumberToString(direction) +
	"," + NumberToString(source) + "," + "dest" + 
	"," + NumberToString(id) + ","+ NumberToString(inarea) + "," + NumberToString(socketid) +"," + NumberToString(scriptId) +"`";
textout(screen,font,projectile.c_str(),100,50*id,makecol(255,255,255));
//Sleep(10000);
return projectile;
}



int Projectile::get_type()
{
	return type;
}
void Projectile::set_mesageLife(int t)
{
	msglife=t;
}

//gets
string Projectile::Get_unit()
{
return toString();
}

string Projectile::Get_dieolog()
{

	return dieolog;
}
int Projectile::Get_desty()
{

	return desty;
}
int Projectile::Get_destx()
{

	return destx;
}
int Projectile::Get_state()
{

	return state;
}


string Projectile::Get_race()
{
	return race;
}
gender Projectile::Get_sex()
{
	return sex;
}
//sets
void Projectile::Set_dieolog(string ch )
{
	dieolog = ch;
}
void Projectile::Set_desty(int ch )
{
	desty = ch;
}
void Projectile::Set_destx(int ch )
{
	destx = ch;
}
void Projectile::Set_state(int ch )
{
	state = ch;
}
void Projectile::Set_unit(string ch )
{
	projectile = ch;
}

void Projectile::Set_race(string New_race)
{
	race =  New_race ;
}
void Projectile::Set_sex(gender New_sex)
{
	sex = New_sex;
}

	int Projectile::Get_scriptId()
	{return scriptId;}

void Projectile::animate()// need do animation as 2darry headarry bodyarray armarray for each anmation
{
//need to put min and max frame in charitor class
	//that way there will be only one if stament do incrementing
if (connte == frspeed)
{
connte=0;
	switch(state)
	{
		case 1://idel
			frspeed =10;///glitch with animate when set to 1
			Set_W(32);
			Set_H(32);
			Set_sourcebitmap( 4);
			source_X = 17;
			if (Get_source_Y() < 5)
			{
				//do idel animation
				Set_source_Y(Get_source_Y() + 1);

			}
			else //if(Get_source_Y() < 5)//max frame
			{
				Set_source_Y(0); // min frame need to make dinamic 
			}
			//states = state;
		break;
		case 2: //walking
			
			Set_W(32);
			Set_H(32);
			Set_sourcebitmap( 4);
			source_X = 4 + direction;
			
			if (Get_source_Y() < 5)// remeber frames start at 0
			{
				//do walk animation
				Set_source_Y(Get_source_Y() + 1);

			}
			else //if(Get_source_Y() >= 6)//max frame
			{
				Set_source_Y(0); // min frame need to make dinamic 
			}
			//states = state;
		break;
	}

}
else
{
connte++;
}
}

void Projectile::do_moveing(Layer* map,View v)
{
/*
if (!Astar->ReachedGoal() && (destx==x) && (desty==y))
		{
         	Astar->PathNextNode();
		}
	//// i changed all 8's to 1's
	*/

	///animation seed is walk_count
	//character::walk_count=1;
/*
	if (ABS( x -character::pointx)<8 && ABS( y -character::pointy)<8)
	{
		character::	walking = false;
	}
*/
	
	
		
		
/*
		if (ABS(destx- x )<p)
		{
			p=ABS(destx- x );
		}
*/


	
		if (destx > x ) 
		{
			direction = 1;//RIGHT;
if (hit_wall( 1,map,v)==false)
	{
			x +=attributess.movmentRate;
		}
		}
	
		if (destx < x ) 
		{
			direction = 3;//LEFT;
if (hit_wall( 2 ,map,v)==false)
	{
			x -=attributess.movmentRate;
		}

		}
	
		/*
		if (ABS(desty- y )<p)
		{
			p=ABS(desty- y );
		}
		*/
	
		if (desty > y ) 
		{
			direction = 2;//DOWN;
if (hit_wall( 3,map,v)==false)
	{
			y +=attributess.movmentRate;
}
		}
	
	
		if (desty < y) 
		{
			direction = 0;//UP;
if (hit_wall( 4,map,v)==false)
	{
			y -=attributess.movmentRate;
}
		}
	

		//this take care of the fog of war movement
		//set_trans_blender(45, 45, 45, 100);
		//makecol(45,45,45)
		//makeacol(255,0,255,50)
		//floodfill(world, x /32, y /32,2);
		//circlefill(world, x /32, y /32,7,1);
		// our world bitmap tells us which tile to blit
		// 0 is a black tile
		// 1 is a darkenened tile
		// 2 is normal
		//state = 2;
}

void Projectile::talk(BITMAP* n,View v)
{
 

	//add a conter for life of message then ereas dieolog string
	// need to atach a txt raper for the hieght of the rect
	if (dieolog.length()> 0) //we have to make this if statement say if on screen too
	{


		if (msglife == 200)
		{
			dieolog="";
		}
		else
		{
			msglife++;
		}
		//max chars per line = 30
		if(dieolog.length()<=30)
		{
			//void drawRectangle(BITMAP *bmp,int x, int y, int w,int h,int color, int style, int bgcolor)
			drawRectangle(n,45+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h-10,text_length(font,dieolog.c_str())+10,text_height(font)+20,makecol(0,0,0),2,makeacol(255,0,255,100));
			textout(n,font,dieolog.c_str(),50+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h,makecol(255,255,255));//makecol(255,0,255)is trans
	
		}
		else if(dieolog.length()>30)
		{
			drawRectangle(n,45+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h-10,text_length(font,dieolog.substr(0,30).c_str())+10,text_height(font)+20*2,makecol(0,0,0),2,makeacol(255,0,255,100));
			textout(n,font,dieolog.substr(0,30).c_str(),50+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h,makecol(255,255,255));//makecol(255,0,255)is trans
			textout(n,font,dieolog.substr(31,dieolog.length()).c_str(),50+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,(10+y)-v.get_screen_pos_y()*32-v.get_scroll_y()-h,makecol(255,255,255));//makecol(255,0,255)is trans
		}
	}

}


void Projectile::showlife(BITMAP* n,View v,DATAFILE *data)
{
	if( damagetaken > -1)
	{
textprintf(n, font, 15+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w, y-v.get_screen_pos_y()*32-v.get_scroll_y()-h-5, makecol(255, 50, 50),"- %d" ,damagetaken);
	damagetaken = -1;
	}

	if (mouse_x >= x-v.get_screen_pos_x()*32-v.get_scroll_x()-w && mouse_y >= y-v.get_screen_pos_y()*32-v.get_scroll_y()-h)
	{
		if (mouse_x <= x-v.get_screen_pos_x()*32-v.get_scroll_x()+w && mouse_y <= y-v.get_screen_pos_y()*32-v.get_scroll_y()+h)
		{
			//void draw_bar(BITMAP *buf, int x, int y, int w, int h, int value, int range, int backcol, int startcol, int endcol)

textout(n,font,attributess.Get_statholder().c_str(),10,400,makecol(255,255,255));
			
draw_bar2(n, x-v.get_screen_pos_x()*32-v.get_scroll_x()-w, 10+y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, 100, 2, attributess.Gethp_bar(), 100,makecol(255,0,0), makecol(0,255,0),  makecol(0,0,255));
draw_bar2(n, x-v.get_screen_pos_x()*32-v.get_scroll_x()-w, 13+y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, 100, 2, attributess.Getattackstamana_bar(), 100,makecol(255,0,0), makecol(255,255,255),  makecol(0,0,255));
	
//targget= id;

		}	

	}

}



void Projectile::runStatScript()
{

	if(age>=age_of_death)
	{
	//kill projectile
	
		if(computerId==0)//only server
		{
			inboxbuffer += "0|0|005|"+NumberToString(id)+"~";//delet projectile from screens
		}

	}
	else
	{
		if(onesecpased==secon)
		{
			age++;
			secon=(!onesecpased);
		}
	}


	if(attributess.Gethp()<1) //check to see it dead
	{

		Set_inarea(1);
		if(computerId==Get_id())
		{
			ch_current_area = 1;//rig
		}
	}



}




void Projectile::runScript(Unit *onewhotiggeredme,DATAFILE *music)
{


	if(triggered== true)
	{

//if(attributess.attackstamana >= 50) //each telaport takes 50
//{
//attributess.attackstamana = attributess.attackstamana - 50; //attack take 50 stamana points to do
//onewhotiggeredme->Set_inarea(2);
//if(attributess.attackstamana < 0)
//{
//attributess.attackstamana=0;
//}

		switch(scriptId)
		{
			case 1:
				//do this
				onewhotiggeredme->Set_inarea(1);
if(computerId==onewhotiggeredme->Get_id())
	{
		play_sound(SAMPLE_MAGIC,music);
		ch_current_area = 1;//rig
		stop_song();
		play_song(MIDI_SPIKKO,music);

	}
				break;
			case 2:
				//do this
				onewhotiggeredme->Set_inarea(2);
if(computerId==onewhotiggeredme->Get_id())
	{
play_sound(SAMPLE_MAGIC,music);
		ch_current_area = 2;//rig
		stop_song();
		play_song(MIDI_BATTLE,music);
	}
				break;
			case 3:
				//do this
				onewhotiggeredme->Set_inarea(3);
if(computerId==onewhotiggeredme->Get_id())
	{
play_sound(SAMPLE_MAGIC,music);
		ch_current_area = 3;//rig
		stop_song();
		play_song(MIDI_FROG,music);
	}

				break;
			case 4:
				//do this
				onewhotiggeredme->Set_inarea(4);
if(computerId==onewhotiggeredme->Get_id())
	{
play_sound(SAMPLE_MAGIC,music);
		ch_current_area = 4;//rig
		stop_song();
		play_song(MIDI_GOTA,music);
	}
				break;
		}	






	}

//	}
	

}

